@@ -30,6 +30,7 @@ import java.net.URLEncoder;
30
30
31
31
import java.io.IOException;
32
32
import java.io.UnsupportedEncodingException;
33
+ import java.io.DataInputStream;
33
34
34
35
import java.text.DateFormat;
35
36
import java.text.SimpleDateFormat;
@@ -385,21 +386,12 @@ public class ApiClient {
385
386
}
386
387
}
387
388
388
- /**
389
- * Invoke API by sending HTTP request with the given options.
390
- *
391
- * @param path The sub-path of the HTTP URL
392
- * @param method The request method, one of "GET", "POST", "PUT", and "DELETE"
393
- * @param queryParams The query parameters
394
- * @param body The request body object
395
- * @param headerParams The header parameters
396
- * @param formParams The form parameters
397
- * @param accept The request's Accept header
398
- * @param contentType The request's Content-Type header
399
- * @param authNames The authentications to apply
400
- * @return The response body in type of string
401
- */
402
- public String invokeAPI(String path, String method, List<Pair > queryParams, Object body, Map<String , String > headerParams, Map<String , String > formParams, String accept, String contentType, String[] authNames) throws ApiException {
389
+ private ClientResponse getAPIResponse(String path, String method, List<Pair > queryParams, Object body, byte[] binaryBody, Map<String , String > headerParams, Map<String , String > formParams, String accept, String contentType, String[] authNames) throws ApiException {
390
+
391
+ if (body != null && binaryBody != null){
392
+ throw new ApiException(500, " either body or binaryBody must be null" );
393
+ }
394
+
403
395
updateParamsForAuth(authNames, queryParams, headerParams);
404
396
405
397
Client client = getClient();
@@ -446,7 +438,10 @@ public class ApiClient {
446
438
response = builder.type(contentType).post(ClientResponse.class,
447
439
encodedFormParams);
448
440
} else if (body == null) {
449
- 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);
450
445
} else if(body instanceof FormDataMultiPart) {
451
446
response = builder.type(contentType).post(ClientResponse.class, body);
452
447
}
@@ -460,7 +455,10 @@ public class ApiClient {
460
455
response = builder.type(contentType).put(ClientResponse.class,
461
456
encodedFormParams);
462
457
} else if(body == null) {
463
- response = builder.put(ClientResponse.class, serialize(body));
458
+ if (binaryBody == null)
459
+ response = builder.put(ClientResponse.class, null);
460
+ else
461
+ response = builder.type(contentType).put(ClientResponse.class, binaryBody);
464
462
} else {
465
463
response = builder.type(contentType).put(ClientResponse.class, serialize(body));
466
464
}
@@ -472,14 +470,38 @@ public class ApiClient {
472
470
response = builder.type(contentType).delete(ClientResponse.class,
473
471
encodedFormParams);
474
472
} else if(body == null) {
475
- response = builder.delete(ClientResponse.class);
473
+ if (binaryBody == null)
474
+ response = builder.delete(ClientResponse.class);
475
+ else
476
+ response = builder.type(contentType).delete(ClientResponse.class, binaryBody);
476
477
} else {
477
478
response = builder.type(contentType).delete(ClientResponse.class, serialize(body));
478
479
}
479
480
}
480
481
else {
481
482
throw new ApiException(500, " unknown method type " + method);
482
483
}
484
+ return response;
485
+ }
486
+
487
+ /**
488
+ * Invoke API by sending HTTP request with the given options.
489
+ *
490
+ * @param path The sub-path of the HTTP URL
491
+ * @param method The request method, one of "GET", "POST", "PUT", and "DELETE"
492
+ * @param queryParams The query parameters
493
+ * @param body The request body object - if it is not binary, otherwise null
494
+ * @param binaryBody The request body object - if it is binary, otherwise null
495
+ * @param headerParams The header parameters
496
+ * @param formParams The form parameters
497
+ * @param accept The request's Accept header
498
+ * @param contentType The request's Content-Type header
499
+ * @param authNames The authentications to apply
500
+ * @return The response body in type of string
501
+ */
502
+ public String invokeAPI(String path, String method, List<Pair > queryParams, Object body, byte[] binaryBody, Map<String , String > headerParams, Map<String , String > formParams, String accept, String contentType, String[] authNames) throws ApiException {
503
+
504
+ ClientResponse response = getAPIResponse(path, method, queryParams, body, binaryBody, headerParams, formParams, accept, contentType, authNames);
483
505
484
506
if (response.getStatusInfo() == ClientResponse.Status.NO_CONTENT) {
485
507
return null;
@@ -511,6 +533,58 @@ public class ApiClient {
511
533
respBody);
512
534
}
513
535
}
536
+ /**
537
+ * Invoke API by sending HTTP request with the given options - return binary result
538
+ *
539
+ * @param path The sub-path of the HTTP URL
540
+ * @param method The request method, one of "GET", "POST", "PUT", and "DELETE"
541
+ * @param queryParams The query parameters
542
+ * @param body The request body object - if it is not binary, otherwise null
543
+ * @param binaryBody The request body object - if it is binary, otherwise null
544
+ * @param headerParams The header parameters
545
+ * @param formParams The form parameters
546
+ * @param accept The request's Accept header
547
+ * @param contentType The request's Content-Type header
548
+ * @param authNames The authentications to apply
549
+ * @return The response body in type of string
550
+ */
551
+ public byte[] invokeBinaryAPI(String path, String method, List<Pair > queryParams, Object body, byte[] binaryBody, Map<String , String > headerParams, Map<String , String > formParams, String accept, String contentType, String[]authNames) throws ApiException {
552
+
553
+ ClientResponse response = getAPIResponse(path, method, queryParams, body, binaryBody, headerParams, formParams, accept, contentType, authNames);
554
+
555
+ if (response.getStatusInfo() == ClientResponse.Status.NO_CONTENT) {
556
+ return null;
557
+ }
558
+ else if(response.getStatusInfo().getFamily() == Family.SUCCESSFUL) {
559
+ if (response.hasEntity()) {
560
+ DataInputStream stream = new DataInputStream(response.getEntityInputStream());
561
+ byte[] data = new byte[response.getLength()];
562
+ try {
563
+ stream.readFully(data);
564
+ } catch (IOException ex) {
565
+ throw new ApiException(500, " Error obtaining binary response data" );
566
+ }
567
+ return data;
568
+ }
569
+ else {
570
+ return new byte[0];
571
+ }
572
+ }
573
+ else {
574
+ String message = " error" ;
575
+ if (response.hasEntity()) {
576
+ try{
577
+ message = String.valueOf(response.getEntity(String.class));
578
+ }
579
+ catch (RuntimeException e) {
580
+ // e.printStackTrace();
581
+ }
582
+ }
583
+ throw new ApiException(
584
+ response.getStatusInfo().getStatusCode(),
585
+ message);
586
+ }
587
+ }
514
588
515
589
/**
516
590
* Update query and header parameters based on authentication settings.
0 commit comments