@@ -3,14 +3,14 @@ part of {{ language.params.packageName }};
33class Client {
44    String endPoint;
55    String type = 'unknown';
6-     Map<String , String > headers;
7-     Map<String , String > config;
6+     Map<String , String >?  headers;
7+     late  Map<String , String > config;
88    bool selfSigned;
99    bool initialized = false;
1010    Dio http;
11-     PersistCookieJar cookieJar;
11+     late  PersistCookieJar cookieJar;
1212
13-     Client({this.endPoint = '{{spec .endpoint }}', this.selfSigned = false, Dio http}) : this.http = http ?? Dio() {
13+     Client({this.endPoint = '{{spec .endpoint }}', this.selfSigned = false, Dio?  http}) : this.http = http ?? Dio() {
1414        // Platform is not supported in web so if web, set type to web automatically and skip Platform check
1515        if(kIsWeb) {
1616            type = 'web';
@@ -34,6 +34,7 @@ class Client {
3434        this.config = {};
3535
3636        assert(endPoint.startsWith(RegExp("http://|https://")), "endPoint $endPoint must start with 'http'");
37+         init();
3738    }
3839
3940    Future<Directory > _getCookiePath() async {
@@ -67,31 +68,29 @@ class Client {
6768    }
6869
6970    Client addHeader(String key, String value) {
70-         headers[key] = value;
71+         headers! [key] = value;
7172
7273        return this;
7374    }
7475
7576    Future init() async {
76-         if(!initialized) {
77-           // if web skip cookie implementation and origin header as those are automatically handled by browsers
78-           if(!kIsWeb) {
77+         // if web skip cookie implementation and origin header as those are automatically handled by browsers
78+         if(!kIsWeb) {
7979            final Directory cookieDir = await _getCookiePath();
80-             cookieJar = new PersistCookieJar(dir: cookieDir.path);
80+             cookieJar = new PersistCookieJar(storage: FileStorage( cookieDir.path) );
8181            this.http.interceptors.add(CookieManager(cookieJar));
8282            PackageInfo packageInfo = await PackageInfo.fromPlatform();
83-             addHeader('Origin', 'appwrite-$type://${packageInfo.packageName ?? packageInfo.appName }');
84-           } else{
85-             // if web set httpClientAdapter as BrowserHttpClientAdapter with  withCredentials true to make cookies work
83+             addHeader('Origin', 'appwrite-$type://${packageInfo.packageName}');
84+         }  else  {
85+             // if web set withCredentials true to make cookies work
8686            this.http.options.extra['withCredentials'] = true;
87-           }
88- 
89-           this.http.options.baseUrl = this.endPoint;
90-           this.http.options.validateStatus = (status) => status <  400;
9187        }
88+ 
89+         this.http.options.baseUrl = this.endPoint;
90+         this.http.options.validateStatus = (status) => status! <  400;
9291    }
9392
94-     Future<Response > call(HttpMethod method, {String path = '', Map<String , String > headers = const {}, Map<String , dynamic > params = const {}, ResponseType responseType}) async {
93+     Future<Response > call(HttpMethod method, {String path = '', Map<String , String > headers = const {}, Map<String , dynamic > params = const {}, ResponseType?  responseType}) async {
9594        if(selfSigned &&  !kIsWeb) {
9695            // Allow self signed requests
9796            (http.httpClientAdapter as DefaultHttpClientAdapter).onHttpClientCreate = (HttpClient client) {
@@ -100,18 +99,21 @@ class Client {
10099            };
101100        }
102101
103-         await this.init();
102+         if(!initialized) {
103+             await this.init();
104+         }
104105
105106        // Origin is hardcoded for testing
106107        Options options = Options(
107-             headers: {...this.headers, ...headers},
108+             headers: {...this.headers! , ...headers},
108109            method: method.name(),
109-             responseType: responseType
110+             responseType: responseType,
111+             listFormat: ListFormat.multiCompatible
110112        );
111113
112114        try {
113115            if(headers['content-type'] == 'multipart/form-data') {
114-                 return await http.request(path, data: FormData.fromMap(params), options: options);
116+                 return await http.request(path, data: FormData.fromMap(params, ListFormat.multiCompatible ), options: options);
115117            }
116118
117119            if (method == HttpMethod.get) {
@@ -128,16 +130,16 @@ class Client {
128130            throw {{spec .title  | caseUcfirst }}Exception(e.message);
129131          }
130132          if(responseType == ResponseType.bytes) {
131-             if(e.response.headers['content-type'].contains('application/json')) {
132-               final res = json.decode(utf8.decode(e.response.data));
133+             if(e.response! .headers['content-type']! .contains('application/json')) {
134+               final res = json.decode(utf8.decode(e.response! .data));
133135              throw {{spec .title  | caseUcfirst }}Exception(res['message'],res['code'], e.response);
134136            } else {
135137              throw {{spec .title  | caseUcfirst }}Exception(e.message);
136138            }
137139          }
138-           throw {{spec .title  | caseUcfirst }}Exception(e.response.data['message'],e.response.data['code'], e.response.data);
140+           throw {{spec .title  | caseUcfirst }}Exception(e.response! .data['message'],e.response! .data['code'], e.response! .data);
139141        } catch(e) {
140-           throw {{spec .title  | caseUcfirst }}Exception(e.message );
142+           throw {{spec .title  | caseUcfirst }}Exception(e.toString() );
141143        }
142144    }
143145}
0 commit comments