3535import com .g42cloud .sdk .core .http .Field ;
3636import com .g42cloud .sdk .core .http .HttpClient ;
3737import com .g42cloud .sdk .core .http .HttpConfig ;
38+ import com .g42cloud .sdk .core .http .HttpMethod ;
3839import com .g42cloud .sdk .core .http .HttpRequest ;
3940import com .g42cloud .sdk .core .http .HttpRequestDef ;
4041import com .g42cloud .sdk .core .http .HttpResponse ;
@@ -157,12 +158,12 @@ public HcClient preInvoke(Map<String, String> extraHeader) {
157158 return client ;
158159 }
159160
160- public <ReqT , ResT > ResT syncInvokeHttp (ReqT request , HttpRequestDef <ReqT , ResT > reqDef )
161+ public <R , S > S syncInvokeHttp (R request , HttpRequestDef <R , S > reqDef )
161162 throws ServiceResponseException {
162163 return syncInvokeHttp (request , reqDef , new SdkExchange ());
163164 }
164165
165- public <ReqT , ResT > ResT syncInvokeHttp (ReqT request , HttpRequestDef <ReqT , ResT > reqDef , SdkExchange exchange )
166+ public <R , S > S syncInvokeHttp (R request , HttpRequestDef <R , S > reqDef , SdkExchange exchange )
166167 throws ServiceResponseException {
167168
168169 if (Objects .isNull (exchange )) {
@@ -206,12 +207,12 @@ public <ReqT, ResT> ResT syncInvokeHttp(ReqT request, HttpRequestDef<ReqT, ResT>
206207 }
207208 }
208209
209- public <ReqT , ResT > CompletableFuture <ResT > asyncInvokeHttp (ReqT request , HttpRequestDef <ReqT , ResT > reqDef ) {
210+ public <R , S > CompletableFuture <S > asyncInvokeHttp (R request , HttpRequestDef <R , S > reqDef ) {
210211 return asyncInvokeHttp (request , reqDef , new SdkExchange ());
211212 }
212213
213- public <ReqT , ResT > CompletableFuture <ResT > asyncInvokeHttp (ReqT request , HttpRequestDef <ReqT , ResT > reqDef ,
214- SdkExchange exchange ) {
214+ public <R , S > CompletableFuture <S > asyncInvokeHttp (R request , HttpRequestDef <R , S > reqDef ,
215+ SdkExchange exchange ) {
215216
216217 if (Objects .isNull (exchange )) {
217218 return CompletableFuture .supplyAsync (() -> {
@@ -227,7 +228,7 @@ public <ReqT, ResT> CompletableFuture<ResT> asyncInvokeHttp(ReqT request, HttpRe
227228 try {
228229 httpRequest = buildRequest (request , reqDef );
229230 } catch (SdkException e ) {
230- CompletableFuture <ResT > future = new CompletableFuture <>();
231+ CompletableFuture <S > future = new CompletableFuture <>();
231232 future .completeExceptionally (e );
232233 return future ;
233234 }
@@ -254,52 +255,48 @@ public <ReqT, ResT> CompletableFuture<ResT> asyncInvokeHttp(ReqT request, HttpRe
254255 }, httpConfig .getExecutorService ());
255256 }
256257
257- protected <ReqT , ResT > HttpRequest buildRequest (ReqT request , HttpRequestDef <ReqT , ResT > reqDef ) {
258+ protected <R , S > HttpRequest buildRequest (R request , HttpRequestDef <R , S > reqDef ) {
258259 String endpoint = this .endpoints .get (endpointIndex .intValue ());
259260 HttpRequest .HttpRequestBuilder httpRequestBuilder = HttpRequest .newBuilder ();
260261 httpRequestBuilder .withMethod (reqDef .getMethod ())
261- .withContentType (reqDef .getContentType ())
262262 .withEndpoint (endpoint )
263263 .withPath (reqDef .getUri ());
264264
265- for (Field <ReqT , ?> field : reqDef .getRequestFields ()) {
266- Optional <?> reqValueOption ;
267- if (httpConfig .isIgnoreRequiredValidation ()) {
268- reqValueOption = field .readValueNoValidation (request );
269- } else {
270- reqValueOption = field .readValue (request );
265+ boolean hasBody = false ;
266+ for (Field <R , ?> field : reqDef .getRequestFields ()) {
267+ Optional <?> reqValueOption = httpConfig .isIgnoreRequiredValidation () ?
268+ field .readValueNoValidation (request ) : field .readValue (request );
269+
270+ if (!reqValueOption .isPresent ()) {
271+ continue ;
271272 }
272- if ( reqValueOption .isPresent ()) {
273- Object reqValue = reqValueOption . get ();
274- if ( field . getLocation () == LocationType . Header ) {
273+ Object reqValue = reqValueOption .get ();
274+ switch ( field . getLocation ()) {
275+ case Header :
275276 httpRequestBuilder .addHeader (field .getName (), convertToStringParams (reqValue ));
276- } else if (field .getLocation () == LocationType .Query ) {
277+ break ;
278+ case Query :
277279 buildQueryParams (httpRequestBuilder , field .getName (), reqValue );
278- } else if (field .getLocation () == LocationType .Path ) {
280+ break ;
281+ case Path :
279282 httpRequestBuilder .addPathParam (field .getName (), convertToStringParams (reqValue ));
280- } else if (field .getLocation () == LocationType .Body ) {
283+ break ;
284+ case Body :
281285 buildRequestBody (httpRequestBuilder , reqValue );
282- } else if (field .getLocation () == LocationType .Cname ) {
283- try {
284- URL url = new URL (
285- endpoint .replace ("\r " , "" ).replace ("\n " , "" ));
286- StringBuilder endpointBuilder = new StringBuilder ();
287- endpointBuilder .append (url .getProtocol ())
288- .append ("://" )
289- .append (reqValue )
290- .append ("." )
291- .append (url .getHost ());
292- if (!StringUtils .isEmpty (url .getPath ())) {
293- endpointBuilder .append ("/" ).append (url .getPath ());
294- }
295- httpRequestBuilder .withEndpoint (endpointBuilder .toString ());
296- } catch (MalformedURLException e ) {
297- throw new SdkException ("Failed to parse endpoint" );
298- }
299- }
286+ hasBody = true ;
287+ break ;
288+ case Cname :
289+ buildCname (httpRequestBuilder , endpoint , reqValue );
290+ break ;
291+ default :
292+ break ;
300293 }
301294 }
302295
296+ if (!(this .httpConfig .isIgnoreContentTypeForGetRequest () && reqDef .getMethod () == HttpMethod .GET && !hasBody )) {
297+ httpRequestBuilder .withContentType (reqDef .getContentType ());
298+ }
299+
303300 // handle upload/download progress
304301 if (request instanceof ProgressRequest ) {
305302 ProgressRequest progressRequest = (ProgressRequest ) request ;
@@ -319,13 +316,32 @@ protected <ReqT, ResT> HttpRequest buildRequest(ReqT request, HttpRequestDef<Req
319316 }
320317
321318 // sign algorithm
322- if (Objects .nonNull (httpConfig ) && Objects . nonNull ( httpConfig .getSigningAlgorithm ())) {
319+ if (Objects .nonNull (httpConfig .getSigningAlgorithm ())) {
323320 httpRequestBuilder .withSigningAlgorithm (httpConfig .getSigningAlgorithm ());
324321 }
325322
326323 return httpRequestBuilder .build ();
327324 }
328325
326+ private void buildCname (HttpRequest .HttpRequestBuilder httpRequestBuilder , String endpoint , Object reqValue ) {
327+ try {
328+ URL url = new URL (
329+ endpoint .replace ("\r " , "" ).replace ("\n " , "" ));
330+ StringBuilder endpointBuilder = new StringBuilder ();
331+ endpointBuilder .append (url .getProtocol ())
332+ .append ("://" )
333+ .append (reqValue )
334+ .append ("." )
335+ .append (url .getHost ());
336+ if (!StringUtils .isEmpty (url .getPath ())) {
337+ endpointBuilder .append ("/" ).append (url .getPath ());
338+ }
339+ httpRequestBuilder .withEndpoint (endpointBuilder .toString ());
340+ } catch (MalformedURLException e ) {
341+ throw new SdkException ("Failed to parse endpoint" );
342+ }
343+ }
344+
329345 private void buildRequestBody (HttpRequest .HttpRequestBuilder httpRequestBuilder , Object reqValue ) {
330346 if (reqValue instanceof SdkFormDataBody ) {
331347 httpRequestBuilder .withFormDataPart (((SdkFormDataBody ) reqValue ).buildFormData ());
@@ -402,10 +418,10 @@ private void handleException(HttpRequest httpRequest, HttpResponse httpResponse)
402418 }
403419 }
404420
405- private <T > T processTextBasedType (HttpResponse httpResponse , HttpRequestDef <?, T > reqDef )
421+ private <S > S processTextBasedType (HttpResponse httpResponse , HttpRequestDef <?, S > reqDef )
406422 throws InstantiationException , IllegalAccessException {
407423
408- T response ;
424+ S response ;
409425 String stringResult = httpResponse .getBodyAsString ();
410426 int statusCode = httpResponse .getStatusCode ();
411427 if (SdkSerializable .class .isAssignableFrom (reqDef .getResponseType ())) {
@@ -419,7 +435,7 @@ private <T> T processTextBasedType(HttpResponse httpResponse, HttpRequestDef<?,
419435 }
420436
421437 if (reqDef .hasResponseField (String .valueOf (statusCode ))) {
422- Field <T , ?> resTField = reqDef .getResponseField (String .valueOf (statusCode ));
438+ Field <S , ?> resTField = reqDef .getResponseField (String .valueOf (statusCode ));
423439 resTField .writeValueSafe (response ,
424440 JsonUtils .toObjectIgnoreUnknown (stringResult , resTField .getFieldType ()),
425441 resTField .getFieldType ());
@@ -428,23 +444,23 @@ private <T> T processTextBasedType(HttpResponse httpResponse, HttpRequestDef<?,
428444 } else {
429445 // process response with body[object, map, list, string...]
430446 response = reqDef .getResponseType ().newInstance ();
431- Field <T , ?> responseField = reqDef .getResponseField (Constants .BODY );
447+ Field <S , ?> responseField = reqDef .getResponseField (Constants .BODY );
432448 Object obj = responseToObject (stringResult , responseField );
433449 responseField .writeValueSafe (response , obj , responseField .getFieldType ());
434450
435451 if (reqDef .hasResponseField (String .valueOf (statusCode ))) {
436- Field <T , ?> resTField = reqDef .getResponseField (String .valueOf (statusCode ));
452+ Field <S , ?> resTField = reqDef .getResponseField (String .valueOf (statusCode ));
437453 resTField .writeValueSafe (response , obj , resTField .getFieldType ());
438454 }
439455 }
440456
441457 return response ;
442458 }
443459
444- private <T > T processStreamType (HttpRequest httpRequest , HttpResponse httpResponse , HttpRequestDef <?, T > reqDef )
460+ private <S > S processStreamType (HttpRequest httpRequest , HttpResponse httpResponse , HttpRequestDef <?, S > reqDef )
445461 throws InstantiationException , IllegalAccessException {
446462
447- T response = reqDef .getResponseType ().newInstance ();
463+ S response = reqDef .getResponseType ().newInstance ();
448464 if (response instanceof SdkStreamResponse ) {
449465 if (Objects .nonNull (httpRequest .getProgressListener ())) {
450466 SimpleProgressManager progressManager = new SimpleProgressManager (
@@ -464,11 +480,11 @@ private <T> T processStreamType(HttpRequest httpRequest, HttpResponse httpRespon
464480 return response ;
465481 }
466482
467- private <ReqT , ResT > ResT extractResponse (
468- HttpRequest httpRequest , HttpResponse httpResponse , HttpRequestDef <ReqT , ResT > reqDef ) {
483+ private <R , S > S extractResponse (
484+ HttpRequest httpRequest , HttpResponse httpResponse , HttpRequestDef <R , S > reqDef ) {
469485
470486 try {
471- ResT finalResT = HttpUtils .isTextBasedContentType (httpResponse .getContentType ()) ?
487+ S finalResT = HttpUtils .isTextBasedContentType (httpResponse .getContentType ()) ?
472488 processTextBasedType (httpResponse , reqDef ) : processStreamType (httpRequest , httpResponse , reqDef );
473489
474490 reqDef .getResponseFields ().forEach (resTField -> {
@@ -492,13 +508,13 @@ private <ReqT, ResT> ResT extractResponse(
492508 }
493509
494510 @ SuppressWarnings ("unchecked" )
495- private <ResT > ResT deserializeSerializableResponse (Class <ResT > clazz , String string )
511+ private <S > S deserializeSerializableResponse (Class <S > clazz , String string )
496512 throws InstantiationException , IllegalAccessException {
497- ResT instance = clazz .newInstance ();
498- return ((SdkSerializable <ResT >) instance ).deserialize (string );
513+ S instance = clazz .newInstance ();
514+ return ((SdkSerializable <S >) instance ).deserialize (string );
499515 }
500516
501- public <ResT > Object responseToObject (String respBody , Field <ResT , ?> responseField ) {
517+ public <S > Object responseToObject (String respBody , Field <S , ?> responseField ) {
502518 Object obj ;
503519 if (responseField .getFieldType ().isAssignableFrom (List .class )) {
504520 obj = JsonUtils .toListObject (respBody , responseField .getInnerContainerType ());
@@ -510,7 +526,7 @@ public <ResT> Object responseToObject(String respBody, Field<ResT, ?> responseFi
510526 return obj ;
511527 }
512528
513- private <ResT > void fillHeaderField (HttpResponse httpResponse , ResT wrapperResponse , Field <ResT , ?> field ) {
529+ private <S > void fillHeaderField (HttpResponse httpResponse , S wrapperResponse , Field <S , ?> field ) {
514530 List <String > infos = httpResponse .getHeaders ().get (field .getName ());
515531 if (Objects .nonNull (infos ) && infos .size () > 0 ) {
516532 if (field .getFieldType ().isAssignableFrom (List .class )) {
0 commit comments