2021-01-21 21:01:42 +00:00
|
|
|
import 'dart:developer';
|
|
|
|
import 'dart:io';
|
|
|
|
|
|
|
|
import 'package:dio/adapter.dart';
|
2021-01-06 17:35:57 +00:00
|
|
|
import 'package:dio/dio.dart';
|
2021-01-14 21:48:05 +00:00
|
|
|
import 'package:selfprivacy/config/get_it_config.dart';
|
|
|
|
import 'package:selfprivacy/logic/get_it/console.dart';
|
|
|
|
import 'package:selfprivacy/logic/models/message.dart';
|
2021-01-06 17:35:57 +00:00
|
|
|
|
|
|
|
abstract class ApiMap {
|
2021-01-21 21:01:42 +00:00
|
|
|
ApiMap() {
|
|
|
|
var client = Dio()..interceptors.add(ConsoleInterceptor());
|
|
|
|
(client.httpClientAdapter as DefaultHttpClientAdapter).onHttpClientCreate =
|
|
|
|
(HttpClient client) {
|
|
|
|
client.badCertificateCallback =
|
|
|
|
(X509Certificate cert, String host, int port) => true;
|
|
|
|
return client;
|
|
|
|
};
|
|
|
|
loggedClient = client;
|
|
|
|
}
|
2021-03-15 15:39:44 +00:00
|
|
|
String? rootAddress;
|
2021-01-06 17:35:57 +00:00
|
|
|
|
2021-03-15 15:39:44 +00:00
|
|
|
late Dio loggedClient;
|
2021-01-06 17:35:57 +00:00
|
|
|
|
|
|
|
void close() {
|
2021-01-14 21:48:05 +00:00
|
|
|
loggedClient.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class ConsoleInterceptor extends InterceptorsWrapper {
|
|
|
|
void addMessage(Message message) {
|
|
|
|
getIt.get<ConsoleModel>().addMessage(message);
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
2021-03-18 12:24:30 +00:00
|
|
|
Future onRequest(
|
|
|
|
RequestOptions options,
|
|
|
|
RequestInterceptorHandler requestInterceptorHandler,
|
|
|
|
) async {
|
2021-01-14 21:48:05 +00:00
|
|
|
addMessage(
|
|
|
|
Message(
|
|
|
|
text:
|
|
|
|
'request-uri: ${options.uri}\nheaders: ${options.headers}\ndata: ${options.data}',
|
|
|
|
),
|
|
|
|
);
|
2021-03-18 12:24:30 +00:00
|
|
|
return super.onRequest(options, requestInterceptorHandler);
|
2021-01-14 21:48:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
2021-03-18 12:24:30 +00:00
|
|
|
Future onResponse(
|
|
|
|
Response response,
|
|
|
|
ResponseInterceptorHandler requestInterceptorHandler,
|
|
|
|
) async {
|
2021-01-14 21:48:05 +00:00
|
|
|
addMessage(
|
|
|
|
Message(
|
|
|
|
text:
|
2021-03-18 12:24:30 +00:00
|
|
|
'response-uri: ${response.realUri}\ncode: ${response.statusCode}\ndata: ${response.toString()}\n',
|
2021-01-14 21:48:05 +00:00
|
|
|
),
|
|
|
|
);
|
2021-03-18 12:24:30 +00:00
|
|
|
return super.onResponse(
|
|
|
|
response,
|
|
|
|
requestInterceptorHandler,
|
|
|
|
);
|
2021-01-14 21:48:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
2021-03-18 12:24:30 +00:00
|
|
|
Future onError(DioError err, ErrorInterceptorHandler handler) async {
|
2021-01-18 10:21:55 +00:00
|
|
|
var response = err.response;
|
2021-01-21 21:01:42 +00:00
|
|
|
log(err.toString());
|
2021-01-18 10:21:55 +00:00
|
|
|
addMessage(
|
|
|
|
Message.warn(
|
|
|
|
text:
|
2021-03-18 12:24:30 +00:00
|
|
|
'response-uri: ${response?.realUri}\ncode: ${response?.statusCode}\ndata: ${response?.toString()}\n',
|
2021-01-18 10:21:55 +00:00
|
|
|
),
|
|
|
|
);
|
2021-03-18 12:24:30 +00:00
|
|
|
return super.onError(err, handler);
|
2021-01-06 17:35:57 +00:00
|
|
|
}
|
|
|
|
}
|