1
0
Fork 0
mirror of synced 2024-07-09 16:36:09 +12:00
appwrite/app/sdks/flutter-dart/lib/client.dart

129 lines
3.9 KiB
Dart
Raw Normal View History

2020-04-06 08:39:45 +12:00
import 'dart:io';
2020-01-31 05:18:59 +13:00
import 'package:dio/dio.dart';
2020-04-09 05:26:18 +12:00
import 'package:dio/adapter.dart';
2020-01-31 05:18:59 +13:00
import 'package:dio_cookie_manager/dio_cookie_manager.dart';
2020-04-09 05:26:18 +12:00
import 'package:cookie_jar/cookie_jar.dart';
import 'package:path_provider/path_provider.dart';
import 'package:package_info/package_info.dart';
2020-01-31 05:18:59 +13:00
2020-03-27 02:20:07 +13:00
import 'enums.dart';
2020-01-31 05:18:59 +13:00
class Client {
String endPoint;
2020-04-09 05:26:18 +12:00
String type = 'unknown';
2020-01-31 05:18:59 +13:00
Map<String, String> headers;
2020-04-09 05:26:18 +12:00
Map<String, String> config;
2020-01-31 05:18:59 +13:00
bool selfSigned;
2020-04-09 05:26:18 +12:00
bool init = false;
Dio http;
PersistCookieJar cookieJar;
2020-04-06 08:39:45 +12:00
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-04-09 05:26:18 +12:00
type = (Platform.isIOS) ? 'ios' : type;
type = (Platform.isMacOS) ? 'macos' : type;
type = (Platform.isAndroid) ? 'android' : type;
type = (Platform.isLinux) ? 'linux' : type;
type = (Platform.isWindows) ? 'windows' : type;
type = (Platform.isFuchsia) ? 'fuchsia' : type;
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-09 05:26:18 +12:00
this.config = {};
2020-04-06 08:39:45 +12:00
assert(endPoint.startsWith(RegExp("http://|https://")), "endPoint $endPoint must start with 'http'");
2020-04-09 05:26:18 +12:00
}
Future<Directory> _getCookiePath() async {
final directory = await getApplicationDocumentsDirectory();
final path = directory.path;
final Directory dir = new Directory('$path/cookies');
await dir.create();
return dir;
2020-01-31 05:18:59 +13:00
}
2020-02-16 07:22:34 +13:00
/// Your project ID
2020-01-31 05:18:59 +13:00
Client setProject(value) {
2020-04-09 05:26:18 +12:00
config['project'] = 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-04-09 05:26:18 +12:00
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-09 05:26:18 +12:00
config['key'] = value;
2020-04-06 08:39:45 +12:00
addHeader('X-Appwrite-Key', value);
2020-01-31 05:18:59 +13:00
return this;
}
2020-04-09 05:26:18 +12:00
2020-01-31 05:18:59 +13:00
Client setLocale(value) {
2020-04-09 05:26:18 +12:00
config['locale'] = value;
2020-04-06 08:39:45 +12:00
addHeader('X-Appwrite-Locale', value);
2020-01-31 05:18:59 +13:00
return this;
}
2020-04-09 05:26:18 +12:00
2020-01-31 05:18:59 +13:00
Client setMode(value) {
2020-04-09 05:26:18 +12:00
config['mode'] = value;
2020-04-06 08:39:45 +12:00
addHeader('X-Appwrite-Mode', value);
2020-01-31 05:18:59 +13:00
return this;
}
2020-04-09 05:26:18 +12:00
2020-01-31 05:18:59 +13:00
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-04-09 05:26:18 +12:00
Future<Response> call(HttpMethod method, {String path = '', Map<String, String> headers = const {}, Map<String, dynamic> params = const {}}) async {
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
}
2020-04-09 05:26:18 +12:00
if(!init) {
final Directory cookieDir = await _getCookiePath();
cookieJar = new PersistCookieJar(dir:cookieDir.path);
this.http.options.baseUrl = this.endPoint;
this.http.options.validateStatus = (status) => status < 400;
this.http.interceptors.add(CookieManager(cookieJar));
PackageInfo packageInfo = await PackageInfo.fromPlatform();
addHeader('Origin', 'appwrite-' + type + '://' + packageInfo.packageName);
init = true;
}
2020-01-31 05:18:59 +13:00
// Origin is hardcoded for testing
Options options = Options(
2020-04-09 05:26:18 +12:00
headers: {...this.headers, ...headers},
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
}
}
}