@@ -28,6 +28,7 @@ import java.net.URLEncoder;
28
28
import java.io.IOException;
29
29
import java.io.File;
30
30
import java.io.UnsupportedEncodingException;
31
+ import java.io.DataInputStream;
31
32
32
33
import java.text.DateFormat;
33
34
import java.text.SimpleDateFormat;
@@ -372,22 +373,12 @@ public class ApiClient {
372
373
}
373
374
}
374
375
375
- /**
376
- * Invoke API by sending HTTP request with the given options.
377
- *
378
- * @param path The sub-path of the HTTP URL
379
- * @param method The request method, one of "GET", "POST", "PUT", and "DELETE"
380
- * @param queryParams The query parameters
381
- * @param body The request body object
382
- * @param headerParams The header parameters
383
- * @param formParams The form parameters
384
- * @param accept The request's Accept header
385
- * @param contentType The request's Content-Type header
386
- * @param authNames The authentications to apply
387
- * @param returnType The return type into which to deserialize the response
388
- * @return The response body in type of string
389
- */
390
- public <T > T invokeAPI(String path, String method, List<Pair > queryParams, Object body, Map<String , String > headerParams, Map<String , Object > formParams, String accept, String contentType, String[] authNames, TypeRef returnType) throws ApiException {
376
+ private ClientResponse getAPIResponse(String path, String method, List<Pair > queryParams, Object body, byte[] binaryBody, Map<String , String > headerParams, Map<String , Object > formParams, String accept, String contentType, String[] authNames) throws ApiException {
377
+
378
+ if (body != null && binaryBody != null){
379
+ throw new ApiException(500, " either body or binaryBody must be null" );
380
+ }
381
+
391
382
updateParamsForAuth(authNames, queryParams, headerParams);
392
383
393
384
Client client = getClient();
@@ -447,7 +438,10 @@ public class ApiClient {
447
438
if (encodedFormParams != null) {
448
439
response = builder.type(contentType).post(ClientResponse.class, encodedFormParams);
449
440
} else if (body == null) {
450
- response = builder.post(ClientResponse.class, null);
441
+ if (binaryBody == null)
442
+ response = builder.post(ClientResponse.class, null);
443
+ else
444
+ response = builder.type(contentType).post(ClientResponse.class, binaryBody);
451
445
} else if (body instanceof FormDataMultiPart) {
452
446
response = builder.type(contentType).post(ClientResponse.class, body);
453
447
} else {
@@ -457,23 +451,50 @@ public class ApiClient {
457
451
if (encodedFormParams != null) {
458
452
response = builder.type(contentType).put(ClientResponse.class, encodedFormParams);
459
453
} else if(body == null) {
460
- response = builder.put(ClientResponse.class, serialize(body, contentType));
454
+ if (binaryBody == null)
455
+ response = builder.put(ClientResponse.class, null);
456
+ else
457
+ response = builder.type(contentType).put(ClientResponse.class, binaryBody);
461
458
} else {
462
459
response = builder.type(contentType).put(ClientResponse.class, serialize(body, contentType));
463
460
}
464
461
} else if ("DELETE".equals(method)) {
465
462
if (encodedFormParams != null) {
466
463
response = builder.type(contentType).delete(ClientResponse.class, encodedFormParams);
467
464
} else if(body == null) {
468
- response = builder.delete(ClientResponse.class);
465
+ if (binaryBody == null)
466
+ response = builder.delete(ClientResponse.class);
467
+ else
468
+ response = builder.type(contentType).delete(ClientResponse.class, binaryBody);
469
469
} else {
470
470
response = builder.type(contentType).delete(ClientResponse.class, serialize(body, contentType));
471
471
}
472
472
} else {
473
473
throw new ApiException(500, " unknown method type " + method);
474
474
}
475
+ return response;
476
+ }
477
+
478
+ /**
479
+ * Invoke API by sending HTTP request with the given options.
480
+ *
481
+ * @param path The sub-path of the HTTP URL
482
+ * @param method The request method, one of "GET", "POST", "PUT", and "DELETE"
483
+ * @param queryParams The query parameters
484
+ * @param body The request body object - if it is not binary, otherwise null
485
+ * @param binaryBody The request body object - if it is binary, otherwise null
486
+ * @param headerParams The header parameters
487
+ * @param formParams The form parameters
488
+ * @param accept The request's Accept header
489
+ * @param contentType The request's Content-Type header
490
+ * @param authNames The authentications to apply
491
+ * @return The response body in type of string
492
+ */
493
+ public <T > T invokeAPI(String path, String method, List<Pair > queryParams, Object body, byte[] binaryBody, Map<String , String > headerParams, Map<String , Object > formParams, String accept, String contentType, String[] authNames, TypeRef returnType) throws ApiException {
475
494
476
- if (response.getStatusInfo() == ClientResponse.Status.NO_CONTENT) {
495
+ ClientResponse response = getAPIResponse(path, method, queryParams, body, binaryBody, headerParams, formParams, accept, contentType, authNames);
496
+
497
+ if (response.getStatusInfo() == ClientResponse.Status.NO_CONTENT) {
477
498
return null;
478
499
} else if (response.getStatusInfo().getFamily() == Family.SUCCESSFUL) {
479
500
if (returnType == null)
@@ -498,6 +519,58 @@ public class ApiClient {
498
519
respBody);
499
520
}
500
521
}
522
+ /**
523
+ * Invoke API by sending HTTP request with the given options - return binary result
524
+ *
525
+ * @param path The sub-path of the HTTP URL
526
+ * @param method The request method, one of "GET", "POST", "PUT", and "DELETE"
527
+ * @param queryParams The query parameters
528
+ * @param body The request body object - if it is not binary, otherwise null
529
+ * @param binaryBody The request body object - if it is binary, otherwise null
530
+ * @param headerParams The header parameters
531
+ * @param formParams The form parameters
532
+ * @param accept The request's Accept header
533
+ * @param contentType The request's Content-Type header
534
+ * @param authNames The authentications to apply
535
+ * @return The response body in type of string
536
+ */
537
+ public byte[] invokeBinaryAPI(String path, String method, List<Pair > queryParams, Object body, byte[] binaryBody, Map<String , String > headerParams, Map<String , Object > formParams, String accept, String contentType, String[]authNames) throws ApiException {
538
+
539
+ ClientResponse response = getAPIResponse(path, method, queryParams, body, binaryBody, headerParams, formParams, accept, contentType, authNames);
540
+
541
+ if (response.getStatusInfo() == ClientResponse.Status.NO_CONTENT) {
542
+ return null;
543
+ }
544
+ else if(response.getStatusInfo().getFamily() == Family.SUCCESSFUL) {
545
+ if (response.hasEntity()) {
546
+ DataInputStream stream = new DataInputStream(response.getEntityInputStream());
547
+ byte[] data = new byte[response.getLength()];
548
+ try {
549
+ stream.readFully(data);
550
+ } catch (IOException ex) {
551
+ throw new ApiException(500, " Error obtaining binary response data" );
552
+ }
553
+ return data;
554
+ }
555
+ else {
556
+ return new byte[0];
557
+ }
558
+ }
559
+ else {
560
+ String message = " error" ;
561
+ if (response.hasEntity()) {
562
+ try{
563
+ message = String.valueOf(response.getEntity(String.class));
564
+ }
565
+ catch (RuntimeException e) {
566
+ // e.printStackTrace();
567
+ }
568
+ }
569
+ throw new ApiException(
570
+ response.getStatusInfo().getStatusCode(),
571
+ message);
572
+ }
573
+ }
501
574
502
575
/**
503
576
* Update query and header parameters based on authentication settings.
0 commit comments