2021-03-25 23:30:34 +00:00
|
|
|
import 'dart:async';
|
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-03-25 23:30:34 +00:00
|
|
|
import 'package:pretty_dio_logger/pretty_dio_logger.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
|
|
|
|
2021-03-25 23:30:34 +00:00
|
|
|
abstract class ApiMap {
|
|
|
|
|
|
|
|
Future<Dio> getClient() async {
|
|
|
|
var dio = Dio(await options);
|
|
|
|
if (hasLoger) {
|
|
|
|
dio.interceptors.add(PrettyDioLogger());
|
|
|
|
}
|
|
|
|
dio..interceptors.add(ConsoleInterceptor());
|
|
|
|
(dio.httpClientAdapter as DefaultHttpClientAdapter).onHttpClientCreate =
|
2021-01-21 21:01:42 +00:00
|
|
|
(HttpClient client) {
|
|
|
|
client.badCertificateCallback =
|
|
|
|
(X509Certificate cert, String host, int port) => true;
|
|
|
|
return client;
|
|
|
|
};
|
2021-03-25 23:30:34 +00:00
|
|
|
return dio;
|
2021-01-21 21:01:42 +00:00
|
|
|
}
|
2021-01-06 17:35:57 +00:00
|
|
|
|
2021-03-25 23:30:34 +00:00
|
|
|
FutureOr<BaseOptions> get options;
|
2021-01-06 17:35:57 +00:00
|
|
|
|
2021-03-25 23:30:34 +00:00
|
|
|
abstract final String rootAddress;
|
|
|
|
abstract final bool hasLoger;
|
|
|
|
abstract final bool isWithToken;
|
2021-01-14 21:48:05 +00:00
|
|
|
}
|
|
|
|
|
2021-03-25 23:30:34 +00:00
|
|
|
// abstract class ApiMapOld {
|
|
|
|
// ApiMapOld() {
|
|
|
|
// 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;
|
|
|
|
// }
|
|
|
|
// String? rootAddress;
|
|
|
|
|
|
|
|
// late Dio loggedClient;
|
|
|
|
|
|
|
|
// void close() {
|
|
|
|
// loggedClient.close();
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
2021-01-14 21:48:05 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|