mirror of
https://github.com/SyncrowIOT/syncrow-app.git
synced 2025-11-26 17:54:54 +00:00
Added Error handling
This commit is contained in:
@ -35,106 +35,4 @@ class AuthenticationAPI {
|
||||
debugPrint("response: $response");
|
||||
return response;
|
||||
}
|
||||
|
||||
// static Future<SuccessResponse> updateUserInfo(
|
||||
// Map<String, dynamic> data) async {
|
||||
// final response = await HTTPService().postRequest(
|
||||
// path: APIConstants.updateUserInfo,
|
||||
// body: data,
|
||||
// expectedResponseModel: (json) {
|
||||
// SuccessResponse token = SuccessResponse.fromJson(json);
|
||||
// return token;
|
||||
// });
|
||||
// return response;
|
||||
// }
|
||||
//
|
||||
// static Future<LoginWithPhoneResponse> loginWithPhone(
|
||||
// {required LoginWithPhone data, String? recaptchaToken}) async {
|
||||
// final response = await HTTPService().postRequest(
|
||||
// path: APIConstants.loginWithChannel,
|
||||
// body: data.toJson(),
|
||||
// options: Options(headers: {
|
||||
// 'captcha-token': recaptchaToken,
|
||||
// 'captcha-site-key': Platform.isAndroid
|
||||
// ? dotenv.env['RECAPTCH_ANDROID_SITE_KEY'] ?? ''
|
||||
// : dotenv.env['RECAPTCH_IOS_SITE_KEY'] ?? ''
|
||||
// }),
|
||||
// expectedResponseModel: (json) {
|
||||
// LoginWithPhoneResponse result = LoginWithPhoneResponse.fromJson(json);
|
||||
// return result;
|
||||
// });
|
||||
// return response;
|
||||
// }
|
||||
//
|
||||
// static Future<List<dynamic>> countryCodes() async {
|
||||
// final response = await HTTPService().postRequest(
|
||||
// path: APIConstants.getCountyCode,
|
||||
// expectedResponseModel: (json) {
|
||||
// List<dynamic> result = json.toList();
|
||||
// return result;
|
||||
// });
|
||||
// return response;
|
||||
// }
|
||||
//
|
||||
// static Future<bool> sendNotificationToken({
|
||||
// required Map<String, String> data,
|
||||
// }) async {
|
||||
// final response = await HTTPService().postRequest(
|
||||
// path: APIConstants.notificationToken,
|
||||
// body: data,
|
||||
// showServerMessage: false,
|
||||
// expectedResponseModel: (json) {
|
||||
// bool checked = false;
|
||||
// if (json != null) {
|
||||
// if (json['success']) {
|
||||
// checked = true;
|
||||
// }
|
||||
// }
|
||||
// return checked;
|
||||
// });
|
||||
// return response;
|
||||
// }
|
||||
//
|
||||
// static Future<bool> logout() async {
|
||||
// final response = await HTTPService().postRequest(
|
||||
// path: APIConstants.logout,
|
||||
// expectedResponseModel: (json) {
|
||||
// bool checked = false;
|
||||
// // print(json);
|
||||
// if (json != null) {
|
||||
// if (json['success']) {
|
||||
// checked = true;
|
||||
// }
|
||||
// }
|
||||
// return checked;
|
||||
// });
|
||||
// return response;
|
||||
// }
|
||||
//
|
||||
// static Future<bool> deleteAccount() async {
|
||||
// final response = await HTTPService().postRequest(
|
||||
// path: APIConstants.deleteAccount,
|
||||
// expectedResponseModel: (json) {
|
||||
// bool checked = false;
|
||||
// if (json != null) {
|
||||
// if (json['success']) {
|
||||
// checked = true;
|
||||
// }
|
||||
// }
|
||||
// return checked;
|
||||
// });
|
||||
// return response;
|
||||
// }
|
||||
|
||||
// static Future<Token> refreshToken(Map<String, dynamic> data) async {
|
||||
// final response = await HTTPService().postRequest(
|
||||
// path: APIConstants.refreshToken,
|
||||
// showServerMessage: false,
|
||||
// body: data,
|
||||
// expectedResponseModel: (json) {
|
||||
// Token token = Token.fromJson(json);
|
||||
// return token;
|
||||
// });
|
||||
// return response;
|
||||
// }
|
||||
}
|
||||
|
||||
@ -2,23 +2,24 @@ import 'package:dio/dio.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:syncrow_app/services/api/api_links_endpoints.dart';
|
||||
import 'package:syncrow_app/services/api/http_interceptor.dart';
|
||||
import 'package:syncrow_app/services/api/network_exception.dart';
|
||||
import 'package:syncrow_app/services/locator.dart';
|
||||
|
||||
class HTTPService {
|
||||
late Dio client = serviceLocator.get<Dio>();
|
||||
final Dio client = serviceLocator.get<Dio>();
|
||||
|
||||
// final navigatorKey = GlobalKey<NavigatorState>();
|
||||
|
||||
String certificateString = "";
|
||||
|
||||
Dio setupDioClient() {
|
||||
client = Dio(
|
||||
static Dio setupDioClient() {
|
||||
Dio client = Dio(
|
||||
BaseOptions(
|
||||
baseUrl: ApiEndpoints.baseUrl,
|
||||
receiveDataWhenStatusError: true,
|
||||
followRedirects: false,
|
||||
connectTimeout: const Duration(minutes: 1),
|
||||
receiveTimeout: const Duration(minutes: 1),
|
||||
connectTimeout: const Duration(seconds: 60),
|
||||
receiveTimeout: const Duration(seconds: 60),
|
||||
),
|
||||
);
|
||||
|
||||
@ -53,16 +54,17 @@ class HTTPService {
|
||||
bool showServerMessage = true,
|
||||
required T Function(dynamic) expectedResponseModel}) async {
|
||||
try {
|
||||
final response = await client.post(path,
|
||||
data: body, queryParameters: queryParameters, options: options);
|
||||
print("post response is $response");
|
||||
final response = await client.post(
|
||||
path,
|
||||
data: body,
|
||||
queryParameters: queryParameters,
|
||||
options: options,
|
||||
);
|
||||
debugPrint("status code is ${response.statusCode}");
|
||||
debugPrint("response data is ${response.data}");
|
||||
return expectedResponseModel(response.data);
|
||||
} catch (error) {
|
||||
debugPrint("******* Error ********");
|
||||
debugPrint(error.toString());
|
||||
rethrow;
|
||||
} on DioException catch (error) {
|
||||
throw ServerFailure.fromDioError(error);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -17,12 +17,12 @@ class ServerFailure extends Failure {
|
||||
factory ServerFailure.fromDioError(DioException dioError) {
|
||||
switch (dioError.type) {
|
||||
case DioExceptionType.connectionTimeout:
|
||||
return ServerFailure("Connection timeout with ApiServer.");
|
||||
return ServerFailure("Connection timeout with the Server.");
|
||||
case DioExceptionType.sendTimeout:
|
||||
return ServerFailure("Send timeout with ApiServer.");
|
||||
return ServerFailure("Send timeout with the Server.");
|
||||
|
||||
case DioExceptionType.receiveTimeout:
|
||||
return ServerFailure("Receive timeout with ApiServer.");
|
||||
return ServerFailure("Receive timeout with the Server.");
|
||||
|
||||
case DioExceptionType.badCertificate:
|
||||
return ServerFailure("Bad certificate!");
|
||||
|
||||
Reference in New Issue
Block a user