-
Notifications
You must be signed in to change notification settings - Fork 0
Add timeout support and comprehensive documentation #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 4 commits
383887f
c7a6a49
61ece20
40594f8
380b9f9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,21 +2,176 @@ | |
|
|
||
| [](https://search.maven.org/search?q=g:%22io.github.ModularEnigma%22%20AND%20a:%22Requests%22) | ||
|
|
||
| A lightweight Java library for building and executing HTTP requests. Wraps `java.net.http.HttpClient` behind a fluent builder API. | ||
|
|
||
| ## Requirements | ||
|
|
||
| - Java 17+ | ||
|
|
||
| ## Installation | ||
|
|
||
| Please visit the [Maven Central Repository](https://search.maven.org/artifact/io.github.ModularEnigma/Requests) | ||
| for instructions on how to add Requests to your project. | ||
| Add the dependency to your `pom.xml`: | ||
|
|
||
| ```xml | ||
| <dependency> | ||
| <groupId>io.github.ModularEnigma</groupId> | ||
| <artifactId>Requests</artifactId> | ||
| <version>x.y.z</version> | ||
| </dependency> | ||
| ``` | ||
|
|
||
| Replace `x.y.z` with the version shown in the badge above. For other build tools (Gradle, sbt, etc.) see the [Maven Central Repository](https://search.maven.org/artifact/io.github.ModularEnigma/Requests). | ||
|
|
||
| ## Usage | ||
|
|
||
| ### GET request | ||
|
|
||
| ```java | ||
| Response response = Request.builder() | ||
| .setURL("https://api.example.com/users/1") | ||
| .setMethod(Request.Method.GET) | ||
| .build() | ||
| .execute(); | ||
|
|
||
| int status = response.getStatusCode(); // e.g. 200 | ||
| String body = response.getBody(); // raw response body | ||
| ``` | ||
|
|
||
| ### POST request | ||
|
|
||
| ```java | ||
| Response response = Request.builder() | ||
| .setURL("https://api.example.com/users") | ||
| .setMethod(Request.Method.POST) | ||
| .setRequestBody("{\"name\": \"Alice\"}") | ||
| .build() | ||
| .execute(); | ||
| ``` | ||
|
|
||
| ### Custom headers | ||
|
|
||
| ```java | ||
| Response response = Request.builder() | ||
| .setURL("https://api.example.com/protected") | ||
| .setMethod(Request.Method.GET) | ||
| .addHeader("Authorization", "Bearer my-token") | ||
| .addHeader("X-Request-ID", "abc123") | ||
| .build() | ||
| .execute(); | ||
| ``` | ||
|
|
||
| `Accept: application/json` and `Content-Type: application/json` are set automatically. Headers added via `addHeader` are appended using `HttpRequest.Builder.header(...)`, which adds an additional header value rather than replacing any existing one. This means adding `Accept` or `Content-Type` via `addHeader` will result in duplicate header values, not an override; avoid re-adding those headers if you intend to change the defaults. | ||
|
|
||
| ### Timeout | ||
|
|
||
| ```java | ||
| import java.time.Duration; | ||
|
|
||
| Response response = Request.builder() | ||
| .setURL("https://api.example.com/heartbeat") | ||
| .setMethod(Request.Method.GET) | ||
| .setTimeout(Duration.ofSeconds(10)) | ||
| .build() | ||
| .execute(); | ||
| ``` | ||
|
|
||
| When a timeout is set and the server does not respond in time, `execute()` throws a `RuntimeException` wrapping a `java.net.http.HttpTimeoutException`. Without a timeout, the request blocks until the OS-level socket timeout fires (which can be several minutes). | ||
|
|
||
| ### Reading the response | ||
|
|
||
| However, for Maven at a glanc, add this to your `pom.xml`: | ||
| ```java | ||
| Response response = /* ... */; | ||
|
|
||
| response.getStatusCode(); // int — HTTP status code (200, 404, 500, …) | ||
| response.getBody(); // String — raw response body, may be empty | ||
| response.getHeaders(); // Map<String, List<String>> — all response headers | ||
| ``` | ||
|
|
||
| ## API reference | ||
|
|
||
| ### `RequestBuilder` | ||
|
|
||
| Obtain an instance via `Request.builder()`. | ||
|
|
||
| | Method | Required | Description | | ||
| |---|---|---| | ||
| | `setURL(String url)` | Yes | Target URL. Must be a valid RFC 2396 URI. | | ||
| | `setMethod(Request.Method method)` | Yes | `GET` or `POST`. | | ||
| | `setRequestBody(String body)` | No | Request body string (typically JSON). Relevant for `POST`. | | ||
| | `addHeader(String header, String value)` | No | Add a custom request header. May be called multiple times. | | ||
| | `setTimeout(Duration timeout)` | No | Per-request response timeout. Must be a positive `Duration`. | | ||
| | `build()` | — | Constructs and returns a `Request`. Throws `IllegalStateException` if `url` or `method` is missing. | | ||
|
|
||
| ### `Request` | ||
|
|
||
| | Method | Description | | ||
| |---|---| | ||
| | `execute()` | Sends the request synchronously and returns a `Response`. Throws `RuntimeException` on I/O error, interruption, or timeout. | | ||
|
|
||
| ### `Response` | ||
|
|
||
| | Method | Return type | Description | | ||
| |---|---|---| | ||
| | `getStatusCode()` | `int` | HTTP status code. | | ||
| | `getBody()` | `String` | Response body. May be empty. | | ||
| | `getHeaders()` | `Map<String, List<String>>` | Response headers. | | ||
|
|
||
| ## Publishing a new version | ||
|
|
||
| ### Prerequisites | ||
|
|
||
| - Write access to the repository | ||
| - A GPG key configured and known to Maven (used to sign artifacts) | ||
| - Sonatype OSSRH credentials in `~/.m2/settings.xml`: | ||
|
|
||
| ```xml | ||
| <dependencies> | ||
| <dependency> | ||
| <groupId>io.github.ModularEnigma</groupId> | ||
| <artifactId>Requests</artifactId> | ||
| <version>{$version}</version> | ||
| </dependency> | ||
| </dependencies> | ||
| <settings> | ||
| <servers> | ||
| <server> | ||
| <id>ossrh</id> | ||
| <username>your-sonatype-username</username> | ||
| <password>your-sonatype-password</password> | ||
| </server> | ||
| </servers> | ||
| </settings> | ||
| ``` | ||
|
|
||
| Where `{$version}` is replaced with the blue version text from the badge above. | ||
| ### Steps | ||
|
|
||
| 1. **Bump the version** in `pom.xml`: | ||
|
|
||
| ```xml | ||
| <version>1.0.5</version> | ||
| ``` | ||
|
|
||
| 2. **Commit the version bump**: | ||
|
|
||
| ```bash | ||
| git add pom.xml | ||
| git commit -m "Release 1.0.5" | ||
| git tag v1.0.5 | ||
| git push origin master --tags | ||
| ``` | ||
|
|
||
| 3. **Deploy to OSSRH staging**: | ||
|
|
||
| ```bash | ||
| mvn clean deploy | ||
| ``` | ||
|
|
||
| This compiles, runs tests, attaches sources and Javadoc jars, signs all artifacts with GPG, and uploads everything to the OSSRH staging repository. | ||
|
|
||
| 4. **Release from staging to Maven Central**: | ||
|
|
||
| - Log in to [https://s01.oss.sonatype.org](https://s01.oss.sonatype.org) | ||
| - Navigate to **Staging Repositories** | ||
| - Select the repository, click **Close**, then **Release** | ||
| - Propagation to Maven Central typically takes 10–30 minutes | ||
|
|
||
| Alternatively, add the `nexus-staging-maven-plugin` to `pom.xml` and run: | ||
|
|
||
| ```bash | ||
| mvn nexus-staging:release | ||
| ``` | ||
|
Comment on lines
+121
to
+175
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chain🌐 Web query:
💡 Result: Sonatype’s legacy OSSRH (oss.sonatype.org / s01.oss.sonatype.org) reached end-of-life on June 30, 2025, aligned with the EOL of the underlying Nexus Repository Manager 2 technology. Publishing to Maven Central is expected to move to the Sonatype Central Publisher Portal (“Central Portal”). [1] What this means for publishers:
Sources: 🌐 Web query:
💡 Result: What happened (timeline)
What you need to do in 2026 (migration actions)
Sources[1] Sonatype: OSSRH Sunset Announcement (EOL June 30, 2025) https://central.sonatype.org/news/20250326_ossrh_sunset/ 🌐 Web query:
💡 Result: Sonatype deprecated and sunset (shut down) the legacy OSSRH publishing service—including What this means for you now (as of March 9, 2026):
Sources: Sonatype OSSRH Sunset Announcement (Mar 26, 2025) [1]; Sonatype “OSSRH Sunset / EOL” page (states OSSRH shut down as of Jun 30, 2025) [2]; Sonatype Central post on OSSRH namespaces visible in the Portal (Oct 7, 2024) [3]. Update the publishing guide to use Sonatype Central Publisher Portal. The documented OSSRH publishing flow is no longer functional. Sonatype shut down OSSRH on June 30, 2025, and Update this section to document the Central Publisher Portal workflow, including Portal user tokens for authentication and the new publishing endpoints. If you need to support legacy OSSRH-style tooling, Sonatype provides a compatibility endpoint at See Sonatype's OSSRH EOL documentation and Central Publisher Portal setup guide for migration details. 🤖 Prompt for AI Agents |
||
|
|
||
| 5. **Update the README** if the feature set changed, then push. | ||
Uh oh!
There was an error while loading. Please reload this page.