selfprivacy.org.app/lib/logic/api_maps/rest_maps/api_map.dart

109 lines
2.7 KiB
Dart
Raw Normal View History

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({final BaseOptions? customOptions}) async {
final Dio dio = Dio(customOptions ?? (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 =
2022-06-05 19:36:32 +00:00
(final HttpClient client) {
2021-01-21 21:01:42 +00:00
client.badCertificateCallback =
(final X509Certificate cert, final String host, final int port) =>
true;
2021-01-21 21:01:42 +00:00
return client;
};
2021-09-29 18:28:47 +00:00
dio.interceptors.add(
InterceptorsWrapper(
onError: (final DioError e, final ErrorInterceptorHandler handler) {
print(e.requestOptions.path);
print(e.requestOptions.data);
2021-09-29 18:28:47 +00:00
print(e.message);
print(e.response);
2021-09-29 18:28:47 +00:00
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
String get 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
2022-06-05 19:36:32 +00:00
void close(final Dio client) {
2021-03-26 13:38:39 +00:00
client.close();
validateStatus = null;
}
}
2021-03-25 23:30:34 +00:00
2021-01-14 21:48:05 +00:00
class ConsoleInterceptor extends InterceptorsWrapper {
2022-06-05 19:36:32 +00:00
void addMessage(final Message message) {
2021-01-14 21:48:05 +00:00
getIt.get<ConsoleModel>().addMessage(message);
}
@override
Future<void> onRequest(
2022-06-05 19:36:32 +00:00
final RequestOptions options,
final RequestInterceptorHandler handler,
2021-03-18 12:24:30 +00:00
) async {
2021-01-14 21:48:05 +00:00
addMessage(
Message(
text:
'request-uri: ${options.uri}\nheaders: ${options.headers}\ndata: ${options.data}',
),
);
2022-05-24 18:55:39 +00:00
return super.onRequest(options, handler);
2021-01-14 21:48:05 +00:00
}
@override
Future<void> onResponse(
2022-06-05 19:36:32 +00:00
final Response response,
final ResponseInterceptorHandler handler,
2021-03-18 12:24:30 +00:00
) 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,
2022-05-24 18:55:39 +00:00
handler,
2021-03-18 12:24:30 +00:00
);
2021-01-14 21:48:05 +00:00
}
@override
Future<void> onError(
final DioError err,
final ErrorInterceptorHandler handler,
) async {
2022-06-05 19:36:32 +00:00
final Response? 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
}
}