|
|
|
|
@ -2,10 +2,11 @@ import 'dart:async';
|
|
|
|
|
import 'dart:convert';
|
|
|
|
|
import 'dart:io';
|
|
|
|
|
|
|
|
|
|
import 'package:car_customer_app/exceptions/api_exception.dart';
|
|
|
|
|
import 'package:flutter/foundation.dart';
|
|
|
|
|
import 'package:http/http.dart';
|
|
|
|
|
import 'package:http/io_client.dart';
|
|
|
|
|
import 'package:car_customer_app/exceptions/api_exception.dart';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
typedef FactoryConstructor<U> = U Function(dynamic);
|
|
|
|
|
|
|
|
|
|
@ -68,12 +69,14 @@ class ApiClient {
|
|
|
|
|
var response = await postJsonForResponse(url, jsonObject, token: token, queryParameters: queryParameters, headers: _headers, retryTimes: retryTimes);
|
|
|
|
|
try {
|
|
|
|
|
if (!kReleaseMode) {
|
|
|
|
|
print("res:"+response.body);
|
|
|
|
|
print("res121:" + response.body);
|
|
|
|
|
print("res121:" + response.statusCode.toString());
|
|
|
|
|
}
|
|
|
|
|
var jsonData = jsonDecode(response.body);
|
|
|
|
|
return factoryConstructor(jsonData);
|
|
|
|
|
} catch (ex) {
|
|
|
|
|
print("exception:"+response.body);
|
|
|
|
|
print(ex);
|
|
|
|
|
print("exception:" + ex.toString());
|
|
|
|
|
throw APIException(APIException.BAD_RESPONSE_FORMAT, arguments: ex);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
@ -92,7 +95,14 @@ class ApiClient {
|
|
|
|
|
return await _postForResponse(url, requestBody, token: token, queryParameters: queryParameters, headers: headers, retryTimes: retryTimes);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<Response> _postForResponse(String url, requestBody, {String? token, Map<String, dynamic>? queryParameters, Map<String, String>? headers, int retryTimes = 0}) async {
|
|
|
|
|
Future<Response> _postForResponse(
|
|
|
|
|
String url,
|
|
|
|
|
requestBody, {
|
|
|
|
|
String? token,
|
|
|
|
|
Map<String, dynamic>? queryParameters,
|
|
|
|
|
Map<String, String>? headers,
|
|
|
|
|
int retryTimes = 0,
|
|
|
|
|
}) async {
|
|
|
|
|
try {
|
|
|
|
|
var _headers = <String, String>{};
|
|
|
|
|
if (token != null) {
|
|
|
|
|
@ -107,12 +117,15 @@ class ApiClient {
|
|
|
|
|
var queryString = new Uri(queryParameters: queryParameters).query;
|
|
|
|
|
url = url + '?' + queryString;
|
|
|
|
|
}
|
|
|
|
|
var response = await _post(Uri.parse(url), body: requestBody, headers: _headers).timeout(Duration(seconds: 100));
|
|
|
|
|
if (!kReleaseMode) {
|
|
|
|
|
|
|
|
|
|
var response;
|
|
|
|
|
|
|
|
|
|
response = await _post(Uri.parse(url), body: requestBody, headers: _headers).timeout(Duration(seconds: 100));
|
|
|
|
|
|
|
|
|
|
if (!kReleaseMode) {
|
|
|
|
|
print("Url:$url");
|
|
|
|
|
print("body:$requestBody");
|
|
|
|
|
print("res: "+ response.body);
|
|
|
|
|
print("res: " + response.body);
|
|
|
|
|
}
|
|
|
|
|
if (response.statusCode >= 200 && response.statusCode < 500) {
|
|
|
|
|
return response;
|
|
|
|
|
@ -145,6 +158,9 @@ class ApiClient {
|
|
|
|
|
} else {
|
|
|
|
|
throw APIException(APIException.OTHER, arguments: e);
|
|
|
|
|
}
|
|
|
|
|
}catch (ex) {
|
|
|
|
|
print("exception1:" + ex.toString());
|
|
|
|
|
throw APIException(APIException.BAD_RESPONSE_FORMAT, arguments: ex);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ -161,4 +177,93 @@ class ApiClient {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<Response> _post(url, {Map<String, String>? headers, body, Encoding? encoding}) => _withClient((client) => client.post(url, headers: headers, body: body, encoding: encoding));
|
|
|
|
|
|
|
|
|
|
Future<U> getJsonForObject<T, U>(FactoryConstructor<U> factoryConstructor, String url,
|
|
|
|
|
{String? token, Map<String, dynamic>? queryParameters, Map<String, String>? headers, int retryTimes = 0}) async {
|
|
|
|
|
var _headers = {'Accept': 'application/json'};
|
|
|
|
|
if (headers != null && headers.isNotEmpty) {
|
|
|
|
|
_headers.addAll(headers);
|
|
|
|
|
}
|
|
|
|
|
var response = await getJsonForResponse(url, token: token, queryParameters: queryParameters, headers: _headers, retryTimes: retryTimes);
|
|
|
|
|
try {
|
|
|
|
|
if (!kReleaseMode) {
|
|
|
|
|
print("res:" + response.body);
|
|
|
|
|
}
|
|
|
|
|
var jsonData = jsonDecode(response.body);
|
|
|
|
|
return factoryConstructor(jsonData);
|
|
|
|
|
} catch (ex) {
|
|
|
|
|
print("exception:" + response.body);
|
|
|
|
|
throw APIException(APIException.BAD_RESPONSE_FORMAT, arguments: ex);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<Response> getJsonForResponse<T>(String url, {String? token, Map<String, dynamic>? queryParameters, Map<String, String>? headers, int retryTimes = 0}) async {
|
|
|
|
|
if (headers == null) {
|
|
|
|
|
headers = {'Content-Type': 'application/json'};
|
|
|
|
|
} else {
|
|
|
|
|
headers['Content-Type'] = 'application/json';
|
|
|
|
|
}
|
|
|
|
|
return await _getForResponse(url, token: token, queryParameters: queryParameters, headers: headers, retryTimes: retryTimes);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<Response> _getForResponse(String url, {String? token, Map<String, dynamic>? queryParameters, Map<String, String>? headers, int retryTimes = 0}) async {
|
|
|
|
|
try {
|
|
|
|
|
var _headers = <String, String>{};
|
|
|
|
|
if (token != null) {
|
|
|
|
|
_headers['Authorization'] = 'Bearer $token';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (headers != null && headers.isNotEmpty) {
|
|
|
|
|
_headers.addAll(headers);
|
|
|
|
|
}
|
|
|
|
|
if (queryParameters != null) {
|
|
|
|
|
String queryString = new Uri(queryParameters: queryParameters).query;
|
|
|
|
|
url = url + '?' + queryString.toString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!kReleaseMode) {
|
|
|
|
|
print("Url:$url");
|
|
|
|
|
print("queryParameters:$queryParameters");
|
|
|
|
|
}
|
|
|
|
|
var response = await _get(Uri.parse(url), headers: _headers).timeout(Duration(seconds: 60));
|
|
|
|
|
|
|
|
|
|
if (!kReleaseMode) {
|
|
|
|
|
print("res: " + response.body.toString());
|
|
|
|
|
}
|
|
|
|
|
if (response.statusCode >= 200 && response.statusCode < 300) {
|
|
|
|
|
return response;
|
|
|
|
|
} else {
|
|
|
|
|
throw _throwAPIException(response);
|
|
|
|
|
}
|
|
|
|
|
} on SocketException catch (e) {
|
|
|
|
|
if (retryTimes > 0) {
|
|
|
|
|
print('will retry after 3 seconds...');
|
|
|
|
|
await Future.delayed(Duration(seconds: 3));
|
|
|
|
|
return await _getForResponse(url, token: token, queryParameters: queryParameters, headers: headers, retryTimes: retryTimes - 1);
|
|
|
|
|
} else {
|
|
|
|
|
throw APIException(APIException.OTHER, arguments: e);
|
|
|
|
|
}
|
|
|
|
|
} on HttpException catch (e) {
|
|
|
|
|
if (retryTimes > 0) {
|
|
|
|
|
print('will retry after 3 seconds...');
|
|
|
|
|
await Future.delayed(Duration(seconds: 3));
|
|
|
|
|
return await _getForResponse(url, token: token, queryParameters: queryParameters, headers: headers, retryTimes: retryTimes - 1);
|
|
|
|
|
} else {
|
|
|
|
|
throw APIException(APIException.OTHER, arguments: e);
|
|
|
|
|
}
|
|
|
|
|
} on TimeoutException catch (e) {
|
|
|
|
|
throw APIException(APIException.TIMEOUT, arguments: e);
|
|
|
|
|
} on ClientException catch (e) {
|
|
|
|
|
if (retryTimes > 0) {
|
|
|
|
|
await Future.delayed(Duration(seconds: 3));
|
|
|
|
|
return await _getForResponse(url, token: token, queryParameters: queryParameters, headers: headers, retryTimes: retryTimes - 1);
|
|
|
|
|
} else {
|
|
|
|
|
throw APIException(APIException.OTHER, arguments: e);
|
|
|
|
|
}
|
|
|
|
|
} catch (e) {
|
|
|
|
|
throw APIException(APIException.OTHER, arguments: e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<Response> _get(url, {Map<String, String>? headers}) => _withClient((client) => client.get(url, headers: headers));
|
|
|
|
|
}
|
|
|
|
|
|