mirror of
https://github.com/SyncrowIOT/web.git
synced 2025-07-10 07:07:19 +00:00
dio and login functions
This commit is contained in:
76
lib/services/api/http_interceptor.dart
Normal file
76
lib/services/api/http_interceptor.dart
Normal file
@ -0,0 +1,76 @@
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
|
||||
import 'dart:async';
|
||||
import 'package:syncrow_web/services/api/network_exception.dart';
|
||||
import 'package:syncrow_web/utils/snack_bar.dart';
|
||||
import 'api_links_endpoints.dart';
|
||||
|
||||
class HTTPInterceptor extends InterceptorsWrapper {
|
||||
List<String> headerExclusionList = [];
|
||||
|
||||
List<String> headerExclusionListOfAddedParameters = [
|
||||
ApiEndpoints.login,
|
||||
];
|
||||
@override
|
||||
void onResponse(Response response, ResponseInterceptorHandler handler) async {
|
||||
if (await validateResponse(response)) {
|
||||
super.onResponse(response, handler);
|
||||
} else {
|
||||
handler.reject(DioException(requestOptions: response.requestOptions, response: response));
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void onRequest(RequestOptions options, RequestInterceptorHandler handler) async {
|
||||
var storage = const FlutterSecureStorage();
|
||||
// var token = await storage.read(key: Token.loginAccessTokenKey);
|
||||
// if (checkHeaderExclusionListOfAddedParameters(options.path)) {
|
||||
// options.headers.putIfAbsent(HttpHeaders.authorizationHeader, () => "Bearer $token");
|
||||
// }
|
||||
// options.headers['Authorization'] = 'Bearer ${'${token!}123'}';
|
||||
super.onRequest(options, handler);
|
||||
}
|
||||
|
||||
@override
|
||||
void onError(DioException err, ErrorInterceptorHandler handler) async {
|
||||
ServerFailure failure = ServerFailure.fromDioError(err);
|
||||
if (failure.toString().isNotEmpty) {
|
||||
CustomSnackBar.displaySnackBar(failure.toString());
|
||||
}
|
||||
var storage = const FlutterSecureStorage();
|
||||
// var token = await storage.read(key: Token.loginAccessTokenKey);
|
||||
// if (err.response?.statusCode == 401 && token != null) {
|
||||
// await AuthCubit.get(NavigationService.navigatorKey.currentContext!).logout();
|
||||
// }
|
||||
super.onError(err, handler);
|
||||
}
|
||||
|
||||
/// Validates the response and returns true if it is successful (status code 2xx).
|
||||
Future<bool> validateResponse(Response response) async {
|
||||
if (response.statusCode != null) {
|
||||
if (response.statusCode! >= 200 && response.statusCode! < 300) {
|
||||
// If the response status code is within the successful range (2xx),
|
||||
// return true indicating a successful response.
|
||||
return true;
|
||||
} else {
|
||||
// If the response status code is not within the successful range (2xx),
|
||||
// return false indicating an unsuccessful response.
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
// If the response status code is null, return false indicating an unsuccessful response.
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
checkHeaderExclusionListOfAddedParameters(String path) {
|
||||
bool shouldAddHeader = true;
|
||||
|
||||
for (var urlConstant in headerExclusionListOfAddedParameters) {
|
||||
if (path.contains(urlConstant)) {
|
||||
shouldAddHeader = false;
|
||||
}
|
||||
}
|
||||
return shouldAddHeader;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user