@@ -32,6 +32,8 @@ import 'dart:typed_data';
3232
3333import 'package:http/http.dart' as http;
3434import 'package:uuid/uuid.dart' ;
35+ import 'package:convert/convert.dart' ;
36+ import 'package:pointycastle/export.dart' ;
3537
3638import '../aspose_words_cloud.dart' ;
3739import './api_request_data.dart' ;
@@ -41,13 +43,52 @@ import './requests/batch_request.dart';
4143
4244class ApiClient {
4345 String _authToken;
46+ bool _encryptorInitialized = false ;
47+
48+ final _publicKeyRequester;
49+ final PKCS1Encoding _encrypter = PKCS1Encoding (RSAEngine ());
4450 final _starting = ByteData .view (utf8.encoder.convert ('--' ).buffer);
4551 final _newline = ByteData .view (utf8.encoder.convert ('\r\n ' ).buffer);
4652 final _newline2x = ByteData .view (utf8.encoder.convert ('\r\n\r\n ' ).buffer);
4753
4854 final Configuration configuration;
4955
50- ApiClient (final this .configuration);
56+ ApiClient (final this .configuration, final this ._publicKeyRequester);
57+
58+ Future _initializeEncrypter () async {
59+ if (_encryptorInitialized == true ) {
60+ return ;
61+ }
62+
63+ _encryptorInitialized = true ;
64+ try {
65+ final rsaPublicKey = await _publicKeyRequester (GetPublicKeyRequest ());
66+ if (rsaPublicKey == null || rsaPublicKey.modulus == null || rsaPublicKey.exponent == null ) {
67+ throw ApiException (400 , 'Invalid public key response.' );
68+ }
69+
70+ final modulus = BigInt .parse (hex.encode (base64Decode (rsaPublicKey.modulus as String )), radix: 16 );
71+ final exponent = BigInt .parse (hex.encode (base64Decode (rsaPublicKey.exponent as String )), radix: 16 );
72+ var pubKey = RSAPublicKey (modulus, exponent);
73+
74+ _encrypter.init (true , PublicKeyParameter <RSAPublicKey >(pubKey));
75+ }
76+ catch (_) {
77+ _encryptorInitialized = false ;
78+ rethrow ;
79+ }
80+ }
81+
82+ Future <String > encryptPassword (final String password) async {
83+ await _initializeEncrypter ();
84+ return base64Encode (
85+ _encrypter.process (
86+ Uint8List .fromList (
87+ utf8.encode (password)
88+ )
89+ )
90+ );
91+ }
5192
5293 void _handleResponse (final int statusCode, final String reasonPhrase, final ByteData body) {
5394 if (statusCode != 200 ) {
@@ -364,7 +405,7 @@ class ApiClient {
364405 }
365406
366407 Future <dynamic > call (final RequestBase request) async {
367- var requestData = request.createRequestData (this );
408+ var requestData = await request.createRequestData (this );
368409 var response = await _callWithChecks (requestData);
369410 if (response == null ) {
370411 return null ;
@@ -373,32 +414,29 @@ class ApiClient {
373414 return request.deserializeResponse (this , response);
374415 }
375416
376- Future < List <dynamic > > callBatch (final List <BatchRequest > requests) async {
377- var bodyParts = requests
378- . map (( request) => request. createRequestData ( this ))
379- . map ((requestData) => serializeBatchPart (requestData))
380- . toList ();
417+ Future < List <dynamic > > callBatch (final List <BatchRequest > requests, final bool displayIntermediateResults ) async {
418+ var bodyParts = < ApiRequestPart > [];
419+ for ( final request in requests) {
420+ bodyParts. add ( serializeBatchPart ( await request. createRequestData ( this )));
421+ }
381422 var boundary = Uuid ().v4 ();
382- var batchUrl = '${configuration .getApiRootUrl ()}/words/batch' ;
423+ var batchUrl = '${configuration .getApiRootUrl ()}/words/batch?displayIntermediateResults=$ displayIntermediateResults ' ;
383424 var batchHeaders = < String , String > {};
384425 var batchBody = serializeMultipart (bodyParts, boundary);
385426 batchHeaders['Content-Type' ] = 'multipart/form-data; boundary="$boundary "' ;
386427
387428 var batchRequestData = ApiRequestData ('PUT' , batchUrl, batchHeaders, batchBody);
388429 var response = await _callWithChecks (batchRequestData);
389430 var responseParts = deserializeMultipartBatch (response);
390- if (responseParts.length != requests.length) {
391- throw ApiException (400 , 'Response and request parts mismatch.' );
392- }
393-
394- var result = List <dynamic >.filled (requests.length, null );
395- for (var i = 0 ; i < requests.length; i++ ) {
396- if (! responseParts.containsKey (requests[i].getRequestId ())) {
431+ var result = < dynamic > [];
432+ responseParts.forEach ((key, value) {
433+ var request = requests.firstWhere ((element) => element.getRequestId () == key);
434+ if (request == null ) {
397435 throw ApiException (400 , 'Failed to deserialize batch multipart response.' );
398436 }
399437
400- result[i] = deserializeBatchPart (requests[i] .getRequest (), responseParts[requests[i]. getRequestId ()] );
401- }
438+ result. add ( deserializeBatchPart (request .getRequest (), value) );
439+ });
402440
403441 return result;
404442 }
@@ -433,7 +471,7 @@ class ApiClient {
433471
434472 var httpRequest = http.Request (requestData.method, Uri .parse (requestData.url));
435473 httpRequest.headers['x-aspose-client' ] = 'dart sdk' ;
436- httpRequest.headers['x-aspose-client-version' ] = '21.9 ' ;
474+ httpRequest.headers['x-aspose-client-version' ] = '21.10 ' ;
437475 httpRequest.headers['Authorization' ] = await _getAuthToken ();
438476 if (requestData.headers != null ) {
439477 httpRequest.headers.addAll (requestData.headers);
0 commit comments