2727
2828package com .aspose .words .cloud ;
2929
30+ import com .aspose .words .cloud .model .requests .RequestIfc ;
3031import com .squareup .okhttp .*;
3132import com .squareup .okhttp .internal .http .HttpMethod ;
3233import com .squareup .okhttp .logging .HttpLoggingInterceptor ;
3637import org .threeten .bp .LocalDate ;
3738import org .threeten .bp .OffsetDateTime ;
3839
40+ import javax .mail .BodyPart ;
41+ import javax .mail .MessagingException ;
42+ import javax .mail .internet .MimeMultipart ;
43+ import javax .mail .util .ByteArrayDataSource ;
3944import java .io .*;
4045import java .lang .reflect .Type ;
4146import java .net .URLConnection ;
@@ -51,10 +56,12 @@ public class ApiClient {
5156 private String apiVersion = "v4.0" ;
5257 private String baseUrl = "https://api.aspose.cloud" ;
5358 private String basePath = baseUrl + "/" + apiVersion ;
54- private String clientVersion = "20.9 " ;
59+ private String clientVersion = "20.10 " ;
5560 private boolean debugging = false ;
5661 private Map <String , String > defaultHeaderMap = new HashMap <String , String >();
5762 private String tempFolderPath = null ;
63+ private Integer notAuthCode = 401 ;
64+ private Integer badRequestCode = 400 ;
5865
5966 private OkHttpClient httpClient ;
6067 private JSON json ;
@@ -92,6 +99,24 @@ public ApiClient() {
9299 setReadTimeout (5 * 60 * 1000 );
93100 }
94101
102+ /**
103+ * Get NotAuth http code
104+ *
105+ * @return App Key
106+ */
107+ public Integer getNotAuthCode () {
108+ return notAuthCode ;
109+ }
110+
111+ /**
112+ * Get BadRequest http code
113+ *
114+ * @return App Key
115+ */
116+ public Integer getBadRequestCode () {
117+ return badRequestCode ;
118+ }
119+
95120 /**
96121 * Get App Key
97122 *
@@ -600,7 +625,17 @@ public <T> T deserialize(Response response, Type returnType) throws ApiException
600625 return null ;
601626 }
602627
603- if ("byte[]" .equals (returnType .toString ())) {
628+ if (returnType .equals (MimeMultipart .class )) {
629+ try {
630+ InputStream in = response .body ().byteStream ();
631+ ByteArrayDataSource dataSource = new ByteArrayDataSource (in , "multipart/form-data" );
632+ return (T ) new MimeMultipart (dataSource );
633+ }
634+ catch (IOException | MessagingException e ) {
635+ throw new ApiException (e );
636+ }
637+ }
638+ else if (returnType .equals (byte [].class )) {
604639 // Handle binary response (byte array).
605640 try {
606641 return (T ) response .body ().bytes ();
@@ -871,22 +906,9 @@ public <T> T handleResponse(Response response, Type returnType) throws ApiExcept
871906 /**
872907 * Build HTTP call with the given options.
873908 *
874- * @param path The sub-path of the HTTP URL
875- * @param method The request method, one of "GET", "HEAD", "OPTIONS", "POST", "PUT", "PATCH" and "DELETE"
876- * @param queryParams The query parameters
877- * @param collectionQueryParams The collection query parameters
878- * @param body The request body object
879- * @param headerParams The header parameters
880- * @param formParams The form parameters
881- * @param authNames The authentications to apply
882- * @param progressRequestListener Progress request listener
883- * @return The HTTP call
884- * @throws ApiException If fail to serialize the request body object
885- * @throws IOException If fail to serialize the request body object
909+ * @param request The http request instance
886910 */
887- public Call buildCall (String path , String method , List <Pair > queryParams , List <Pair > collectionQueryParams , Object body , Map <String , String > headerParams , Map <String , Object > formParams , String [] authNames , ProgressRequestBody .ProgressRequestListener progressRequestListener ) throws ApiException , IOException {
888- Request request = buildRequest (path , method , queryParams , collectionQueryParams , body , headerParams , formParams , authNames , progressRequestListener );
889-
911+ public Call buildCall (Request request ) {
890912 return httpClient .newCall (request );
891913 }
892914
@@ -906,11 +928,10 @@ public Call buildCall(String path, String method, List<Pair> queryParams, List<P
906928 * @throws ApiException If fail to serialize the request body object
907929 * @throws IOException If fail to serialize the request body object
908930 */
909- public Request buildRequest (String path , String method , List <Pair > queryParams , List <Pair > collectionQueryParams , Object body , Map <String , String > headerParams , Map <String , Object > formParams , String [] authNames , ProgressRequestBody .ProgressRequestListener progressRequestListener ) throws ApiException , IOException {
910- addOAuthAuthentication (headerParams );
911- final String url = buildUrl (path , queryParams , collectionQueryParams );
912- final Request .Builder reqBuilder = new Request .Builder ().url (url );
913- processHeaderParams (headerParams , reqBuilder );
931+ 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 {
932+ if (addAuthHeaders ) {
933+ addOAuthAuthentication (headerParams );
934+ }
914935
915936 String contentType = (String ) headerParams .get ("Content-Type" );
916937 // ensuring a default content type
@@ -922,11 +943,17 @@ public Request buildRequest(String path, String method, List<Pair> queryParams,
922943 if (!HttpMethod .permitsRequestBody (method )) {
923944 reqBody = null ;
924945 }
946+ else if (body instanceof RequestBody ) {
947+ reqBody = (RequestBody ) body ;
948+ }
925949 else if ("application/x-www-form-urlencoded" .equals (contentType )) {
926950 reqBody = buildRequestBodyFormEncoding (formParams );
927951 }
928952 else if ("multipart/form-data" .equals (contentType )) {
929- reqBody = buildRequestBodyMultipart (formParams );
953+ String boundary = UUID .randomUUID ().toString ();
954+ contentType += "; boundary=" + boundary ;
955+ headerParams .put ("Content-Type" , contentType );
956+ reqBody = buildRequestBodyMultipart (formParams , boundary );
930957 }
931958 else if (body == null ) {
932959 if ("DELETE" .equals (method )) {
@@ -942,8 +969,11 @@ else if (body == null) {
942969 reqBody = serialize (body , contentType );
943970 }
944971
945- Request request = null ;
972+ final String url = buildUrl (path , queryParams , collectionQueryParams );
973+ final Request .Builder reqBuilder = new Request .Builder ().url (url );
974+ processHeaderParams (headerParams , reqBuilder , addAuthHeaders );
946975
976+ Request request = null ;
947977 if (progressRequestListener != null && reqBody != null ) {
948978 ProgressRequestBody progressRequestBody = new ProgressRequestBody (reqBody , progressRequestListener );
949979 request = reqBuilder .method (method , progressRequestBody ).build ();
@@ -1012,13 +1042,15 @@ public String buildUrl(String path, List<Pair> queryParams, List<Pair> collectio
10121042 * @param headerParams Header parameters in the ofrm of Map
10131043 * @param reqBuilder Reqeust.Builder
10141044 */
1015- public void processHeaderParams (Map <String , String > headerParams , Request .Builder reqBuilder ) {
1045+ public void processHeaderParams (Map <String , String > headerParams , Request .Builder reqBuilder , Boolean addDefaultHeaders ) {
10161046 for (Entry <String , String > param : headerParams .entrySet ()) {
10171047 reqBuilder .header (param .getKey (), parameterToString (param .getValue ()));
10181048 }
1019- for (Entry <String , String > header : defaultHeaderMap .entrySet ()) {
1020- if (!headerParams .containsKey (header .getKey ())) {
1021- reqBuilder .header (header .getKey (), parameterToString (header .getValue ()));
1049+ if (addDefaultHeaders ) {
1050+ for (Entry <String , String > header : defaultHeaderMap .entrySet ()) {
1051+ if (!headerParams .containsKey (header .getKey ())) {
1052+ reqBuilder .header (header .getKey (), parameterToString (header .getValue ()));
1053+ }
10221054 }
10231055 }
10241056 }
@@ -1045,8 +1077,8 @@ public RequestBody buildRequestBodyFormEncoding(Map<String, Object> formParams)
10451077 * @throws IOException If fail to serialize the request body object
10461078 * @return RequestBody
10471079 */
1048- public RequestBody buildRequestBodyMultipart (Map <String , Object > formParams ) throws IOException {
1049- MultipartBuilder mpBuilder = new MultipartBuilder ().type (MultipartBuilder .FORM );
1080+ public RequestBody buildRequestBodyMultipart (Map <String , Object > formParams , String boundary ) throws IOException {
1081+ MultipartBuilder mpBuilder = new MultipartBuilder (boundary ).type (MultipartBuilder .FORM );
10501082 if (formParams .isEmpty ()) {
10511083 Headers partHeaders = Headers .of ("Content-Disposition" , "form-data" );
10521084 mpBuilder .addPart (partHeaders , RequestBody .create (MediaType .parse ("none" ), new byte [] {}));
@@ -1113,6 +1145,97 @@ public void requestToken() throws ApiException {
11131145 }
11141146 }
11151147
1148+ /**
1149+ * AddParameterToQuery
1150+ */
1151+ public void addParameterToQuery (List <Pair > queryParams , String paramName , Object paramValue ) {
1152+ queryParams .addAll (parameterToPair (paramName , paramValue ));
1153+ }
1154+
1155+ /**
1156+ * AddParameterToPath
1157+ */
1158+ public String addParameterToPath (String path , String paramName , Object paramValue ) {
1159+ if (path .contains ("{" + paramName + "}" )) {
1160+ if (paramValue == null || paramValue .equals ("" )) {
1161+ return path .replace ("{" + paramName + "}" , "" );
1162+ }
1163+ else {
1164+ return path .replace ("{" + paramName + "}" , paramValue .toString ());
1165+ }
1166+ }
1167+
1168+ return path ;
1169+ }
1170+
1171+ /**
1172+ * Build batch request
1173+ */
1174+ public Request buildBatchRequest (RequestIfc [] requests ) throws ApiException , IOException {
1175+ Headers multipartHeaders = Headers .of ("Content-Disposition" , "form-data" );
1176+ MultipartBuilder builder = new MultipartBuilder ().type (MultipartBuilder .FORM );
1177+ for (RequestIfc request : requests ) {
1178+ Request httpRequest = request .buildHttpRequest (this , null , null , false );
1179+ builder .addPart (multipartHeaders , new ChildRequestContent (httpRequest , basePath + "/words/" ));
1180+ }
1181+
1182+ RequestBody requestBody = builder .build ();
1183+ Map <String , String > headers = new HashMap <>();
1184+ headers .put ("Content-Type" , requestBody .contentType ().toString ());
1185+ return buildRequest ("/words/batch" , "PUT" , new ArrayList <>(), new ArrayList <>(), requestBody , headers , new HashMap <>(), true , null );
1186+ }
1187+
1188+ /**
1189+ * Parse batch part
1190+ */
1191+ public Object parseBatchPart (Request masterRequest , BodyPart bodyPart , Type returnType ) throws IOException , MessagingException , ApiException {
1192+ InputStream is = bodyPart .getInputStream ();
1193+ ByteArrayOutputStream buffer = new ByteArrayOutputStream ();
1194+
1195+ int nRead ;
1196+ byte [] data = new byte [16384 ];
1197+
1198+ while ((nRead = is .read (data , 0 , data .length )) != -1 ) {
1199+ buffer .write (data , 0 , nRead );
1200+ }
1201+
1202+ try {
1203+ String stringData = buffer .toString ("UTF-8" );
1204+ int lastSplitIndex = stringData .indexOf ("\r \n " );
1205+ Integer responseCode = Integer .parseInt (stringData .substring (0 , lastSplitIndex ).split ("\\ s+" )[0 ]);
1206+ if (responseCode != 200 ) {
1207+ return new ApiException (responseCode , stringData );
1208+ }
1209+
1210+ Headers .Builder headersBuilder = new Headers .Builder ();
1211+ while (true ) {
1212+ int splitIndex = stringData .indexOf ("\r \n " , lastSplitIndex + 2 );
1213+ String headerStr = stringData .substring (lastSplitIndex + 2 , splitIndex );
1214+ lastSplitIndex = splitIndex ;
1215+
1216+ if (headerStr .isEmpty ()) {
1217+ break ;
1218+ }
1219+
1220+ headersBuilder .add (headerStr );
1221+ }
1222+
1223+ ResponseBody responseBody = null ;
1224+ Headers headers = headersBuilder .build ();
1225+ byte [] rawBody = buffer .toByteArray ();
1226+ if (rawBody .length != lastSplitIndex + 2 ) {
1227+ byte [] responseBytes = Arrays .copyOfRange (rawBody , lastSplitIndex + 2 , rawBody .length );
1228+ responseBody = ResponseBody .create (MediaType .parse (headers .get ("Content-Type" )), responseBytes );
1229+ }
1230+
1231+ Response response = new Response .Builder ().request (masterRequest ).protocol (Protocol .HTTP_1_1 ).code (responseCode ).headers (headers ).body (responseBody ).build ();
1232+ return deserialize (response , returnType );
1233+ }
1234+ catch (Exception e ) {
1235+ throw new ApiException (400 , "Invalid response format." );
1236+ }
1237+ }
1238+
11161239 /**
11171240 * Add OAuth2 header
11181241 *
0 commit comments