|
|
|
@ -16,15 +16,11 @@ class APIError {
|
|
|
|
int? errorCode;
|
|
|
|
int? errorCode;
|
|
|
|
int? errorType;
|
|
|
|
int? errorType;
|
|
|
|
String? errorMessage;
|
|
|
|
String? errorMessage;
|
|
|
|
int? errorStatusCode;
|
|
|
|
int? errorStatusCode;
|
|
|
|
|
|
|
|
|
|
|
|
APIError(this.errorCode, this.errorMessage, this.errorType, this.errorStatusCode);
|
|
|
|
APIError(this.errorCode, this.errorMessage, this.errorType, this.errorStatusCode);
|
|
|
|
|
|
|
|
|
|
|
|
Map<String, dynamic> toJson() => {
|
|
|
|
Map<String, dynamic> toJson() => {'errorCode': errorCode, 'errorMessage': errorMessage, 'errorType': errorType, 'ErrorStatusCode': errorStatusCode};
|
|
|
|
'errorCode': errorCode,
|
|
|
|
|
|
|
|
'errorMessage': errorMessage,
|
|
|
|
|
|
|
|
'errorType': errorType,
|
|
|
|
|
|
|
|
'ErrorStatusCode': errorStatusCode
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
@override
|
|
|
|
String toString() {
|
|
|
|
String toString() {
|
|
|
|
@ -46,7 +42,7 @@ APIException _throwAPIException(Response response) {
|
|
|
|
APIError? apiError;
|
|
|
|
APIError? apiError;
|
|
|
|
if (response.body != null && response.body.isNotEmpty) {
|
|
|
|
if (response.body != null && response.body.isNotEmpty) {
|
|
|
|
var jsonError = jsonDecode(response.body);
|
|
|
|
var jsonError = jsonDecode(response.body);
|
|
|
|
apiError = APIError(jsonError['ErrorCode'], jsonError['ErrorMessage'], jsonError['ErrorType'],jsonError['ErrorStatusCode']);
|
|
|
|
apiError = APIError(jsonError['ErrorCode'], jsonError['ErrorMessage'], jsonError['ErrorType'], jsonError['ErrorStatusCode']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return APIException(APIException.BAD_REQUEST, error: apiError);
|
|
|
|
return APIException(APIException.BAD_REQUEST, error: apiError);
|
|
|
|
case 401:
|
|
|
|
case 401:
|
|
|
|
@ -73,7 +69,7 @@ class ApiClient {
|
|
|
|
factory ApiClient() => _instance;
|
|
|
|
factory ApiClient() => _instance;
|
|
|
|
|
|
|
|
|
|
|
|
Future<U> postJsonForObject<T, U>(FactoryConstructor<U> factoryConstructor, String url, T jsonObject,
|
|
|
|
Future<U> postJsonForObject<T, U>(FactoryConstructor<U> factoryConstructor, String url, T jsonObject,
|
|
|
|
{String? token, Map<String, dynamic>? queryParameters, Map<String, String>? headers, int retryTimes = 0, bool isFormData = false}) async {
|
|
|
|
{String? token, Map<String, dynamic>? queryParameters, Map<String, String>? headers, int retryTimes = 0, bool isFormData = false, bool returnJsonData = true}) async {
|
|
|
|
var _headers = {'Accept': 'application/json'};
|
|
|
|
var _headers = {'Accept': 'application/json'};
|
|
|
|
if (headers != null && headers.isNotEmpty) {
|
|
|
|
if (headers != null && headers.isNotEmpty) {
|
|
|
|
_headers.addAll(headers);
|
|
|
|
_headers.addAll(headers);
|
|
|
|
@ -84,22 +80,22 @@ class ApiClient {
|
|
|
|
print("body:$bodyJson");
|
|
|
|
print("body:$bodyJson");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var response = await postJsonForResponse(url, jsonObject, token: token, queryParameters: queryParameters, headers: _headers, retryTimes: retryTimes, isFormData: isFormData);
|
|
|
|
var response = await postJsonForResponse(url, jsonObject, token: token, queryParameters: queryParameters, headers: _headers, retryTimes: retryTimes, isFormData: isFormData);
|
|
|
|
// try {
|
|
|
|
// try {
|
|
|
|
if (!kReleaseMode) {
|
|
|
|
if (!kReleaseMode) {
|
|
|
|
logger.i("res: " + response.body);
|
|
|
|
logger.i("res: " + response.body);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var jsonData = jsonDecode(response.body);
|
|
|
|
var jsonData = jsonDecode(response.body);
|
|
|
|
if (jsonData["IsAuthenticated"] != null) {
|
|
|
|
if (jsonData["IsAuthenticated"] != null) {
|
|
|
|
AppState().setIsAuthenticated = jsonData["IsAuthenticated"];
|
|
|
|
AppState().setIsAuthenticated = jsonData["IsAuthenticated"];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (jsonData["ErrorMessage"] == null) {
|
|
|
|
if (jsonData["ErrorMessage"] == null) {
|
|
|
|
return factoryConstructor(jsonData["Data"]);
|
|
|
|
return factoryConstructor(returnJsonData ? jsonData["Data"] : jsonData);
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
APIError? apiError;
|
|
|
|
APIError? apiError;
|
|
|
|
apiError = APIError(jsonData['ErrorCode'], jsonData['ErrorEndUserMessage'], jsonData['ErrorType'] ?? 0, jsonData['ErrorStatusCode']);
|
|
|
|
apiError = APIError(jsonData['ErrorCode'], jsonData['ErrorEndUserMessage'], jsonData['ErrorType'] ?? 0, jsonData['ErrorStatusCode']);
|
|
|
|
throw APIException(APIException.BAD_REQUEST, error: apiError);
|
|
|
|
throw APIException(APIException.BAD_REQUEST, error: apiError);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// } catch (ex) {
|
|
|
|
// } catch (ex) {
|
|
|
|
// if (ex is APIException) {
|
|
|
|
// if (ex is APIException) {
|
|
|
|
// rethrow;
|
|
|
|
// rethrow;
|
|
|
|
@ -147,7 +143,7 @@ class ApiClient {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var response = await _post(Uri.parse(url), body: requestBody, headers: _headers).timeout(Duration(seconds: 120));
|
|
|
|
var response = await _post(Uri.parse(url), body: requestBody, headers: _headers).timeout(Duration(seconds: 120));
|
|
|
|
|
|
|
|
|
|
|
|
if (response. statusCode >= 200 && response.statusCode < 300) {
|
|
|
|
if (response.statusCode >= 200 && response.statusCode < 300) {
|
|
|
|
return response;
|
|
|
|
return response;
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
throw _throwAPIException(response);
|
|
|
|
throw _throwAPIException(response);
|
|
|
|
|