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/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);
|
2022-02-16 07:28:29 +00:00
|
|
|
if (hasLogger) {
|
2021-03-25 23:30:34 +00:00
|
|
|
dio.interceptors.add(PrettyDioLogger());
|
|
|
|
}
|
2021-09-29 18:28:47 +00:00
|
|
|
dio.interceptors.add(ConsoleInterceptor());
|
2021-03-25 23:30:34 +00:00
|
|
|
(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-09-29 18:28:47 +00:00
|
|
|
|
|
|
|
dio.interceptors.add(InterceptorsWrapper(onError: (DioError e, handler) {
|
|
|
|
print(e.requestOptions.path);
|
|
|
|
print(e.requestOptions.data);
|
|
|
|
|
|
|
|
print(e.message);
|
|
|
|
print(e.response);
|
|
|
|
|
|
|
|
return handler.next(e);
|
|
|
|
}));
|
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;
|
2022-02-16 07:28:29 +00:00
|
|
|
abstract final bool hasLogger;
|
2021-03-25 23:30:34 +00:00
|
|
|
abstract final bool isWithToken;
|
|
|
|
|
2021-03-26 13:38:39 +00:00
|
|
|
ValidateStatus? validateStatus;
|
2021-03-25 23:30:34 +00:00
|
|
|
|
2021-03-26 13:38:39 +00:00
|
|
|
void close(Dio client) {
|
|
|
|
client.close();
|
|
|
|
validateStatus = null;
|
|
|
|
}
|
|
|
|
}
|
2021-03-25 23:30:34 +00:00
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|