2020-04-06 08:39:45 +12:00
|
|
|
import 'dart:io';
|
|
|
|
|
2020-04-06 16:55:38 +12:00
|
|
|
import 'package:cookie_jar/cookie_jar.dart';
|
2020-04-06 08:39:45 +12:00
|
|
|
import 'package:dio/adapter.dart';
|
2020-01-31 05:18:59 +13:00
|
|
|
import 'package:dio/dio.dart';
|
|
|
|
import 'package:dio_cookie_manager/dio_cookie_manager.dart';
|
|
|
|
|
2020-03-27 02:20:07 +13:00
|
|
|
import 'enums.dart';
|
|
|
|
|
2020-01-31 05:18:59 +13:00
|
|
|
class Client {
|
|
|
|
String endPoint;
|
|
|
|
Map<String, String> headers;
|
|
|
|
bool selfSigned;
|
2020-04-06 08:39:45 +12:00
|
|
|
final Dio http;
|
|
|
|
|
2020-04-06 16:55:38 +12:00
|
|
|
Client({this.endPoint = 'https://appwrite.io/v1', this.selfSigned = false, Dio http}) : this.http = http ?? Dio() {
|
2020-01-31 05:18:59 +13:00
|
|
|
this.headers = {
|
|
|
|
'content-type': 'application/json',
|
2020-04-07 00:16:19 +12:00
|
|
|
'x-sdk-version': 'appwrite:dart:0.0.10',
|
2020-01-31 05:18:59 +13:00
|
|
|
};
|
|
|
|
|
2020-04-06 08:39:45 +12:00
|
|
|
assert(endPoint.startsWith(RegExp("http://|https://")), "endPoint $endPoint must start with 'http'");
|
2020-01-31 05:18:59 +13:00
|
|
|
this.http.options.baseUrl = this.endPoint;
|
|
|
|
this.http.options.validateStatus = (status) => status != 404;
|
|
|
|
this.http.interceptors.add(CookieManager(CookieJar()));
|
|
|
|
}
|
|
|
|
|
2020-02-16 07:22:34 +13:00
|
|
|
/// Your project ID
|
2020-01-31 05:18:59 +13:00
|
|
|
Client setProject(value) {
|
2020-04-06 08:39:45 +12:00
|
|
|
addHeader('X-Appwrite-Project', value);
|
2020-01-31 05:18:59 +13:00
|
|
|
return this;
|
|
|
|
}
|
2020-02-16 07:22:34 +13:00
|
|
|
/// Your secret API key
|
2020-01-31 05:18:59 +13:00
|
|
|
Client setKey(value) {
|
2020-04-06 08:39:45 +12:00
|
|
|
addHeader('X-Appwrite-Key', value);
|
2020-01-31 05:18:59 +13:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
Client setLocale(value) {
|
2020-04-06 08:39:45 +12:00
|
|
|
addHeader('X-Appwrite-Locale', value);
|
2020-01-31 05:18:59 +13:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
Client setMode(value) {
|
2020-04-06 08:39:45 +12:00
|
|
|
addHeader('X-Appwrite-Mode', value);
|
2020-01-31 05:18:59 +13:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
Client setSelfSigned({bool status = true}) {
|
2020-04-06 08:39:45 +12:00
|
|
|
selfSigned = status;
|
2020-01-31 05:18:59 +13:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2020-04-06 08:39:45 +12:00
|
|
|
Client setEndpoint(String endPoint) {
|
2020-01-31 05:18:59 +13:00
|
|
|
this.endPoint = endPoint;
|
|
|
|
this.http.options.baseUrl = this.endPoint;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
Client addHeader(String key, String value) {
|
2020-04-06 08:39:45 +12:00
|
|
|
headers[key] = value;
|
2020-01-31 05:18:59 +13:00
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2020-03-27 02:20:07 +13:00
|
|
|
Future<Response> call(HttpMethod method, {String path = '', Map<String, String> headers = const {}, Map<String, dynamic> params = const {}}) {
|
2020-04-06 16:55:38 +12:00
|
|
|
if(selfSigned) {
|
2020-01-31 05:18:59 +13:00
|
|
|
// Allow self signed requests
|
2020-04-06 08:39:45 +12:00
|
|
|
(http.httpClientAdapter as DefaultHttpClientAdapter).onHttpClientCreate = (HttpClient client) {
|
|
|
|
client.badCertificateCallback = (X509Certificate cert, String host, int port) => true;
|
|
|
|
return client;
|
|
|
|
};
|
2020-01-31 05:18:59 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
// Origin is hardcoded for testing
|
|
|
|
Options options = Options(
|
|
|
|
headers: {...this.headers, ...headers, "Origin": "http://localhost"},
|
2020-03-27 02:20:07 +13:00
|
|
|
method: method.name(),
|
2020-01-31 05:18:59 +13:00
|
|
|
);
|
|
|
|
|
2020-04-06 08:39:45 +12:00
|
|
|
if (method == HttpMethod.get) {
|
|
|
|
return http.get(path, queryParameters: params, options: options);
|
|
|
|
} else {
|
|
|
|
return http.request(path, data: params, options: options);
|
2020-01-31 05:18:59 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|