mirror of
https://github.com/SyncrowIOT/web.git
synced 2025-07-10 15:17:31 +00:00
@ -1,10 +1,10 @@
|
||||
import 'dart:async';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
|
||||
import 'package:syncrow_web/pages/auth/model/token.dart';
|
||||
import 'package:syncrow_web/services/api/network_exception.dart';
|
||||
import 'dart:async';
|
||||
import 'package:syncrow_web/utils/constants/api_const.dart';
|
||||
import 'package:syncrow_web/utils/snack_bar.dart';
|
||||
|
||||
@ -18,8 +18,7 @@ class HTTPInterceptor extends InterceptorsWrapper {
|
||||
];
|
||||
|
||||
@override
|
||||
Future<void> onResponse(
|
||||
Response response, ResponseInterceptorHandler handler) async {
|
||||
void onResponse(Response response, ResponseInterceptorHandler handler) async {
|
||||
if (await validateResponse(response)) {
|
||||
super.onResponse(response, handler);
|
||||
} else {
|
||||
@ -29,27 +28,26 @@ class HTTPInterceptor extends InterceptorsWrapper {
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> onRequest(
|
||||
void onRequest(
|
||||
RequestOptions options, RequestInterceptorHandler handler) async {
|
||||
const storage = FlutterSecureStorage();
|
||||
final token = await storage.read(key: Token.loginAccessTokenKey);
|
||||
var storage = const FlutterSecureStorage();
|
||||
var token = await storage.read(key: Token.loginAccessTokenKey);
|
||||
if (checkHeaderExclusionListOfAddedParameters(options.path)) {
|
||||
options.headers
|
||||
.putIfAbsent(HttpHeaders.authorizationHeader, () => 'Bearer $token');
|
||||
.putIfAbsent(HttpHeaders.authorizationHeader, () => "Bearer $token");
|
||||
}
|
||||
// options.headers['Authorization'] = 'Bearer ${'${token!}123'}';
|
||||
super.onRequest(options, handler);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> onError(
|
||||
DioException err, ErrorInterceptorHandler handler) async {
|
||||
final failure = ServerFailure.fromDioError(err);
|
||||
void onError(DioException err, ErrorInterceptorHandler handler) async {
|
||||
ServerFailure failure = ServerFailure.fromDioError(err);
|
||||
if (failure.toString().isNotEmpty) {
|
||||
CustomSnackBar.displaySnackBar(failure.toString());
|
||||
}
|
||||
const storage = FlutterSecureStorage();
|
||||
final token = await storage.read(key: Token.loginAccessTokenKey);
|
||||
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();
|
||||
}
|
||||
@ -74,10 +72,10 @@ class HTTPInterceptor extends InterceptorsWrapper {
|
||||
}
|
||||
}
|
||||
|
||||
bool checkHeaderExclusionListOfAddedParameters(String path) {
|
||||
var shouldAddHeader = true;
|
||||
checkHeaderExclusionListOfAddedParameters(String path) {
|
||||
bool shouldAddHeader = true;
|
||||
|
||||
for (final urlConstant in headerExclusionListOfAddedParameters) {
|
||||
for (var urlConstant in headerExclusionListOfAddedParameters) {
|
||||
if (path.contains(urlConstant)) {
|
||||
shouldAddHeader = false;
|
||||
}
|
||||
|
@ -8,10 +8,10 @@ class HTTPService {
|
||||
|
||||
// final navigatorKey = GlobalKey<NavigatorState>();
|
||||
|
||||
String certificateString = '';
|
||||
String certificateString = "";
|
||||
|
||||
static Dio setupDioClient() {
|
||||
final client = Dio(
|
||||
Dio client = Dio(
|
||||
BaseOptions(
|
||||
baseUrl: ApiEndpoints.baseUrl,
|
||||
receiveDataWhenStatusError: true,
|
||||
|
@ -17,34 +17,34 @@ class ServerFailure extends Failure {
|
||||
factory ServerFailure.fromDioError(DioException dioError) {
|
||||
switch (dioError.type) {
|
||||
case DioExceptionType.connectionTimeout:
|
||||
return ServerFailure('Connection timeout with the Server.');
|
||||
return ServerFailure("Connection timeout with the Server.");
|
||||
case DioExceptionType.sendTimeout:
|
||||
return ServerFailure('Send timeout with the Server.');
|
||||
return ServerFailure("Send timeout with the Server.");
|
||||
|
||||
case DioExceptionType.receiveTimeout:
|
||||
return ServerFailure('Receive timeout with the Server.');
|
||||
return ServerFailure("Receive timeout with the Server.");
|
||||
|
||||
case DioExceptionType.badCertificate:
|
||||
return ServerFailure('Bad certificate!');
|
||||
return ServerFailure("Bad certificate!");
|
||||
|
||||
case DioExceptionType.badResponse:
|
||||
{
|
||||
// var document = parser.parse(dioError.response!.data.toString());
|
||||
// var message = document.body!.text;
|
||||
return ServerFailure.fromResponse(dioError.response!.statusCode,
|
||||
dioError.response?.data['message'] ?? 'Error');
|
||||
return ServerFailure.fromResponse(dioError.response!.statusCode!,
|
||||
dioError.response?.data['message'] ?? "Error");
|
||||
}
|
||||
case DioExceptionType.cancel:
|
||||
return ServerFailure('The request to ApiServer was canceled');
|
||||
return ServerFailure("The request to ApiServer was canceled");
|
||||
|
||||
case DioExceptionType.connectionError:
|
||||
return ServerFailure('No Internet Connection');
|
||||
return ServerFailure("No Internet Connection");
|
||||
|
||||
case DioExceptionType.unknown:
|
||||
return ServerFailure('Unexpected Error, Please try again!');
|
||||
return ServerFailure("Unexpected Error, Please try again!");
|
||||
|
||||
default:
|
||||
return ServerFailure('Unexpected Error, Please try again!');
|
||||
return ServerFailure("Unexpected Error, Please try again!");
|
||||
}
|
||||
}
|
||||
|
||||
@ -54,9 +54,9 @@ class ServerFailure extends Failure {
|
||||
case 403:
|
||||
return ServerFailure(responseMessage);
|
||||
case 400:
|
||||
final errors = <String>[];
|
||||
List<String> errors = [];
|
||||
if (responseMessage is List) {
|
||||
for (final error in responseMessage) {
|
||||
for (var error in responseMessage) {
|
||||
errors.add(error);
|
||||
}
|
||||
} else {
|
||||
@ -64,11 +64,11 @@ class ServerFailure extends Failure {
|
||||
}
|
||||
return ServerFailure(errors.join('\n'));
|
||||
case 404:
|
||||
return ServerFailure('');
|
||||
return ServerFailure("");
|
||||
case 500:
|
||||
return ServerFailure(responseMessage);
|
||||
default:
|
||||
return ServerFailure('Opps there was an Error, Please try again!');
|
||||
return ServerFailure("Opps there was an Error, Please try again!");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user