|
|
|
|
@ -39,7 +39,7 @@ APIException _throwAPIException(Response response) {
|
|
|
|
|
case 404:
|
|
|
|
|
APIError apiError;
|
|
|
|
|
if (response.body != null && response.body.isNotEmpty) {
|
|
|
|
|
// var jsonError = jsonDecode(response.body);
|
|
|
|
|
// var jsonError = jsonDecode(response.body);
|
|
|
|
|
apiError = APIError(404, response.body.toString());
|
|
|
|
|
}
|
|
|
|
|
return APIException(APIException.BAD_REQUEST, error: apiError);
|
|
|
|
|
@ -68,7 +68,7 @@ class ApiClient {
|
|
|
|
|
factory ApiClient() => _instance;
|
|
|
|
|
|
|
|
|
|
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}) async {
|
|
|
|
|
{String token, Map<String, dynamic> queryParameters, Map<String, String> headers, int retryTimes = 0, bool isPutRequest = false}) async {
|
|
|
|
|
var _headers = {'Accept': 'application/json'};
|
|
|
|
|
if (headers != null && headers.isNotEmpty) {
|
|
|
|
|
_headers.addAll(headers);
|
|
|
|
|
@ -77,7 +77,11 @@ class ApiClient {
|
|
|
|
|
print("Url:$url");
|
|
|
|
|
print("body:${jsonEncode(jsonObject)}");
|
|
|
|
|
}
|
|
|
|
|
var response = await postJsonForResponse(url, jsonObject, token: token, queryParameters: queryParameters, headers: _headers, retryTimes: retryTimes);
|
|
|
|
|
var response = await postJsonForResponse(url, jsonObject, token: token,
|
|
|
|
|
queryParameters: queryParameters,
|
|
|
|
|
headers: _headers,
|
|
|
|
|
retryTimes: retryTimes,
|
|
|
|
|
isPutRequest: isPutRequest);
|
|
|
|
|
try {
|
|
|
|
|
var jsonData = jsonDecode(response.body);
|
|
|
|
|
return factoryConstructor(jsonData);
|
|
|
|
|
@ -86,7 +90,8 @@ class ApiClient {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<Response> postJsonForResponse<T>(String url, T jsonObject, {String token, Map<String, dynamic> queryParameters, Map<String, String> headers, int retryTimes = 0}) async {
|
|
|
|
|
Future<Response> postJsonForResponse<T>(String url, T jsonObject,
|
|
|
|
|
{String token, Map<String, dynamic> queryParameters, Map<String, String> headers, int retryTimes = 0, bool isPutRequest = false}) async {
|
|
|
|
|
String requestBody;
|
|
|
|
|
if (jsonObject != null) {
|
|
|
|
|
requestBody = jsonEncode(jsonObject);
|
|
|
|
|
@ -97,10 +102,14 @@ class ApiClient {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return await _postForResponse(url, requestBody, token: token, queryParameters: queryParameters, headers: headers, retryTimes: retryTimes);
|
|
|
|
|
return await _postForResponse(url, requestBody, token: token,
|
|
|
|
|
queryParameters: queryParameters,
|
|
|
|
|
headers: headers,
|
|
|
|
|
retryTimes: retryTimes,
|
|
|
|
|
isPutRequest: isPutRequest);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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, bool isPutRequest = false}) async {
|
|
|
|
|
try {
|
|
|
|
|
var _headers = <String, String>{};
|
|
|
|
|
if (token != null) {
|
|
|
|
|
@ -115,7 +124,7 @@ 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(minutes: 2));
|
|
|
|
|
var response = await _post(Uri.parse(url), body: requestBody, headers: _headers, isPutRequest: isPutRequest).timeout(Duration(minutes: 2));
|
|
|
|
|
|
|
|
|
|
if (response.statusCode >= 200 && response.statusCode < 300) {
|
|
|
|
|
return response;
|
|
|
|
|
@ -126,7 +135,11 @@ class ApiClient {
|
|
|
|
|
if (retryTimes > 0) {
|
|
|
|
|
print('will retry after 3 seconds...');
|
|
|
|
|
await Future.delayed(Duration(seconds: 3));
|
|
|
|
|
return await _postForResponse(url, requestBody, token: token, queryParameters: queryParameters, headers: headers, retryTimes: retryTimes - 1);
|
|
|
|
|
return await _postForResponse(url, requestBody, token: token,
|
|
|
|
|
queryParameters: queryParameters,
|
|
|
|
|
headers: headers,
|
|
|
|
|
retryTimes: retryTimes - 1,
|
|
|
|
|
isPutRequest: isPutRequest);
|
|
|
|
|
} else {
|
|
|
|
|
throw APIException(APIException.OTHER, arguments: e);
|
|
|
|
|
}
|
|
|
|
|
@ -134,7 +147,11 @@ class ApiClient {
|
|
|
|
|
if (retryTimes > 0) {
|
|
|
|
|
print('will retry after 3 seconds...');
|
|
|
|
|
await Future.delayed(Duration(seconds: 3));
|
|
|
|
|
return await _postForResponse(url, requestBody, token: token, queryParameters: queryParameters, headers: headers, retryTimes: retryTimes - 1);
|
|
|
|
|
return await _postForResponse(url, requestBody, token: token,
|
|
|
|
|
queryParameters: queryParameters,
|
|
|
|
|
headers: headers,
|
|
|
|
|
retryTimes: retryTimes - 1,
|
|
|
|
|
isPutRequest: isPutRequest);
|
|
|
|
|
} else {
|
|
|
|
|
throw APIException(APIException.OTHER, arguments: e);
|
|
|
|
|
}
|
|
|
|
|
@ -144,7 +161,11 @@ class ApiClient {
|
|
|
|
|
if (retryTimes > 0) {
|
|
|
|
|
print('will retry after 3 seconds...');
|
|
|
|
|
await Future.delayed(Duration(seconds: 3));
|
|
|
|
|
return await _postForResponse(url, requestBody, token: token, queryParameters: queryParameters, headers: headers, retryTimes: retryTimes - 1);
|
|
|
|
|
return await _postForResponse(url, requestBody, token: token,
|
|
|
|
|
queryParameters: queryParameters,
|
|
|
|
|
headers: headers,
|
|
|
|
|
retryTimes: retryTimes - 1,
|
|
|
|
|
isPutRequest: isPutRequest);
|
|
|
|
|
} else {
|
|
|
|
|
throw APIException(APIException.OTHER, arguments: e);
|
|
|
|
|
}
|
|
|
|
|
@ -154,7 +175,8 @@ class ApiClient {
|
|
|
|
|
bool _certificateCheck(X509Certificate cert, String host, int port) => true;
|
|
|
|
|
|
|
|
|
|
Future<T> _withClient<T>(Future<T> Function(Client) fn) async {
|
|
|
|
|
var httpClient = HttpClient()..badCertificateCallback = _certificateCheck;
|
|
|
|
|
var httpClient = HttpClient()
|
|
|
|
|
..badCertificateCallback = _certificateCheck;
|
|
|
|
|
var client = IOClient(httpClient);
|
|
|
|
|
try {
|
|
|
|
|
return await fn(client);
|
|
|
|
|
@ -163,5 +185,12 @@ 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<Response> _post(url, {Map<String, String> headers, body, Encoding encoding, bool isPutRequest = false}) =>
|
|
|
|
|
_withClient((client) {
|
|
|
|
|
if (isPutRequest) {
|
|
|
|
|
return client.put(url, headers: headers, body: body, encoding: encoding);
|
|
|
|
|
} else {
|
|
|
|
|
return client.post(url, headers: headers, body: body, encoding: encoding);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|