11package io .github .swnck ;
22
3+ import io .github .swnck .request .AbstractBody ;
34import io .github .swnck .request .AbstractRequest ;
4- import io .github .swnck .request .PostRequest ;
55import io .github .swnck .util .ContentType ;
66import io .github .swnck .util .StatusCode ;
77import lombok .Getter ;
1515import java .net .http .HttpRequest ;
1616import java .net .http .HttpResponse ;
1717import java .nio .charset .StandardCharsets ;
18+ import java .time .Duration ;
1819import java .util .List ;
1920import java .util .Map ;
20- import java .util .Objects ;
2121import java .util .concurrent .CompletableFuture ;
2222
23+ /**
24+ * Represents an HTTP response retrieved using an asynchronous HTTP client.
25+ * This class is designed to process a given request, initiate the corresponding HTTP call,
26+ * and handle the response data, including body, status code, headers, and timing.
27+ */
2328@ Getter
2429@ Setter
2530public class JxResponse {
2631 private static final HttpClient CLIENT = HttpClient .newBuilder ().build ();
2732 private static final Logger LOGGER = LoggerFactory .getLogger (JxResponse .class );
2833
29-
3034 private String body ;
3135 private String contentType ;
3236 private String uri ;
@@ -36,15 +40,24 @@ public class JxResponse {
3640
3741 private Map <String , List <String >> headers ;
3842
43+ /**
44+ * Constructs a JxResponse object by initiating and handling an asynchronous HTTP request based on the provided request object.
45+ * Handles the composition of the request URI with query parameters, method type, headers, and optional body content.
46+ * Completes the operation by storing the response details such as body, status code, headers, and execution duration.
47+ *
48+ * @param request the {@link AbstractRequest} object containing the details of the HTTP request,
49+ * including the URL, query parameters, headers, method, timeout, and body (if applicable).
50+ */
3951 public JxResponse (AbstractRequest <?> request ) {
40- String urlWithParams = buildUrlWithParams (request .url , request .getQueryParams ());
41- String bodyContent = (request instanceof PostRequest ) ? ((PostRequest ) request ).getBody () : null ;
52+ String urlWithParams = buildUrlWithParams (request .getUrl () , request .getQueryParams ());
53+ String bodyContent = (request instanceof AbstractBody <?> ) ? ((AbstractBody <?> ) request ).getBody () : null ;
4254
4355 HttpRequest .Builder requestBuilder ;
4456
4557 try {
4658 requestBuilder = HttpRequest .newBuilder ()
4759 .uri (new URI (urlWithParams ))
60+ .timeout (Duration .ofMillis (request .getTimeoutMillis ()))
4861 .method (request .getMethod ().toString (),
4962 (bodyContent == null ) ? HttpRequest .BodyPublishers .noBody () : HttpRequest .BodyPublishers .ofString (bodyContent ));
5063 } catch (Exception e ) {
@@ -53,7 +66,6 @@ public JxResponse(AbstractRequest<?> request) {
5366 }
5467
5568 request .getHeaders ().forEach ((key , value ) -> requestBuilder .header (key , value .toString ()));
56- requestBuilder .header ("Content-Type" , Objects .requireNonNullElse (request .contentType , ContentType .TEXT_PLAIN ).getMimeType ());
5769
5870 HttpRequest httpRequest = requestBuilder .build ();
5971
@@ -63,7 +75,8 @@ public JxResponse(AbstractRequest<?> request) {
6375
6476 response .thenAccept (httpResponse -> {
6577 this .body = httpResponse .body ();
66- this .contentType = httpResponse .headers ().firstValue ("Content-Type" ).orElse (ContentType .TEXT_PLAIN .getMimeType ());
78+ this .contentType = httpResponse .headers ().firstValue ("Content-Type" )
79+ .orElse (ContentType .TEXT_PLAIN .getMimeType ());
6780 this .statusCode = httpResponse .statusCode ();
6881 this .uri = httpResponse .uri ().toString ();
6982 this .headers = httpResponse .headers ().map ();
@@ -76,6 +89,15 @@ public JxResponse(AbstractRequest<?> request) {
7689 }).join ();
7790 }
7891
92+ /**
93+ * Constructs a complete URL by appending query parameters to the base URL.
94+ * If the provided query parameters are null or empty, the original URL is returned as is.
95+ * Handles encoding of both keys and values in the query parameters to ensure proper URL formatting.
96+ *
97+ * @param url the base URL to which query parameters will be appended
98+ * @param queryParams a map containing query parameter keys and their corresponding values
99+ * @return the complete URL with query parameters appended and properly encoded
100+ */
79101 private String buildUrlWithParams (String url , Map <String , Object > queryParams ) {
80102 if (queryParams == null || queryParams .isEmpty ()) return url ;
81103 StringBuilder sb = new StringBuilder (url );
0 commit comments