-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathApiClient.java
More file actions
971 lines (889 loc) · 33.9 KB
/
ApiClient.java
File metadata and controls
971 lines (889 loc) · 33.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
package com.aspose.barcode.cloud;
import com.aspose.barcode.cloud.model.ApiErrorResponse;
import okhttp3.Call;
import okhttp3.FormBody;
import okhttp3.Headers;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.internal.http.HttpMethod;
import okhttp3.logging.HttpLoggingInterceptor;
import okhttp3.logging.HttpLoggingInterceptor.Level;
import okio.BufferedSink;
import okio.Okio;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Type;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.time.LocalDate;
import java.time.OffsetDateTime;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/** ApiClient. */
public class ApiClient {
public final String apiVersion = "v4.0";
public final String clientVersion = "26.3.2";
private String baseUrl = "https://api.aspose.cloud";
private String tokenUrl = "https://id.aspose.cloud/connect/token";
private String clientId;
private String clientSecret;
private boolean debugging = false;
private final Map<String, String> defaultHeaderMap = new HashMap<>();
private String tempFolderPath = null;
private OkHttpClient httpClient;
private final JSON json;
private HttpLoggingInterceptor loggingInterceptor;
private String accessToken;
/** Constructor for ApiClient with clientId and clientSecret */
public ApiClient(String clientId, String clientSecret) {
this(clientId, clientSecret, null, null);
}
/** Constructor for ApiClient with accessToken */
public ApiClient(String accessToken) {
this();
this.setAccessToken(accessToken);
}
/** Constructor for ApiClient with clientId, clientSecret, baseUrl and tokenUrl */
public ApiClient(String clientId, String clientSecret, String baseUrl, String tokenUrl) {
this();
this.clientId = clientId;
this.clientSecret = clientSecret;
if (baseUrl != null) {
this.baseUrl = baseUrl;
}
if (tokenUrl != null) {
this.tokenUrl = tokenUrl;
}
}
/** Constructor for ApiClient with readTimeout */
protected ApiClient(long readTimeoutMillis) {
httpClient =
new OkHttpClient.Builder()
.readTimeout(readTimeoutMillis, TimeUnit.MILLISECONDS)
.build();
json = new JSON();
// Set default User-Agent.
setUserAgent("OpenApi-Generator/26.3.2/java");
addDefaultHeader("x-aspose-client", "java sdk");
addDefaultHeader("x-aspose-client-version", clientVersion);
setReadTimeout(60_000);
}
protected ApiClient() {
this(60_000);
}
/**
* Get base path.
*
* @return Base path
*/
public String getBasePath() {
return baseUrl + "/" + apiVersion;
}
/**
* Get HTTP client.
*
* @return An instance of OkHttpClient
*/
public OkHttpClient getHttpClient() {
return httpClient;
}
/**
* Helper method to set access token for the first OAuth2 authentication.
*
* @param accessToken Access token
*/
protected void setAccessToken(String accessToken) {
if (accessToken == null) {
throw new RuntimeException("accessToken can not be null");
}
this.accessToken = accessToken;
}
/**
* Set the User-Agent header's value (by adding to the default header map).
*
* @param userAgent HTTP request's user agent
*/
public void setUserAgent(String userAgent) {
addDefaultHeader("User-Agent", userAgent);
}
/**
* Add a default header.
*
* @param key The header's key
* @param value The header's value
*/
public void addDefaultHeader(String key, String value) {
defaultHeaderMap.put(key, value);
}
/**
* Check that whether debugging is enabled for this API client.
*
* @return True if debugging is enabled, false otherwise.
*/
public boolean isDebugging() {
return debugging;
}
/**
* Enable/disable debugging for this API client.
*
* @param debugging To enable (true) or disable (false) debugging
*/
public void setDebugging(boolean debugging) {
if (debugging != this.debugging) {
if (debugging) {
loggingInterceptor = new HttpLoggingInterceptor();
loggingInterceptor.setLevel(Level.BODY);
httpClient.interceptors().add(loggingInterceptor);
} else {
httpClient.interceptors().remove(loggingInterceptor);
loggingInterceptor = null;
}
}
this.debugging = debugging;
}
/**
* The path of temporary folder used to store downloaded files from endpoints with file
* response. The default value is <code>null</code>, i.e. using the system's default temporary
* folder.
*
* @see <a
* href="https://docs.oracle.com/javase/7/docs/api/java/io/File.html#createTempFile">createTempFile</a>
* @return Temporary folder path
*/
public String getTempFolderPath() {
return tempFolderPath;
}
/**
* Set the temporary folder path (for downloading files)
*
* @param tempFolderPath Temporary folder path
*/
public void setTempFolderPath(String tempFolderPath) {
this.tempFolderPath = tempFolderPath;
}
/**
* Get connection timeout (in milliseconds).
*
* @return Timeout in milliseconds
*/
public int getConnectTimeout() {
return httpClient.connectTimeoutMillis();
}
/**
* Get read timeout (in milliseconds).
*
* @return Timeout in milliseconds
*/
public int getReadTimeout() {
return httpClient.readTimeoutMillis();
}
/**
* Sets the read timeout (in milliseconds). A value of 0 means no timeout, otherwise values must
* be between 1 and {@link Integer#MAX_VALUE}.
*
* @param readTimeout read timeout in milliseconds
*/
public void setReadTimeout(int readTimeout) {
httpClient =
new OkHttpClient.Builder().readTimeout(readTimeout, TimeUnit.MILLISECONDS).build();
}
/**
* Get write timeout (in milliseconds).
*
* @return Timeout in milliseconds
*/
public int getWriteTimeout() {
return httpClient.writeTimeoutMillis();
}
/**
* Format the given parameter object into string.
*
* @param param Parameter
* @return String representation of the parameter
*/
public String parameterToString(Object param) {
if (param == null) {
return "";
} else if (param instanceof Date
|| param instanceof OffsetDateTime
|| param instanceof LocalDate) {
// Serialize to json string and remove the " enclosing characters
String jsonStr = json.serialize(param);
return jsonStr.substring(1, jsonStr.length() - 1);
} else if (param instanceof Collection) {
StringBuilder b = new StringBuilder();
for (Object o : (Collection<Object>) param) {
if (b.length() > 0) {
b.append(",");
}
b.append(o);
}
return b.toString();
} else {
return String.valueOf(param);
}
}
/**
* Formats the specified query parameter to a list containing a single {@code Pair} object.
*
* <p>Note that {@code value} must not be a collection.
*
* @param name The name of the parameter.
* @param value The value of the parameter.
* @return A list containing a single {@code Pair} object.
*/
public List<Pair> parameterToPair(String name, Object value) {
List<Pair> params = new ArrayList<Pair>();
// preconditions
if (name == null || name.isEmpty() || value == null || value instanceof Collection) {
return params;
}
params.add(new Pair(name, parameterToString(value)));
return params;
}
/**
* Formats the specified collection query parameters to a list of {@code Pair} objects.
*
* <p>Note that the values of each of the returned Pair objects are percent-encoded.
*
* @param collectionFormat The collection format of the parameter.
* @param name The name of the parameter.
* @param value The value of the parameter.
* @return A list of {@code Pair} objects.
*/
public List<Pair> parameterToPairs(String collectionFormat, String name, Collection value) {
List<Pair> params = new ArrayList<Pair>();
// preconditions
if (name == null || name.isEmpty() || value == null || value.isEmpty()) {
return params;
}
// create the params based on the collection format
if ("multi".equals(collectionFormat)) {
for (Object item : value) {
params.add(new Pair(name, escapeString(parameterToString(item))));
}
return params;
}
// collectionFormat is assumed to be "csv" by default
String delimiter = ",";
// escape all delimiters except commas, which are URI reserved
// characters
if ("ssv".equals(collectionFormat)) {
delimiter = escapeString(" ");
} else if ("tsv".equals(collectionFormat)) {
delimiter = escapeString("\t");
} else if ("pipes".equals(collectionFormat)) {
delimiter = escapeString("|");
}
StringBuilder sb = new StringBuilder();
for (Object item : value) {
sb.append(delimiter);
sb.append(escapeString(parameterToString(item)));
}
params.add(new Pair(name, sb.substring(delimiter.length())));
return params;
}
/**
* Sanitize filename by removing path. e.g. ../../sun.gif becomes sun.gif
*
* @param filename The filename to be sanitized
* @return The sanitized filename
*/
public String sanitizeFilename(String filename) {
return filename.replaceAll(".*[/\\\\]", "");
}
/**
* Check if the given MIME is a JSON MIME. JSON MIME examples: application/json
* application/json; charset=UTF8 APPLICATION/JSON application/vnd.company+json "* / *" is also
* default to JSON
*
* @param mime MIME (Multipurpose Internet Mail Extensions)
* @return True if the given MIME is JSON, false otherwise.
*/
public boolean isJsonMime(String mime) {
String jsonMime = "(?i)^(application/json|[^;/ \t]+/[^;/ \t]+[+]json)[ \t]*(;.*)?$";
return mime != null && (mime.matches(jsonMime) || mime.equals("*/*"));
}
/**
* Select the Accept header's value from the given accepts array: if JSON exists in the given
* array, use it; otherwise use all of them (joining into a string)
*
* @param accepts The accepts array to select from
* @return The Accept header to use. If the given array is empty, null will be returned (not to
* set the Accept header explicitly).
*/
public String selectHeaderAccept(String[] accepts) {
if (accepts.length == 0) {
return null;
}
for (String accept : accepts) {
if (isJsonMime(accept)) {
return accept;
}
}
return String.join(",", accepts);
}
/**
* Select the Content-Type header's value from the given array: if JSON exists in the given
* array, use it; otherwise use the first one of the array.
*
* @param contentTypes The Content-Type array to select from
* @return The Content-Type header to use. If the given array is empty, or matches "any", JSON
* will be used.
*/
public String selectHeaderContentType(String[] contentTypes) {
if (contentTypes.length == 0 || contentTypes[0].equals("*/*")) {
return "application/json";
}
for (String contentType : contentTypes) {
if (isJsonMime(contentType)) {
return contentType;
}
}
return contentTypes[0];
}
/**
* Escape the given string to be used as URL query value.
*
* @param str String to be escaped
* @return Escaped string
*/
public String escapeString(String str) {
try {
return URLEncoder.encode(str, "utf8").replaceAll("\\+", "%20");
} catch (UnsupportedEncodingException e) {
return str;
}
}
/**
* Deserialize response body to Java object, according to the return type and the Content-Type
* response header.
*
* @param response HTTP response
* @param returnType The type of the Java object
* @return The deserialized Java object
* @throws ApiException If fail to deserialize response body, i.e. cannot read response body or
* the Content-Type of the response is not supported.
*/
@SuppressWarnings("unchecked")
public <T> T deserialize(Response response, Type returnType) throws ApiException {
if (response == null || returnType == null) {
return null;
}
if ("byte[]".equals(returnType.toString())) {
// Handle binary response (byte array).
try {
return (T) response.body().bytes();
} catch (IOException e) {
throw new ApiException(e);
}
} else if (returnType.equals(File.class)) {
// Handle file downloading.
return (T) downloadFileFromResponse(response);
}
String respBody;
try {
if (response.body() != null) {
respBody = response.body().string();
} else {
respBody = null;
}
} catch (IOException e) {
throw new ApiException(e);
}
if (respBody == null || respBody.isEmpty()) {
return null;
}
String contentType = response.headers().get("Content-Type");
if (contentType == null) {
// ensuring a default content type
contentType = "application/json";
}
if (isJsonMime(contentType)) {
return json.deserialize(respBody, returnType);
} else if (returnType.equals(String.class)) {
// Expecting string, return the raw response body.
return (T) respBody;
} else {
throw new ApiException(
"Content type \"" + contentType + "\" is not supported for type: " + returnType,
response.code(),
response.headers().toMultimap(),
respBody);
}
}
/**
* Serialize the given Java object into request body according to the object's class and the
* request Content-Type.
*
* @param obj The Java object
* @param contentType The request Content-Type
* @return The serialized request body
* @throws ApiException If fail to serialize the given object
*/
public RequestBody serialize(Object obj, String contentType) throws ApiException {
if (obj instanceof byte[]) {
// Binary (byte array) body parameter support.
return RequestBody.create((byte[]) obj, MediaType.parse(contentType));
} else if (obj instanceof File) {
// File body parameter support.
return RequestBody.create((File) obj, MediaType.parse(contentType));
} else if (isJsonMime(contentType)) {
String content;
if (obj != null) {
content = json.serialize(obj);
} else {
content = null;
}
return RequestBody.create(content, MediaType.parse(contentType));
} else {
throw new ApiException("Content type \"" + contentType + "\" is not supported");
}
}
/**
* Download file from the given response.
*
* @param response An instance of the Response object
* @return Downloaded file
* @throws ApiException If fail to read file content from response and write to disk
*/
public File downloadFileFromResponse(Response response) throws ApiException {
try {
File file = prepareDownloadFile(response);
BufferedSink sink = Okio.buffer(Okio.sink(file));
sink.writeAll(response.body().source());
sink.close();
return file;
} catch (IOException e) {
throw new ApiException(e);
}
}
/**
* Prepare file for download.
*
* @param response An instance of the Response object
* @return Prepared file for the download
* @throws IOException If fail to prepare file for download
*/
public File prepareDownloadFile(Response response) throws IOException {
String filename = null;
String contentDisposition = response.header("Content-Disposition");
if (contentDisposition != null && !contentDisposition.isEmpty()) {
// Get filename from the Content-Disposition header.
Pattern pattern = Pattern.compile("filename=['\"]?([^'\"\\s]+)['\"]?");
Matcher matcher = pattern.matcher(contentDisposition);
if (matcher.find()) {
filename = sanitizeFilename(matcher.group(1));
}
}
String prefix = null;
String suffix = null;
if (filename == null) {
prefix = "download-";
suffix = "";
} else {
int pos = filename.lastIndexOf(".");
if (pos == -1) {
prefix = filename + "-";
} else {
prefix = filename.substring(0, pos) + "-";
suffix = filename.substring(pos);
}
// File.createTempFile requires the prefix to be at least three characters long
if (prefix.length() < 3) {
prefix = "download-";
}
}
if (tempFolderPath == null) {
return File.createTempFile(prefix, suffix);
} else {
return File.createTempFile(prefix, suffix, new File(tempFolderPath));
}
}
/**
* Override {@link #execute(Call, Type)}.
*
* @param call An instance of the Call object
* @return ApiResponse<T>
* @throws ApiException If fail to execute the call
*/
public <T> ApiResponse<T> execute(Call call) throws ApiException {
return execute(call, null);
}
/**
* Execute HTTP call and deserialize the HTTP response body into the given return type.
*
* @param returnType The return type used to deserialize HTTP response body
* @param call Call
* @return ApiResponse object containing response status, headers and data, which is a Java
* object deserialized from response body and would be null when returnType is null.
* @throws ApiException If fail to execute the call
*/
public <T> ApiResponse<T> execute(Call call, Type returnType) throws ApiException {
try {
Response response = call.execute();
T data = handleResponse(response, returnType);
return new ApiResponse<T>(response.code(), response.headers().toMultimap(), data);
} catch (IOException e) {
throw new ApiException(e);
}
}
/**
* Override {@link #executeAsync(Call, Type, ApiCallback)}
*
* @param call An instance of the Call object
* @param callback ApiCallback<T>
*/
public <T> void executeAsync(Call call, ApiCallback<T> callback) {
executeAsync(call, null, callback);
}
/**
* Execute HTTP call asynchronously.
*
* @see #execute(Call, Type)
* @param call The callback to be executed when the API call finishes
* @param returnType Return type
* @param callback ApiCallback
*/
public <T> void executeAsync(Call call, final Type returnType, final ApiCallback<T> callback) {
call.enqueue(
new okhttp3.Callback() {
@Override
public void onFailure(Call call, IOException e) {
callback.onFailure(new ApiException(e), 0, null);
}
@Override
public void onResponse(Call call, Response response) {
T result;
try {
result = handleResponse(response, returnType);
} catch (ApiException e) {
callback.onFailure(e, response.code(), response.headers().toMultimap());
return;
}
callback.onSuccess(
result, response.code(), response.headers().toMultimap());
}
});
}
/**
* Handle the given response, return the deserialized object when the response is successful.
*
* @param response Response
* @param returnType Return type
* @return Type
* @throws ApiException If the response has an unsuccessful status code or fail to deserialize
* the response body
*/
public <T> T handleResponse(Response response, Type returnType) throws ApiException {
if (response.isSuccessful()) {
if (returnType == null || response.code() == 204) {
// returning null if the returnType is not defined,
// or the status code is 204 (No Content)
if (response.body() != null) {
try {
response.body().close();
} catch (Exception e) {
throw new ApiException(
response.message(),
e,
response.code(),
response.headers().toMultimap());
}
}
return null;
}
return deserialize(response, returnType);
}
if (response.body() == null) {
throw new ApiException(response.message(), response.code());
}
ApiErrorResponse errorResponse;
try {
errorResponse = deserialize(response, ApiErrorResponse.class);
} catch (Exception e) {
throw new ApiException(
response.message(), e, response.code(), response.headers().toMultimap());
}
throw new ApiException(response.message(), response.code(), errorResponse);
}
/**
* Build HTTP call with the given options.
*
* @param path The sub-path of the HTTP URL
* @param method The request method, one of "GET", "HEAD", "OPTIONS", "POST", "PUT", "PATCH" and
* "DELETE"
* @param queryParams The query parameters
* @param collectionQueryParams The collection query parameters
* @param body The request body object
* @param headerParams The header parameters
* @param formParams The form parameters
* @param progressRequestListener Progress request listener
* @return The HTTP call
* @throws ApiException If fail to serialize the request body object
*/
public Call buildCall(
String path,
String method,
List<Pair> queryParams,
List<Pair> collectionQueryParams,
Object body,
Map<String, String> headerParams,
Map<String, Object> formParams,
ProgressRequestBody.ProgressRequestListener progressRequestListener)
throws ApiException {
Request request =
buildRequest(
path,
method,
queryParams,
collectionQueryParams,
body,
headerParams,
formParams,
progressRequestListener);
return httpClient.newCall(request);
}
/**
* Build an HTTP request with the given options.
*
* @param path The sub-path of the HTTP URL
* @param method The request method, one of "GET", "HEAD", "OPTIONS", "POST", "PUT", "PATCH" and
* "DELETE"
* @param queryParams The query parameters
* @param collectionQueryParams The collection query parameters
* @param body The request body object
* @param headerParams The header parameters
* @param formParams The form parameters
* @param progressRequestListener Progress request listener
* @return The HTTP request
* @throws ApiException If fail to serialize the request body object
*/
public Request buildRequest(
String path,
String method,
List<Pair> queryParams,
List<Pair> collectionQueryParams,
Object body,
Map<String, String> headerParams,
Map<String, Object> formParams,
ProgressRequestBody.ProgressRequestListener progressRequestListener)
throws ApiException {
addOAuthAuthentication(headerParams);
final String url = buildUrl(path, queryParams, collectionQueryParams);
final Request.Builder reqBuilder = new Request.Builder().url(url);
processHeaderParams(headerParams, reqBuilder);
String contentType = headerParams.get("Content-Type");
// ensuring a default content type
if (contentType == null) {
contentType = "application/json";
}
RequestBody reqBody;
if (!HttpMethod.permitsRequestBody(method)) {
reqBody = null;
} else if ("multipart/form-data".equals(contentType)) {
reqBody = buildRequestBodyMultipart(formParams);
} else if (body == null) {
if ("DELETE".equals(method)) {
// allow calling DELETE without sending a request body
reqBody = null;
} else {
// use an empty request body (for POST, PUT and PATCH)
reqBody = RequestBody.create("", MediaType.parse(contentType));
}
} else {
reqBody = serialize(body, contentType);
}
Request request = null;
if (progressRequestListener != null && reqBody != null) {
ProgressRequestBody progressRequestBody =
new ProgressRequestBody(reqBody, progressRequestListener);
request = reqBuilder.method(method, progressRequestBody).build();
} else {
request = reqBuilder.method(method, reqBody).build();
}
return request;
}
/**
* Build full URL by concatenating base path, the given sub path and query parameters.
*
* @param path The sub path
* @param queryParams The query parameters
* @param collectionQueryParams The collection query parameters
* @return The full URL
*/
public String buildUrl(String path, List<Pair> queryParams, List<Pair> collectionQueryParams) {
final StringBuilder url = new StringBuilder();
url.append(getBasePath()).append(path);
if (queryParams != null && !queryParams.isEmpty()) {
// support (constant) query string in `path`, e.g. "/posts?draft=1"
String prefix = path.contains("?") ? "&" : "?";
for (Pair param : queryParams) {
if (param.getValue() != null) {
if (prefix != null) {
url.append(prefix);
prefix = null;
} else {
url.append("&");
}
String value = parameterToString(param.getValue());
url.append(escapeString(param.getKey()))
.append("=")
.append(escapeString(value));
}
}
}
if (collectionQueryParams != null && !collectionQueryParams.isEmpty()) {
String prefix = url.toString().contains("?") ? "&" : "?";
for (Pair param : collectionQueryParams) {
if (param.getValue() != null) {
if (prefix != null) {
url.append(prefix);
prefix = null;
} else {
url.append("&");
}
String value = parameterToString(param.getValue());
// collection query parameter value already escaped as part of parameterToPairs
url.append(escapeString(param.getKey())).append("=").append(value);
}
}
}
return url.toString();
}
/**
* Set header parameters to the request builder, including default headers.
*
* @param headerParams Header parameters in the form of Map
* @param reqBuilder Request.Builder
*/
public void processHeaderParams(Map<String, String> headerParams, Request.Builder reqBuilder) {
for (Entry<String, String> param : headerParams.entrySet()) {
reqBuilder.header(param.getKey(), parameterToString(param.getValue()));
}
for (Entry<String, String> header : defaultHeaderMap.entrySet()) {
if (!headerParams.containsKey(header.getKey())) {
reqBuilder.header(header.getKey(), parameterToString(header.getValue()));
}
}
}
/**
* Build a multipart (file uploading) request body with the given form parameters, which could
* contain text fields and file fields.
*
* @param formParams Form parameters in the form of Map
* @return RequestBody
*/
public RequestBody buildRequestBodyMultipart(Map<String, Object> formParams) {
MultipartBody.Builder mpBuilder = new MultipartBody.Builder().setType(MultipartBody.FORM);
for (Entry<String, Object> param : formParams.entrySet()) {
Object paramValue = param.getValue();
if (paramValue instanceof File) {
File file = (File) paramValue;
Headers partHeaders =
Headers.of(
"Content-Disposition",
"form-data; name=\""
+ param.getKey()
+ "\"; filename=\""
+ file.getName()
+ "\"");
MediaType mediaType = MediaType.parse(guessContentTypeFromFile(file));
mpBuilder.addPart(partHeaders, RequestBody.create(file, mediaType));
} else if (paramValue instanceof Collection) {
Collection<Object> collection = (Collection<Object>) paramValue;
for (Object item : collection) {
Headers partHeaders =
Headers.of(
"Content-Disposition",
"form-data; name=\"" + param.getKey() + "\"");
mpBuilder.addPart(
partHeaders, RequestBody.create(parameterToString(item), null));
}
} else {
Headers partHeaders =
Headers.of(
"Content-Disposition",
"form-data; name=\"" + param.getKey() + "\"");
mpBuilder.addPart(
partHeaders, RequestBody.create(null, parameterToString(paramValue)));
}
}
return mpBuilder.build();
}
/**
* Guess Content-Type header from the given file (defaults to "application/octet-stream").
*
* @param file The given file
* @return The guessed Content-Type
*/
public String guessContentTypeFromFile(File file) {
String contentType = URLConnection.guessContentTypeFromName(file.getName());
if (contentType == null) {
return "application/octet-stream";
} else {
return contentType;
}
}
/**
* Request OAuth token.
*
* @throws ApiException If authorization is failed
*/
public void requestToken() throws ApiException {
try {
RequestBody requestBody =
new FormBody.Builder()
.addEncoded("grant_type", "client_credentials")
.addEncoded("client_id", clientId)
.addEncoded("client_secret", clientSecret)
.build();
Request request =
new Request.Builder()
.url(tokenUrl)
.post(requestBody)
.addHeader("Content-Type", " application/x-www-form-urlencoded")
.build();
Response response = httpClient.newCall(request).execute();
String responseBody = response.body().string();
if (response.isSuccessful()) {
GetAccessTokenResult result =
json.deserialize(responseBody, GetAccessTokenResult.class);
setAccessToken(result.access_token);
} else {
throw new ApiException(responseBody);
}
} catch (Exception ex) {
throw new ApiException(ex);
}
}
/**
* Add OAuth2 header
*
* @param headerParams Map of request headers
*/
private void addOAuthAuthentication(Map<String, String> headerParams) throws ApiException {
if (accessToken == null) {
requestToken();
}
headerParams.put("Authorization", "Bearer " + accessToken);
}
/** GetAccessTokenResult class for deserialization. */
@SuppressWarnings({"unused", "InnerClassMayBeStatic"})
private class GetAccessTokenResult {
public String access_token;
}
}