configuration method
Future<Dio>
configuration(
- Dio dioClient
)
Implementation
Future<Dio> configuration(Dio dioClient) async {
if (!kIsWeb) {
await Rhttp.init();
final rhttpAdapter = await RhttpCompatibleClient.create(
settings: ClientSettings(
timeout: 10.seconds,
connectTimeout: 10.seconds,
throwOnStatusCode: false,
),
);
dioClient.httpClientAdapter = ConversionLayerAdapter(rhttpAdapter);
// // Transform json with compute
// dioClient.transformer = FlutterTransformer();
}
// Integration retry
dioClient.interceptors.add(
rt.RetryInterceptor(
dio: dioClient,
// logPrint: print, // specify log function (optional)
retryDelays: [
// set delays between retries (optional)
1.seconds, // wait 1 sec before first retry
2.seconds, // wait 2 sec before second retry
// Duration(seconds: 3), // wait 3 sec before third retry
],
),
);
// Add interceptor for prevent response when system is maintaining...
dioClient.interceptors.add(
InterceptorsWrapper(
onResponse: (response, handler) async {
final bool isRefreshingToken =
response.requestOptions.path == ApiEndpoints.auth &&
response.requestOptions.method == 'GET';
if (response.statusCode == StatusCode.unauthorized) {
if (isRefreshingToken) {
handler.next(response);
_logOut();
} else if (_authLocal.refreshToken.isNotEmpty &&
_authLocal.accessToken.isNotEmpty) {
try {
final String oldAccessToken =
response.requestOptions.headers['Authorization'];
final (String accessToken, String _) = await onRefreshToken(
oldAccessToken: oldAccessToken.split(' ').last,
);
response.requestOptions.headers['Authorization'] =
'Bearer $accessToken';
final Response cloneReq = await dioClient.fetch(
response.requestOptions,
);
handler.resolve(cloneReq);
// ignore: empty_catches
} catch (_) {
handler.next(response);
_logOut();
}
} else {
handler.next(response);
}
} else {
handler.next(response);
}
},
onError: (error, handler) async {},
),
);
return dioClient;
}