3434import org .slf4j .Logger ;
3535import org .slf4j .LoggerFactory ;
3636
37- class RequestSender {
37+ public class RequestSender {
3838
3939 private static final Logger log = LoggerFactory .getLogger (RequestSender .class );
40- private final String name ;
41- private final String hostUrl ;
42- private final Map <String , String > defaultHeaders ;
40+
41+ @ NotNull
4342 private final HttpClient httpClient ;
44- private final long socketTimeoutSec ;
4543
46- public RequestSender (
47- final String name ,
48- String hostUrl ,
49- Map <String , String > defaultHeaders ,
50- HttpClient httpClient ,
51- long socketTimeoutSec ) {
52- this .name = name ;
53- this .httpClient = httpClient ;
54- this .hostUrl = hostUrl ;
55- this .defaultHeaders = defaultHeaders ;
56- this .socketTimeoutSec = socketTimeoutSec ;
44+ @ NotNull
45+ private final RequestSender .KeyProperties keyProps ;
46+
47+ public RequestSender (@ NotNull RequestSender .KeyProperties keyProps ) {
48+ this .keyProps = keyProps ;
49+ this .httpClient = HttpClient .newBuilder ()
50+ .connectTimeout (Duration .ofSeconds (keyProps .connectionTimeoutSec ))
51+ .build ();
5752 }
5853
5954 /**
@@ -71,10 +66,10 @@ HttpResponse<String> sendRequest(
7166 @ Nullable Map <String , String > headers ,
7267 @ Nullable String httpMethod ,
7368 @ Nullable String body ) {
74- URI uri = URI .create (hostUrl + endpoint );
75- HttpRequest .Builder builder = newBuilder ().uri (uri ).timeout (Duration .ofSeconds (socketTimeoutSec ));
69+ URI uri = URI .create (keyProps . hostUrl + endpoint );
70+ HttpRequest .Builder builder = newBuilder ().uri (uri ).timeout (Duration .ofSeconds (keyProps . socketTimeoutSec ));
7671
77- if (keepDefHeaders ) defaultHeaders .forEach (builder ::header );
72+ if (keepDefHeaders ) keyProps . defaultHeaders .forEach (builder ::header );
7873 if (headers != null ) headers .forEach (builder ::header );
7974
8075 HttpRequest .BodyPublisher bodyPublisher =
@@ -91,7 +86,7 @@ private HttpResponse<String> sendRequest(HttpRequest request) {
9186 try {
9287 CompletableFuture <HttpResponse <String >> futureResponse =
9388 httpClient .sendAsync (request , HttpResponse .BodyHandlers .ofString ());
94- response = futureResponse .get (socketTimeoutSec , TimeUnit .SECONDS );
89+ response = futureResponse .get (keyProps . socketTimeoutSec , TimeUnit .SECONDS );
9590 return response ;
9691 } catch (Exception e ) {
9792 log .warn ("We got exception while executing Http request against remote server." , e );
@@ -101,13 +96,29 @@ private HttpResponse<String> sendRequest(HttpRequest request) {
10196 log .info (
10297 "[Storage API stats => type,storageId,host,method,path,status,timeTakenMs,resSize] - StorageAPIStats {} {} {} {} {} {} {} {}" ,
10398 "HttpStorage" ,
104- this .name ,
105- this .hostUrl ,
99+ keyProps .name ,
100+ keyProps .hostUrl ,
106101 request .method (),
107102 request .uri (),
108103 (response == null ) ? "-" : response .statusCode (),
109104 executionTime ,
110105 (response == null ) ? 0 : response .body ().length ());
111106 }
112107 }
108+
109+ public boolean hasKeyProps (KeyProperties thatKeyProps ) {
110+ return this .keyProps .equals (thatKeyProps );
111+ }
112+
113+ /**
114+ * Set of properties that are just enough to construct the sender
115+ * and distinguish unambiguously between objects
116+ * in terms of their effective configuration
117+ */
118+ public record KeyProperties (
119+ @ NotNull String name ,
120+ @ NotNull String hostUrl ,
121+ @ NotNull Map <String , String > defaultHeaders ,
122+ long connectionTimeoutSec ,
123+ long socketTimeoutSec ) {}
113124}
0 commit comments