selfprivacy.org.app/lib/logic/api_maps/server.dart

72 lines
1.5 KiB
Dart
Raw Normal View History

2021-03-26 01:30:34 +02:00
import 'dart:async';
2021-01-21 23:01:42 +02:00
import 'dart:io';
2021-01-21 09:35:38 +02:00
import 'package:dio/dio.dart';
2021-03-26 01:30:34 +02:00
import 'package:selfprivacy/config/get_it_config.dart';
2021-06-08 21:52:44 +03:00
import 'package:selfprivacy/logic/models/user.dart';
2021-01-21 09:35:38 +02:00
import 'api_map.dart';
2021-03-26 01:30:34 +02:00
class ServerApi extends ApiMap {
bool hasLoger;
bool isWithToken;
ServerApi({this.hasLoger = false, this.isWithToken = true});
BaseOptions get options {
var options = BaseOptions();
if (isWithToken) {
var cloudFlareDomain = getIt<ApiConfigModel>().cloudFlareDomain;
var domainName = cloudFlareDomain!.domainName;
options = BaseOptions(baseUrl: 'https://api.$domainName');
}
return options;
2021-01-21 23:01:42 +02:00
}
Future<bool> isHttpServerWorking() async {
bool res;
Response response;
2021-03-26 01:30:34 +02:00
var client = await getClient();
2021-01-21 23:01:42 +02:00
try {
2021-03-26 01:30:34 +02:00
response = await client.get('/serviceStatus');
2021-01-21 23:01:42 +02:00
res = response.statusCode == HttpStatus.ok;
} catch (e) {
res = false;
2021-01-21 09:35:38 +02:00
}
2021-03-26 15:38:39 +02:00
close(client);
2021-01-21 23:01:42 +02:00
return res;
2021-01-21 09:35:38 +02:00
}
2021-01-21 23:01:42 +02:00
2021-06-08 21:52:44 +03:00
Future<bool> createUser(User user) async {
bool res;
Response response;
var client = await getClient();
try {
2021-06-21 00:08:52 +03:00
response = await client.post(
'/createUser',
options: Options(
headers: {
"X-Username": user.login,
"X-Password": user.password,
},
),
);
2021-06-08 21:52:44 +03:00
res = response.statusCode == HttpStatus.ok;
} catch (e) {
2021-06-21 00:08:52 +03:00
print(e);
2021-06-08 21:52:44 +03:00
res = false;
}
2021-06-21 00:08:52 +03:00
2021-06-08 21:52:44 +03:00
close(client);
return res;
}
2021-03-26 01:30:34 +02:00
String get rootAddress =>
throw UnimplementedError('not used in with implementation');
2021-01-21 23:01:42 +02:00
}