|
|
|
|
@ -2,11 +2,12 @@ import 'dart:async';
|
|
|
|
|
import 'dart:convert';
|
|
|
|
|
import 'dart:developer';
|
|
|
|
|
import 'dart:io';
|
|
|
|
|
|
|
|
|
|
import 'package:flutter/foundation.dart';
|
|
|
|
|
import 'package:hmg_qline/services/logger_service.dart';
|
|
|
|
|
import 'package:hmg_qline/utilities/api_exception.dart';
|
|
|
|
|
import 'package:http/http.dart' as http;
|
|
|
|
|
import 'package:http/io_client.dart';
|
|
|
|
|
import 'package:hmg_qline/utilities/api_exception.dart';
|
|
|
|
|
|
|
|
|
|
typedef FactoryConstructor<U> = U Function(dynamic);
|
|
|
|
|
|
|
|
|
|
@ -14,11 +15,14 @@ abstract class ApiClient {
|
|
|
|
|
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});
|
|
|
|
|
|
|
|
|
|
Future<http.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<http.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 {
|
|
|
|
|
@ -34,7 +38,8 @@ class ApiClientImp implements ApiClient {
|
|
|
|
|
headers0.addAll(headers);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var response = await postJsonForResponse(url, jsonObject, token: token, queryParameters: queryParameters, headers: headers0, retryTimes: retryTimes);
|
|
|
|
|
var response =
|
|
|
|
|
await postJsonForResponse(url, jsonObject, token: token, queryParameters: queryParameters, headers: headers0, retryTimes: retryTimes);
|
|
|
|
|
try {
|
|
|
|
|
if (!kReleaseMode) {
|
|
|
|
|
log("statusCode:${response.statusCode}");
|
|
|
|
|
@ -49,7 +54,8 @@ class ApiClientImp implements ApiClient {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
Future<http.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;
|
|
|
|
|
if (jsonObject != null) {
|
|
|
|
|
requestBody = jsonEncode(jsonObject);
|
|
|
|
|
@ -96,7 +102,9 @@ class ApiClientImp implements ApiClient {
|
|
|
|
|
loggerService.logInfo(jsonDecode(requestBody).toString());
|
|
|
|
|
loggerService.logInfo("------Response------");
|
|
|
|
|
loggerService.logInfo(jsonDecode(response.body).toString());
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (response.statusCode >= 200 && response.statusCode < 500) {
|
|
|
|
|
var jsonData = jsonDecode(response.body);
|
|
|
|
|
if (jsonData["StatusMessage"] != null && jsonData["StatusMessage"] == "Unauthorized user attempt to access API") {
|
|
|
|
|
@ -150,7 +158,8 @@ class ApiClientImp implements ApiClient {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<http.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
|
|
|
|
|
Future<U> getJsonForObject<T, U>(FactoryConstructor<U> factoryConstructor, String url,
|
|
|
|
|
@ -170,7 +179,8 @@ class ApiClientImp implements ApiClient {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
Future<http.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) {
|
|
|
|
|
headers = {'Content-Type': 'application/json'};
|
|
|
|
|
} else {
|
|
|
|
|
@ -181,7 +191,8 @@ class ApiClientImp implements ApiClient {
|
|
|
|
|
|
|
|
|
|
bool isFirstCall = true;
|
|
|
|
|
|
|
|
|
|
Future<http.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 {
|
|
|
|
|
var headers0 = <String, String>{};
|
|
|
|
|
if (token != null) {
|
|
|
|
|
|