48 lines
1.3 KiB
Dart
48 lines
1.3 KiB
Dart
import 'package:dio/dio.dart';
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
import 'package:logging/logging.dart';
|
|
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
|
|
|
part 'dio.g.dart';
|
|
|
|
final logger = Logger('Dio');
|
|
|
|
@riverpod
|
|
Dio dio(Ref ref) {
|
|
final dio = Dio(BaseOptions(
|
|
baseUrl: 'http://localhost:8080',
|
|
connectTimeout: const Duration(seconds: 5),
|
|
receiveTimeout: const Duration(seconds: 3),
|
|
));
|
|
|
|
dio.interceptors.add(LogInterceptor(responseBody: true));
|
|
|
|
return dio;
|
|
}
|
|
|
|
// Create a custom LogInterceptor using the logger object
|
|
class CustomLogInterceptor extends Interceptor {
|
|
@override
|
|
void onRequest(RequestOptions options, RequestInterceptorHandler handler) {
|
|
logger.info('REQUEST[${options.method}] => PATH: ${options.path}');
|
|
return super.onRequest(options, handler);
|
|
}
|
|
|
|
@override
|
|
// ignore: strict_raw_type
|
|
void onResponse(Response response, ResponseInterceptorHandler handler) {
|
|
logger.info(
|
|
'RESPONSE[${response.statusCode}] => PATH: ${response.requestOptions.path}',
|
|
);
|
|
return super.onResponse(response, handler);
|
|
}
|
|
|
|
@override
|
|
void onError(DioException err, ErrorInterceptorHandler handler) {
|
|
logger.severe(
|
|
'ERROR[${err.response?.statusCode}] => PATH: ${err.requestOptions.path}',
|
|
);
|
|
return super.onError(err, handler);
|
|
}
|
|
}
|