Skip to content

Commit 63bc597

Browse files
authored
Merge pull request #12 from swnck/dev
Add support for HTTP protocol version in AbstractRequest; update JxRe…
2 parents 45f8ec8 + c33d3f8 commit 63bc597

File tree

4 files changed

+37
-86
lines changed

4 files changed

+37
-86
lines changed

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ plugins {
55
}
66

77
group = "io.github.swnck"
8-
version = "1.0.4"
8+
version = "1.0.5"
99

1010
repositories {
1111
mavenCentral()

src/main/java/io/github/swnck/JxResponse.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ public JxResponse(AbstractRequest<?> request) {
5757
try {
5858
requestBuilder = HttpRequest.newBuilder()
5959
.uri(new URI(urlWithParams))
60+
.version(request.getVersion())
6061
.timeout(Duration.ofMillis(request.getTimeoutMillis()))
6162
.method(request.getMethod().toString(),
6263
(bodyContent == null) ? HttpRequest.BodyPublishers.noBody() : HttpRequest.BodyPublishers.ofString(bodyContent));

src/main/java/io/github/swnck/request/AbstractRequest.java

Lines changed: 24 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import lombok.Getter;
99
import lombok.Setter;
1010

11+
import java.net.http.HttpClient;
1112
import java.time.Duration;
1213
import java.util.HashMap;
1314
import java.util.Map;
@@ -23,103 +24,20 @@
2324
@Getter
2425
@Setter
2526
public abstract class AbstractRequest<T extends AbstractRequest<T>>{
26-
27-
/**
28-
* A collection of key-value pairs representing the HTTP headers for the request.
29-
* <p>
30-
* Each key in the map corresponds to a header name, and its associated value
31-
* represents the header value. These headers are used to define additional
32-
* metadata or configuration for the outgoing HTTP request.
33-
* <p>
34-
* This map is initialized as an empty {@code HashMap} and can be modified
35-
* using associated methods such as {@code setHeader} or {@code setHeaders}.
36-
* <p>
37-
* The map is marked as {@code final}, ensuring that it cannot be reassigned
38-
* after being initialized, while still allowing modification of its contents.
39-
*/
4027
private final Map<String, Object> headers = new HashMap<>();
4128

42-
/**
43-
* Represents a collection of query parameters to be included in an HTTP request.
44-
* <p>
45-
* This map stores key-value pairs where the key is the name of the query parameter
46-
* and the value is the parameter's value. It is used to construct the query string
47-
* included in the request's URL. Adding, modifying, or removing parameters in this
48-
* map directly affects the query parameters of the associated request.
49-
* <p>
50-
* Query parameters can be set or updated using methods such as {@code setQueryParam(String, String)}
51-
* or {@code setQueryParams(Map<String, Object>)}.
52-
* <p>
53-
* The map is initialized as an empty {@code HashMap} and will contain no parameters by default.
54-
* <p>
55-
* This field is immutable, and its reference cannot be reassigned.
56-
*/
5729
private final Map<String, Object> queryParams = new HashMap<>();
5830

59-
/**
60-
* Represents the HTTP method used for the current request.
61-
* This field stores the specific HTTP method type, such as GET, POST,
62-
* DELETE, PUT, or PATCH, to dictate how the request interacts with
63-
* the target resource.
64-
* <p>
65-
* The {@code method} is initialized during the construction of the
66-
* {@code AbstractRequest} or its subclasses, and it governs the
67-
* behavior of the HTTP operation performed by the request.
68-
* <p>
69-
* Supported HTTP methods:
70-
* - GET: Retrieve data from the server.
71-
* - POST: Send data to the server for creation.
72-
* - DELETE: Remove a resource on the server.
73-
* - PUT: Create or overwrite a resource on the server.
74-
* - PATCH: Make partial modifications to a resource on the server.
75-
*
76-
* @see Method
77-
*/
7831
private Method method;
7932

80-
/**
81-
* Represents the Cross-Origin Resource Sharing (CORS) configuration for the request.
82-
* This variable holds an instance of the {@code Cors} class, which defines the CORS
83-
* headers to be included with the HTTP request.
84-
* <p>
85-
* The {@code Cors} class provides methods for specifying allowed origins, HTTP methods,
86-
* headers, and credential settings for configuring cross-origin requests.
87-
* <p>
88-
* If set, the CORS headers defined in this variable are applied to the request,
89-
* enabling the appropriate handling of cross-origin policies.
90-
* <p>
91-
* Default value is {@code null} when no CORS configuration is set.
92-
*/
9333
private Cors cors = null;
9434

95-
/**
96-
* The URL to which the HTTP request will be sent.
97-
* This field is expected to hold the target endpoint of the request,
98-
* including the protocol (e.g., "http://" or "https://") and, optionally,
99-
* any path or query components.
100-
* <p>
101-
* The value of this field must be initialized either through a constructor
102-
* or using the `setUrl` method, ensuring the string starts with a valid
103-
* protocol. If not explicitly provided, "http://" is prefixed by default.
104-
* <p>
105-
* This field is critical for configuring the desired endpoint for the request
106-
* execution and is used in conjunction with other request elements
107-
* such as headers, query parameters, and HTTP methods.
108-
*/
10935
private String url;
11036

111-
/**
112-
* The timeout duration in milliseconds for the execution of the request.
113-
* This value specifies the maximum amount of time the request is allowed to run
114-
* before being forcibly terminated or marked as timed out.
115-
* <p>
116-
* A default value of 100000 milliseconds (100 seconds) is assigned, signifying the request
117-
* will wait up to 100 seconds unless a different timeout is set using the
118-
* appropriate configuration method. Adjusting this value allows tuning
119-
* the request's timeout based on specific use cases or requirements.
120-
*/
12137
private int timeoutMillis = 100000;
12238

39+
private HttpClient.Version version = HttpClient.Version.HTTP_1_1;
40+
12341
/**
12442
* Constructs a new instance of {@code AbstractRequest} with the specified URL and HTTP method.
12543
* This constructor initializes the request with the provided URL and method, sets the default
@@ -380,6 +298,27 @@ public T setTimeout(int timeoutMillis) {
380298
return (T) this;
381299
}
382300

301+
302+
/**
303+
* Sets the HTTP protocol version for the current request.
304+
*
305+
* This method allows the user to specify the desired {@link HttpClient.Version}
306+
* to be used for the request. The specified version is stored and updated
307+
* within the request instance.
308+
*
309+
* @param version the HTTP protocol version to set; must not be null.
310+
* @return the updated instance of the request, allowing for method chaining.
311+
* @throws IllegalArgumentException if the provided version is null.
312+
*/
313+
@SuppressWarnings("unchecked")
314+
public T setVersion(HttpClient.Version version) {
315+
if (version == null) {
316+
throw new IllegalArgumentException("Version cannot be null");
317+
}
318+
this.version = version;
319+
return (T) this;
320+
}
321+
383322
/**
384323
* Sets the timeout duration for the request execution.
385324
* The timeout specifies the period after which the request will time out if not completed.

src/test/java/io/github/swnck/JxRequestTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import io.github.swnck.request.PostRequest;
66
import org.junit.jupiter.api.Test;
77

8+
import java.net.http.HttpClient;
9+
810
import static org.junit.jupiter.api.Assertions.assertNotNull;
911

1012
public class JxRequestTest {
@@ -38,6 +40,15 @@ void delete() {
3840
assertNotNull(response);
3941
}
4042

43+
@Test
44+
void getWithVersion() {
45+
GetRequest jxRequest = JxRequest.get("http://localhost:8080/users/")
46+
.setVersion(HttpClient.Version.HTTP_2);
47+
JxResponse response = jxRequest.send();
48+
System.out.println(response);
49+
assertNotNull(response);
50+
}
51+
4152
@Test
4253
void postWithParam() {
4354
PostRequest jxRequest = JxRequest.post()

0 commit comments

Comments
 (0)