This repository has been archived on 2024-10-18. You can view files and clone it, but cannot push or open issues or pull requests.
ftc_patient_app/lib/web_services/logging_interceptor.dart

104 lines
2.9 KiB
Dart

import 'dart:convert';
import 'dart:developer';
import 'package:dio/dio.dart';
import 'package:flutter/foundation.dart';
class Logging extends Interceptor {
// String get _token => LocalStorage.token ?? emptyString;
// String get _userId => GetStorage().read(keyUserId) ?? emptyString;
// Map<String, String> get headers => {HttpHeaders.authorizationHeader: _token};
@override
void onRequest(RequestOptions options, RequestInterceptorHandler handler) {
// if (_token.isNotEmpty) {
// options.headers.addAll({HttpHeaders.authorizationHeader: _token});
// }
if (kDebugMode) {
// Logger.print(options);
_CurlLogger.print(options);
}
return super.onRequest(options, handler);
}
@override
void onResponse(Response response, ResponseInterceptorHandler handler) {
if (kDebugMode) {
log('RESPONSE(${response.statusCode}) => ${response.requestOptions.baseUrl + response.requestOptions.path}'
'\n'
"api response: ${jsonEncode(response.data)}");
}
return super.onResponse(response, handler);
}
@override
void onError(DioException err, ErrorInterceptorHandler handler) {
if (kDebugMode) {
debugPrint(
'ERROR(${err.response?.statusCode}) => ${err.requestOptions.baseUrl + err.requestOptions.path}',
);
}
return super.onError(err, handler);
}
}
abstract class _SimpleLogger {
static print(RequestOptions options) {
debugPrint('REQUEST(${options.method}) => ${options.uri.toString()}');
debugPrint('Headers: ${options.headers}');
if (options.queryParameters.isNotEmpty) {
log("queryParameters: ${options.queryParameters}");
}
if (options.data != null) {
if (options.data is FormData) {
debugPrint("data: ");
for (var value in (options.data as FormData).fields) {
log(value.toString());
}
debugPrint("files: ");
for (var value in (options.data as FormData).files) {
log("${value.key}: ${value.value.filename}");
}
} else {
log("data: ${options.data}");
}
}
}
}
abstract class _CurlLogger {
static print(RequestOptions options) {
String curl = 'curl';
curl += " --location '${options.uri}'";
if (options.data != null) {
// Add the data
if (options.data is FormData) {
for (MapEntry entry in (options.data as FormData).fields) {
curl += " --form '${entry.key}=\"${entry.value}\"'";
}
for (MapEntry<String, MultipartFile> entry
in (options.data as FormData).files) {
curl += " --form '${entry.key}=@\"${entry.value.filename}\"'";
}
} else {
curl += ' --data \'${options.data}\'';
}
}
if (options.headers.isNotEmpty) {
// Add the headers
options.headers.forEach((key, value) {
curl += ' --header \'$key: $value\'';
});
}
log(curl);
}
}