|
8 | 8 | import lombok.Getter; |
9 | 9 | import lombok.Setter; |
10 | 10 |
|
| 11 | +import java.net.http.HttpClient; |
11 | 12 | import java.time.Duration; |
12 | 13 | import java.util.HashMap; |
13 | 14 | import java.util.Map; |
|
23 | 24 | @Getter |
24 | 25 | @Setter |
25 | 26 | 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 | | - */ |
40 | 27 | private final Map<String, Object> headers = new HashMap<>(); |
41 | 28 |
|
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 | | - */ |
57 | 29 | private final Map<String, Object> queryParams = new HashMap<>(); |
58 | 30 |
|
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 | | - */ |
78 | 31 | private Method method; |
79 | 32 |
|
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 | | - */ |
93 | 33 | private Cors cors = null; |
94 | 34 |
|
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 | | - */ |
109 | 35 | private String url; |
110 | 36 |
|
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 | | - */ |
121 | 37 | private int timeoutMillis = 100000; |
122 | 38 |
|
| 39 | + private HttpClient.Version version = HttpClient.Version.HTTP_1_1; |
| 40 | + |
123 | 41 | /** |
124 | 42 | * Constructs a new instance of {@code AbstractRequest} with the specified URL and HTTP method. |
125 | 43 | * This constructor initializes the request with the provided URL and method, sets the default |
@@ -380,6 +298,27 @@ public T setTimeout(int timeoutMillis) { |
380 | 298 | return (T) this; |
381 | 299 | } |
382 | 300 |
|
| 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 | + |
383 | 322 | /** |
384 | 323 | * Sets the timeout duration for the request execution. |
385 | 324 | * The timeout specifies the period after which the request will time out if not completed. |
|
0 commit comments