|
27 | 27 |
|
28 | 28 | package com.aspose.words.cloud; |
29 | 29 |
|
| 30 | +import com.aspose.words.cloud.model.*; |
30 | 31 | import com.aspose.words.cloud.model.requests.*; |
31 | 32 | import com.squareup.okhttp.*; |
32 | 33 | import com.squareup.okhttp.internal.http.HttpMethod; |
@@ -58,7 +59,7 @@ public class ApiClient { |
58 | 59 | private String apiVersion = "v4.0"; |
59 | 60 | private String baseUrl = "https://api.aspose.cloud"; |
60 | 61 | private String basePath = baseUrl + "/" + apiVersion; |
61 | | - private String clientVersion = "22.9"; |
| 62 | + private String clientVersion = "22.10"; |
62 | 63 | private boolean debugging = false; |
63 | 64 | private Map<String, String> defaultHeaderMap = new HashMap<String, String>(); |
64 | 65 | private String tempFolderPath = null; |
@@ -686,27 +687,24 @@ else if (returnType.equals(String.class)) { |
686 | 687 | * class and the request Content-Type. |
687 | 688 | * |
688 | 689 | * @param obj The Java object |
689 | | - * @param contentType The request Content-Type |
690 | 690 | * @return The serialized request body |
691 | 691 | * @throws ApiException If fail to serialize the given object |
692 | 692 | */ |
693 | | - public RequestBody serialize(Object obj, String contentType) throws ApiException { |
694 | | - if (obj instanceof byte[]) { |
695 | | - // Binary (byte array) body parameter support. |
696 | | - return RequestBody.create(MediaType.parse(contentType), (byte[]) obj); |
697 | | - } |
698 | | - else if (isJsonMime(contentType)) { |
699 | | - String content; |
700 | | - if (obj != null) { |
701 | | - content = json.serialize(obj); |
702 | | - } |
703 | | - else { |
704 | | - content = null; |
705 | | - } |
706 | | - return RequestBody.create(MediaType.parse(contentType), content); |
707 | | - } |
| 693 | + public RequestBody serialize(Object obj) throws ApiException { |
| 694 | + if (obj instanceof RequestBody) { |
| 695 | + return (RequestBody) obj; |
| 696 | + } |
| 697 | + else if (obj instanceof byte[]) { |
| 698 | + return RequestBody.create(MediaType.parse("application/octet-stream"), (byte[]) obj); |
| 699 | + } |
| 700 | + else if (obj instanceof ModelIfc) { |
| 701 | + return RequestBody.create(MediaType.parse("application/json"), json.serialize(obj)); |
| 702 | + } |
| 703 | + else if (obj instanceof String) { |
| 704 | + return RequestBody.create(MediaType.parse("text/plain"), (String) obj); |
| 705 | + } |
708 | 706 | else { |
709 | | - throw new ApiException("Content type \"" + contentType + "\" is not supported"); |
| 707 | + throw new ApiException("Unsupported object type to serialize."); |
710 | 708 | } |
711 | 709 | } |
712 | 710 |
|
@@ -812,54 +810,46 @@ public Call buildCall(Request request) { |
812 | 810 | * @param method The request method, one of "GET", "HEAD", "OPTIONS", "POST", "PUT", "PATCH" and "DELETE" |
813 | 811 | * @param queryParams The query parameters |
814 | 812 | * @param collectionQueryParams The collection query parameters |
815 | | - * @param body The request body object |
816 | 813 | * @param headerParams The header parameters |
817 | 814 | * @param formParams The form parameters |
| 815 | + * @param filesContentParams The file content parameters |
818 | 816 | * @param addAuthHeaders The authentications to apply |
819 | 817 | * @param progressRequestListener Progress request listener |
820 | 818 | * @return The HTTP request |
821 | 819 | * @throws ApiException If fail to serialize the request body object |
822 | 820 | * @throws IOException If fail to serialize the request body object |
823 | 821 | */ |
824 | | - public Request buildRequest(String path, String method, List<Pair> queryParams, List<Pair> collectionQueryParams, Object body, Map<String, String> headerParams, Map<String, Object> formParams, Boolean addAuthHeaders, ProgressRequestBody.ProgressRequestListener progressRequestListener) throws ApiException, IOException { |
| 822 | + public Request buildRequest(String path, String method, List<Pair> queryParams, List<Pair> collectionQueryParams, Map<String, String> headerParams, Map<String, Object> formParams, List<FileReference> filesContentParams, Boolean addAuthHeaders, ProgressRequestBody.ProgressRequestListener progressRequestListener) throws ApiException, IOException { |
825 | 823 | if (addAuthHeaders) { |
826 | 824 | addOAuthAuthentication(headerParams); |
827 | 825 | } |
828 | 826 |
|
829 | | - String contentType = (String) headerParams.get("Content-Type"); |
830 | | - // ensuring a default content type |
831 | | - if (contentType == null) { |
832 | | - contentType = "application/json"; |
833 | | - } |
| 827 | + RequestBody reqBody = null; |
| 828 | + if (HttpMethod.permitsRequestBody(method)) { |
| 829 | + String contentType = headerParams.get("Content-Type"); |
| 830 | + int partsCount = formParams.size() + filesContentParams.size(); |
| 831 | + if ("application/x-www-form-urlencoded".equals(contentType)) { |
| 832 | + reqBody = buildRequestBodyFormEncoding(formParams); |
| 833 | + } |
| 834 | + else if (partsCount == 0) { |
| 835 | + if (!"DELETE".equals(method)) { |
| 836 | + reqBody = RequestBody.create(MediaType.parse("none"), ""); |
| 837 | + } |
| 838 | + } |
| 839 | + else if (partsCount == 1) { |
| 840 | + Object part = formParams.values().toArray()[0]; |
| 841 | + if (part != null) { |
| 842 | + reqBody = serialize(part); |
| 843 | + } |
| 844 | + } |
| 845 | + else if (partsCount > 1) { |
| 846 | + String boundary = UUID.randomUUID().toString(); |
| 847 | + reqBody = buildRequestBodyMultipart(formParams, filesContentParams, boundary); |
| 848 | + } |
834 | 849 |
|
835 | | - RequestBody reqBody; |
836 | | - if (!HttpMethod.permitsRequestBody(method)) { |
837 | | - reqBody = null; |
838 | | - } |
839 | | - else if (body instanceof RequestBody) { |
840 | | - reqBody = (RequestBody) body; |
841 | | - } |
842 | | - else if ("application/x-www-form-urlencoded".equals(contentType)) { |
843 | | - reqBody = buildRequestBodyFormEncoding(formParams); |
844 | | - } |
845 | | - else if ("multipart/form-data".equals(contentType)) { |
846 | | - String boundary = UUID.randomUUID().toString(); |
847 | | - contentType += "; boundary=" + boundary; |
848 | | - headerParams.put("Content-Type", contentType); |
849 | | - reqBody = buildRequestBodyMultipart(formParams, boundary); |
850 | | - } |
851 | | - else if (body == null) { |
852 | | - if ("DELETE".equals(method)) { |
853 | | - // allow calling DELETE without sending a request body |
854 | | - reqBody = null; |
855 | | - } |
856 | | - else { |
857 | | - // use an empty request body (for POST, PUT and PATCH) |
858 | | - reqBody = RequestBody.create(MediaType.parse(contentType), ""); |
| 850 | + if (reqBody != null && reqBody.contentType() != null && !headerParams.containsKey("Content-Type")) { |
| 851 | + headerParams.put("Content-Type", reqBody.contentType().toString()); |
859 | 852 | } |
860 | | - } |
861 | | - else { |
862 | | - reqBody = serialize(body, contentType); |
863 | 853 | } |
864 | 854 |
|
865 | 855 | final String url = buildUrl(path, queryParams, collectionQueryParams); |
@@ -967,27 +957,26 @@ public RequestBody buildRequestBodyFormEncoding(Map<String, Object> formParams) |
967 | 957 | * which could contain text fields and file fields. |
968 | 958 | * |
969 | 959 | * @param formParams Form parameters in the form of Map |
| 960 | + * @param fileParams Form parameters of additional files |
| 961 | + * @param boundary Boundary string |
970 | 962 | * @throws IOException If fail to serialize the request body object |
| 963 | + * @throws ApiException If fail to serialize the request body object |
971 | 964 | * @return RequestBody |
972 | 965 | */ |
973 | | - public RequestBody buildRequestBodyMultipart(Map<String, Object> formParams, String boundary) throws IOException { |
| 966 | + public RequestBody buildRequestBodyMultipart(Map<String, Object> formParams, List<FileReference> fileParams, String boundary) throws IOException, ApiException { |
974 | 967 | MultipartBuilder mpBuilder = new MultipartBuilder(boundary).type(MultipartBuilder.FORM); |
975 | 968 | if (formParams.isEmpty()) { |
976 | 969 | Headers partHeaders = Headers.of("Content-Disposition", "form-data"); |
977 | 970 | mpBuilder.addPart(partHeaders, RequestBody.create(MediaType.parse("none"), new byte[] {})); |
978 | 971 | } |
979 | 972 | else { |
980 | 973 | for (Entry<String, Object> param : formParams.entrySet()) { |
981 | | - if (param.getValue() instanceof byte[]) { |
982 | | - byte[] file = (byte[]) param.getValue(); |
983 | | - Headers partHeaders = Headers.of("Content-Disposition", "form-data; name=\"" + param.getKey() + "\"; filename=\"" + param.getKey() + "\""); |
984 | | - MediaType mediaType = MediaType.parse(guessContentTypeFromFile(file)); |
985 | | - mpBuilder.addPart(partHeaders, RequestBody.create(mediaType, file)); |
986 | | - } |
987 | | - else { |
988 | | - Headers partHeaders = Headers.of("Content-Disposition", "form-data; name=\"" + param.getKey() + "\""); |
989 | | - mpBuilder.addPart(partHeaders, RequestBody.create(null, parameterToString(param.getValue()))); |
990 | | - } |
| 974 | + Headers partHeaders = Headers.of("Content-Disposition", "form-data; name=\"" + param.getKey() + "\""); |
| 975 | + mpBuilder.addPart(partHeaders, serialize(param.getValue())); |
| 976 | + } |
| 977 | + for (FileReference param : fileParams) { |
| 978 | + Headers partHeaders = Headers.of("Content-Disposition", "form-data; name=\"" + param.getReference() + "\""); |
| 979 | + mpBuilder.addPart(partHeaders, RequestBody.create(MediaType.parse("application/octet-stream"), param.getContent())); |
991 | 980 | } |
992 | 981 | } |
993 | 982 | return mpBuilder.build(); |
@@ -1078,14 +1067,15 @@ public Request buildBatchRequest(BatchPartRequest[] requests, Boolean displayInt |
1078 | 1067 |
|
1079 | 1068 | RequestBody requestBody = builder.build(); |
1080 | 1069 | Map<String, String> headers = new HashMap<>(); |
1081 | | - headers.put("Content-Type", requestBody.contentType().toString()); |
| 1070 | + Map<String, Object> formData = new HashMap<>(); |
| 1071 | + formData.put("Body", requestBody); |
1082 | 1072 |
|
1083 | 1073 | String url = "/words/batch"; |
1084 | 1074 | if (!displayIntermediateResults) { |
1085 | 1075 | url += "?displayIntermediateResults=false"; |
1086 | 1076 | } |
1087 | 1077 |
|
1088 | | - return buildRequest(url, "PUT", new ArrayList<>(), new ArrayList<>(), requestBody, headers, new HashMap<>(), true, null); |
| 1078 | + return buildRequest(url, "PUT", new ArrayList<>(), new ArrayList<>(), headers, formData, new ArrayList<>(), true, null); |
1089 | 1079 | } |
1090 | 1080 |
|
1091 | 1081 | /** |
|
0 commit comments