|
|
|
@ -3,9 +3,9 @@ import 'dart:convert';
|
|
|
|
import 'dart:developer';
|
|
|
|
import 'dart:developer';
|
|
|
|
import 'dart:io';
|
|
|
|
import 'dart:io';
|
|
|
|
import 'package:flutter/foundation.dart';
|
|
|
|
import 'package:flutter/foundation.dart';
|
|
|
|
import 'package:http/http.dart';
|
|
|
|
import 'package:hmg_qline/services/logger_service.dart';
|
|
|
|
|
|
|
|
import 'package:http/http.dart' as http;
|
|
|
|
import 'package:http/io_client.dart';
|
|
|
|
import 'package:http/io_client.dart';
|
|
|
|
import 'package:hmg_qline/constants/app_constants.dart';
|
|
|
|
|
|
|
|
import 'package:hmg_qline/utilities/api_exception.dart';
|
|
|
|
import 'package:hmg_qline/utilities/api_exception.dart';
|
|
|
|
|
|
|
|
|
|
|
|
typedef FactoryConstructor<U> = U Function(dynamic);
|
|
|
|
typedef FactoryConstructor<U> = U Function(dynamic);
|
|
|
|
@ -14,14 +14,18 @@ abstract class ApiClient {
|
|
|
|
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});
|
|
|
|
{String? token, Map<String, dynamic>? queryParameters, Map<String, String>? headers, int retryTimes = 0});
|
|
|
|
|
|
|
|
|
|
|
|
Future<Response> postJsonForResponse<T>(String url, T jsonObject, {String? token, Map<String, dynamic>? queryParameters, Map<String, String>? headers, int retryTimes = 0});
|
|
|
|
Future<http.Response> postJsonForResponse<T>(String url, T jsonObject, {String? token, Map<String, dynamic>? queryParameters, Map<String, String>? headers, int retryTimes = 0});
|
|
|
|
|
|
|
|
|
|
|
|
Future<U> getJsonForObject<T, U>(FactoryConstructor<U> factoryConstructor, String url, {String? token, Map<String, dynamic>? queryParameters, Map<String, String>? headers, int retryTimes = 0});
|
|
|
|
Future<U> getJsonForObject<T, U>(FactoryConstructor<U> factoryConstructor, String url, {String? token, Map<String, dynamic>? queryParameters, Map<String, String>? headers, int retryTimes = 0});
|
|
|
|
|
|
|
|
|
|
|
|
Future<Response> getJsonForResponse<T>(String url, {String? token, Map<String, dynamic>? queryParameters, Map<String, String>? headers, int retryTimes = 0});
|
|
|
|
Future<http.Response> getJsonForResponse<T>(String url, {String? token, Map<String, dynamic>? queryParameters, Map<String, String>? headers, int retryTimes = 0});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class ApiClientImp implements ApiClient {
|
|
|
|
class ApiClientImp implements ApiClient {
|
|
|
|
|
|
|
|
LoggerService loggerService;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ApiClientImp({required this.loggerService});
|
|
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
@override
|
|
|
|
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}) async {
|
|
|
|
{String? token, Map<String, dynamic>? queryParameters, Map<String, String>? headers, int retryTimes = 0}) async {
|
|
|
|
@ -45,7 +49,7 @@ class ApiClientImp implements ApiClient {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
@override
|
|
|
|
Future<Response> postJsonForResponse<T>(String url, T jsonObject, {String? token, Map<String, dynamic>? queryParameters, Map<String, String>? headers, int retryTimes = 0}) async {
|
|
|
|
Future<http.Response> postJsonForResponse<T>(String url, T jsonObject, {String? token, Map<String, dynamic>? queryParameters, Map<String, String>? headers, int retryTimes = 0}) async {
|
|
|
|
String? requestBody;
|
|
|
|
String? requestBody;
|
|
|
|
if (jsonObject != null) {
|
|
|
|
if (jsonObject != null) {
|
|
|
|
requestBody = jsonEncode(jsonObject);
|
|
|
|
requestBody = jsonEncode(jsonObject);
|
|
|
|
@ -59,7 +63,7 @@ class ApiClientImp implements 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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Future<Response> _postForResponse(
|
|
|
|
Future<http.Response> _postForResponse(
|
|
|
|
String url,
|
|
|
|
String url,
|
|
|
|
requestBody, {
|
|
|
|
requestBody, {
|
|
|
|
String? token,
|
|
|
|
String? token,
|
|
|
|
@ -81,17 +85,17 @@ class ApiClientImp implements ApiClient {
|
|
|
|
var queryString = Uri(queryParameters: queryParameters).query;
|
|
|
|
var queryString = Uri(queryParameters: queryParameters).query;
|
|
|
|
url = '$url?$queryString';
|
|
|
|
url = '$url?$queryString';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Response response;
|
|
|
|
http.Response response;
|
|
|
|
|
|
|
|
|
|
|
|
response = await _post(Uri.parse(url), body: requestBody, headers: headers0).timeout(const Duration(seconds: 100));
|
|
|
|
response = await _post(Uri.parse(url), body: requestBody, headers: headers0).timeout(const Duration(seconds: 100));
|
|
|
|
|
|
|
|
|
|
|
|
if (!kReleaseMode) {
|
|
|
|
if (!kReleaseMode) {
|
|
|
|
logger.d("------URL------");
|
|
|
|
loggerService.logInfo("------URL------");
|
|
|
|
logger.i(url);
|
|
|
|
loggerService.logInfo(url);
|
|
|
|
logger.d("------Payload------");
|
|
|
|
loggerService.logInfo("------Payload------");
|
|
|
|
logger.i(jsonDecode(requestBody));
|
|
|
|
loggerService.logInfo(jsonDecode(requestBody).toString());
|
|
|
|
logger.d("------Response------");
|
|
|
|
loggerService.logInfo("------Response------");
|
|
|
|
logger.i(jsonDecode(response.body));
|
|
|
|
loggerService.logInfo(jsonDecode(response.body).toString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (response.statusCode >= 200 && response.statusCode < 500) {
|
|
|
|
if (response.statusCode >= 200 && response.statusCode < 500) {
|
|
|
|
var jsonData = jsonDecode(response.body);
|
|
|
|
var jsonData = jsonDecode(response.body);
|
|
|
|
@ -120,7 +124,7 @@ class ApiClientImp implements ApiClient {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} on TimeoutException catch (e) {
|
|
|
|
} on TimeoutException catch (e) {
|
|
|
|
throw APIException(APIException.timeout, arguments: e);
|
|
|
|
throw APIException(APIException.timeout, arguments: e);
|
|
|
|
} on ClientException catch (e) {
|
|
|
|
} on http.ClientException catch (e) {
|
|
|
|
if (retryTimes > 0) {
|
|
|
|
if (retryTimes > 0) {
|
|
|
|
log('will retry after 3 seconds...');
|
|
|
|
log('will retry after 3 seconds...');
|
|
|
|
await Future.delayed(const Duration(seconds: 3));
|
|
|
|
await Future.delayed(const Duration(seconds: 3));
|
|
|
|
@ -136,7 +140,7 @@ class ApiClientImp implements ApiClient {
|
|
|
|
|
|
|
|
|
|
|
|
bool _certificateCheck(X509Certificate cert, String host, int port) => true;
|
|
|
|
bool _certificateCheck(X509Certificate cert, String host, int port) => true;
|
|
|
|
|
|
|
|
|
|
|
|
Future<T> _withClient<T>(Future<T> Function(Client) fn) async {
|
|
|
|
Future<T> _withClient<T>(Future<T> Function(http.Client) fn) async {
|
|
|
|
var httpClient = HttpClient()..badCertificateCallback = _certificateCheck;
|
|
|
|
var httpClient = HttpClient()..badCertificateCallback = _certificateCheck;
|
|
|
|
var client = IOClient(httpClient);
|
|
|
|
var client = IOClient(httpClient);
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
@ -146,7 +150,7 @@ class ApiClientImp implements ApiClient {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Future<Response> _post(url, {Map<String, String>? headers, body, Encoding? encoding}) => _withClient((client) => client.post(url, headers: headers, body: body, encoding: encoding));
|
|
|
|
Future<http.Response> _post(url, {Map<String, String>? headers, body, Encoding? encoding}) => _withClient((client) => client.post(url, headers: headers, body: body, encoding: encoding));
|
|
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
@override
|
|
|
|
Future<U> getJsonForObject<T, U>(FactoryConstructor<U> factoryConstructor, String url,
|
|
|
|
Future<U> getJsonForObject<T, U>(FactoryConstructor<U> factoryConstructor, String url,
|
|
|
|
@ -166,7 +170,7 @@ class ApiClientImp implements ApiClient {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
@override
|
|
|
|
Future<Response> getJsonForResponse<T>(String url, {String? token, Map<String, dynamic>? queryParameters, Map<String, String>? headers, int retryTimes = 0}) async {
|
|
|
|
Future<http.Response> getJsonForResponse<T>(String url, {String? token, Map<String, dynamic>? queryParameters, Map<String, String>? headers, int retryTimes = 0}) async {
|
|
|
|
if (headers == null) {
|
|
|
|
if (headers == null) {
|
|
|
|
headers = {'Content-Type': 'application/json'};
|
|
|
|
headers = {'Content-Type': 'application/json'};
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
@ -177,7 +181,7 @@ class ApiClientImp implements ApiClient {
|
|
|
|
|
|
|
|
|
|
|
|
bool isFirstCall = true;
|
|
|
|
bool isFirstCall = true;
|
|
|
|
|
|
|
|
|
|
|
|
Future<Response> _getForResponse(String url, {String? token, Map<String, dynamic>? queryParameters, Map<String, String>? headers, int retryTimes = 0}) async {
|
|
|
|
Future<http.Response> _getForResponse(String url, {String? token, Map<String, dynamic>? queryParameters, Map<String, String>? headers, int retryTimes = 0}) async {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
var headers0 = <String, String>{};
|
|
|
|
var headers0 = <String, String>{};
|
|
|
|
if (token != null) {
|
|
|
|
if (token != null) {
|
|
|
|
@ -193,13 +197,13 @@ class ApiClientImp implements ApiClient {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (!kReleaseMode) {
|
|
|
|
if (!kReleaseMode) {
|
|
|
|
logger.i(url);
|
|
|
|
loggerService.logInfo(url);
|
|
|
|
logger.i("$queryParameters");
|
|
|
|
loggerService.logInfo("$queryParameters");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var response = await _get(Uri.parse(url), headers: headers0).timeout(const Duration(seconds: 60));
|
|
|
|
var response = await _get(Uri.parse(url), headers: headers0).timeout(const Duration(seconds: 60));
|
|
|
|
|
|
|
|
|
|
|
|
if (!kReleaseMode) {
|
|
|
|
if (!kReleaseMode) {
|
|
|
|
logger.i(jsonDecode(response.body));
|
|
|
|
loggerService.logInfo(jsonDecode(response.body));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (response.statusCode >= 200 && response.statusCode < 500) {
|
|
|
|
if (response.statusCode >= 200 && response.statusCode < 500) {
|
|
|
|
var jsonData = jsonDecode(response.body);
|
|
|
|
var jsonData = jsonDecode(response.body);
|
|
|
|
@ -229,7 +233,7 @@ class ApiClientImp implements ApiClient {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} on TimeoutException catch (e) {
|
|
|
|
} on TimeoutException catch (e) {
|
|
|
|
throw APIException(APIException.timeout, arguments: e);
|
|
|
|
throw APIException(APIException.timeout, arguments: e);
|
|
|
|
} on ClientException catch (e) {
|
|
|
|
} on http.ClientException catch (e) {
|
|
|
|
if (retryTimes > 0) {
|
|
|
|
if (retryTimes > 0) {
|
|
|
|
await Future.delayed(const Duration(seconds: 3));
|
|
|
|
await Future.delayed(const Duration(seconds: 3));
|
|
|
|
return await _getForResponse(url, token: token, queryParameters: queryParameters, headers: headers, retryTimes: retryTimes - 1);
|
|
|
|
return await _getForResponse(url, token: token, queryParameters: queryParameters, headers: headers, retryTimes: retryTimes - 1);
|
|
|
|
@ -239,5 +243,5 @@ class ApiClientImp implements ApiClient {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Future<Response> _get(url, {Map<String, String>? headers}) => _withClient((client) => client.get(url, headers: headers));
|
|
|
|
Future<http.Response> _get(url, {Map<String, String>? headers}) => _withClient((client) => client.get(url, headers: headers));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|