final
This commit is contained in:
@@ -7,14 +7,12 @@ import 'package:crypto/crypto.dart';
|
||||
import 'package:dio/dio.dart';
|
||||
import '../store/store.dart';
|
||||
import 'utils.dart';
|
||||
import 'package:hive/hive.dart';
|
||||
import 'package:universal_io/io.dart';
|
||||
|
||||
import '../constants.dart';
|
||||
|
||||
typedef void PostCallback(Response response);
|
||||
|
||||
String generateSignature(String string, {String key}) {
|
||||
String generateSignature(String string, {String? key}) {
|
||||
if (key == null) {
|
||||
key = Constants.API_SECRET;
|
||||
}
|
||||
@@ -37,17 +35,17 @@ class HttpUtil {
|
||||
'Http-Contact-Authorization': '',
|
||||
'Http-Device-Type': Utils.getOs(checkWeb: true),
|
||||
'Http-Api-Branch': 'flutter',
|
||||
'Http-Language-Code': store.state.locale.languageCode,
|
||||
'Http-Country-Code': store.state.locale.countryCode,
|
||||
'Http-Language-Code': (store == null || store.state == null || store.state.locale == null) ? 'en' : store.state.locale!.languageCode,
|
||||
'Http-Country-Code': (store == null || store.state == null || store.state.locale == null) ? 'US' : store.state.locale!.countryCode!,
|
||||
};
|
||||
|
||||
static Future<dynamic> httpGet(String url,
|
||||
{
|
||||
Map<String, dynamic> queryParameters,
|
||||
Map<String, dynamic>? queryParameters,
|
||||
int businessId = 0,
|
||||
Function(int, int) receiveProgress,
|
||||
Function(int, int)? receiveProgress,
|
||||
bool returnError = false,
|
||||
Map<String, String> additionalHeaders,
|
||||
Map<String, String>? additionalHeaders,
|
||||
}) async {
|
||||
Map<String, dynamic> localHeaders = json.decode(json.encode(headers));
|
||||
if (additionalHeaders != null) {
|
||||
@@ -59,8 +57,7 @@ class HttpUtil {
|
||||
}
|
||||
|
||||
localHeaders['Http-Business-Id'] = businessId == 0 ? '${Constants.BUSINESS_ID}' : businessId.toString();
|
||||
Box box = await Utils.getBox();
|
||||
localHeaders['Http-Contact-Authorization'] = box.get(Constants.KEY_ACCESS_TOKEN, defaultValue: '');
|
||||
localHeaders['Http-Contact-Authorization'] = Utils.prefs?.getString(Constants.KEY_ACCESS_TOKEN) ?? '';
|
||||
localHeaders['Http-App-Key'] = platformName;
|
||||
localHeaders['Http-Device-Type'] = platformName;
|
||||
String requestUrl = Constants.BASE_API_URL + url;
|
||||
@@ -69,7 +66,9 @@ class HttpUtil {
|
||||
requestUrl = url;
|
||||
}
|
||||
|
||||
Utils.jsonPrettyPrint(queryParameters);
|
||||
if (queryParameters != null) {
|
||||
Utils.jsonPrettyPrint(queryParameters!);
|
||||
}
|
||||
|
||||
Dio dio = Dio();
|
||||
try {
|
||||
@@ -84,9 +83,9 @@ class HttpUtil {
|
||||
// print('response data: ${response.data}');
|
||||
// Utils.jsonPrettyPrint(response.data);
|
||||
|
||||
int statusCode = response.statusCode;
|
||||
int statusCode = response.statusCode!;
|
||||
return response.data;
|
||||
} on DioError catch(e) {
|
||||
} on DioException catch(e) {
|
||||
print('HttpGet failed, request: $requestUrl, headers: $localHeaders, error ${e.response}');
|
||||
if (returnError) {
|
||||
return e;
|
||||
@@ -95,16 +94,16 @@ class HttpUtil {
|
||||
}
|
||||
}
|
||||
|
||||
static Future<dynamic> httpPost(String url, PostCallback callback,
|
||||
static Future<dynamic> httpPost(String url, PostCallback? callback,
|
||||
{
|
||||
Map<String, dynamic> queryParameters,
|
||||
Map<String, dynamic>? queryParameters,
|
||||
int businessId = 0,
|
||||
bool isFormData = false,
|
||||
Map<String, String> additionalHeaders,
|
||||
Map<String, dynamic> body,
|
||||
FormData formData,
|
||||
Function(int, int) sendProgress,
|
||||
Function(int, int) receiveProgress,
|
||||
Map<String, String>? additionalHeaders,
|
||||
Map<String, dynamic>? body,
|
||||
FormData? formData,
|
||||
Function(int, int)? sendProgress,
|
||||
Function(int, int)? receiveProgress,
|
||||
bool returnError = false,
|
||||
}) async {
|
||||
|
||||
@@ -116,8 +115,7 @@ class HttpUtil {
|
||||
localHeaders['Http-Signature'] = generateSignature(url);
|
||||
}
|
||||
localHeaders['Http-Business-Id'] = businessId == 0 ? '${Constants.BUSINESS_ID}' : businessId.toString();
|
||||
Box box = await Utils.getBox();
|
||||
localHeaders['Http-Contact-Authorization'] = box.get(Constants.KEY_ACCESS_TOKEN, defaultValue: '');
|
||||
localHeaders['Http-Contact-Authorization'] = Utils.prefs?.getString(Constants.KEY_ACCESS_TOKEN) ?? '';
|
||||
localHeaders['Http-App-Key'] = platformName;
|
||||
localHeaders['Http-Device-Type'] = platformName;
|
||||
|
||||
@@ -138,7 +136,7 @@ class HttpUtil {
|
||||
Response response = await dio.post(
|
||||
requestUrl,
|
||||
queryParameters: queryParameters == null ? {} : queryParameters,
|
||||
data: isFormData ? ((formData != null)?formData:FormData.fromMap(body)) : json.encode(body),
|
||||
data: isFormData ? ((formData != null)?formData:FormData.fromMap(body!)) : json.encode(body),
|
||||
options: Options(
|
||||
headers: localHeaders,
|
||||
contentType: isFormData ? Headers.formUrlEncodedContentType : Headers.jsonContentType,
|
||||
@@ -149,17 +147,17 @@ class HttpUtil {
|
||||
|
||||
// Utils.jsonPrettyPrint(response.data);
|
||||
|
||||
int statusCode = response.statusCode;
|
||||
int statusCode = response.statusCode!;
|
||||
if (callback != null) {
|
||||
callback(response);
|
||||
}
|
||||
return response.data;
|
||||
} on DioError catch(e, stacktrace) {
|
||||
} on DioException catch(e, stacktrace) {
|
||||
if (e.response != null) {
|
||||
try {
|
||||
Utils.jsonPrettyPrint(e.response.data);
|
||||
Utils.jsonPrettyPrint(e.response?.data);
|
||||
} catch (err) {
|
||||
print(e.response.data);
|
||||
print(e.response?.data);
|
||||
}
|
||||
}
|
||||
if (returnError) {
|
||||
@@ -169,14 +167,14 @@ class HttpUtil {
|
||||
}
|
||||
}
|
||||
|
||||
static Future<dynamic> httpPut(String url, PostCallback callback,
|
||||
static Future<dynamic> httpPut(String url, PostCallback? callback,
|
||||
{
|
||||
Map<String, dynamic> queryParameters,
|
||||
Map<String, dynamic>? queryParameters,
|
||||
int businessId = 0,
|
||||
Map<String, String> additionalHeaders,
|
||||
Map<String, dynamic> body,
|
||||
Function(int, int) sendProgress,
|
||||
Function(int, int) receiveProgress,
|
||||
Map<String, String>? additionalHeaders,
|
||||
Map<String, dynamic>? body,
|
||||
Function(int, int)? sendProgress,
|
||||
Function(int, int)? receiveProgress,
|
||||
bool returnError = false,
|
||||
}) async {
|
||||
|
||||
@@ -190,8 +188,7 @@ class HttpUtil {
|
||||
}
|
||||
|
||||
localHeaders['Http-Business-Id'] = businessId == 0 ? '${Constants.BUSINESS_ID}' : businessId.toString();
|
||||
Box box = await Utils.getBox();
|
||||
localHeaders['Http-Contact-Authorization'] = box.get(Constants.KEY_ACCESS_TOKEN, defaultValue: '');
|
||||
localHeaders['Http-Contact-Authorization'] = Utils.prefs?.getString(Constants.KEY_ACCESS_TOKEN) ?? '';
|
||||
localHeaders['Http-App-Key'] = platformName;
|
||||
localHeaders['Http-Device-Type'] = platformName;
|
||||
|
||||
@@ -218,14 +215,14 @@ class HttpUtil {
|
||||
|
||||
// Utils.jsonPrettyPrint(response.data);
|
||||
|
||||
int statusCode = response.statusCode;
|
||||
int statusCode = response.statusCode!;
|
||||
if (callback != null) {
|
||||
callback(response);
|
||||
}
|
||||
return response.data;
|
||||
} on DioError catch(e) {
|
||||
} on DioException catch(e) {
|
||||
if (e.response != null) {
|
||||
Utils.jsonPrettyPrint(e.response.data);
|
||||
Utils.jsonPrettyPrint(e.response!.data);
|
||||
}
|
||||
if (returnError) {
|
||||
return e;
|
||||
@@ -234,14 +231,14 @@ class HttpUtil {
|
||||
}
|
||||
}
|
||||
|
||||
static Future<dynamic> httpPatch(String url, PostCallback callback,
|
||||
static Future<dynamic> httpPatch(String url, PostCallback? callback,
|
||||
{
|
||||
Map<String, dynamic> queryParameters,
|
||||
Map<String, dynamic>? queryParameters,
|
||||
int businessId = 0,
|
||||
Map<String, String> additionalHeaders,
|
||||
Map<String, dynamic> body,
|
||||
Function(int, int) sendProgress,
|
||||
Function(int, int) receiveProgress,
|
||||
Map<String, String>? additionalHeaders,
|
||||
Map<String, dynamic>? body,
|
||||
Function(int, int)? sendProgress,
|
||||
Function(int, int)? receiveProgress,
|
||||
bool returnError = false,
|
||||
}) async {
|
||||
|
||||
@@ -255,8 +252,7 @@ class HttpUtil {
|
||||
}
|
||||
|
||||
localHeaders['Http-Business-Id'] = businessId == 0 ? '${Constants.BUSINESS_ID}' : businessId.toString();
|
||||
Box box = await Utils.getBox();
|
||||
localHeaders['Http-Contact-Authorization'] = box.get(Constants.KEY_ACCESS_TOKEN, defaultValue: '');
|
||||
localHeaders['Http-Contact-Authorization'] = Utils.prefs?.getString(Constants.KEY_ACCESS_TOKEN) ?? '';
|
||||
localHeaders['Http-App-Key'] = platformName;
|
||||
localHeaders['Http-Device-Type'] = platformName;
|
||||
|
||||
@@ -268,7 +264,9 @@ class HttpUtil {
|
||||
requestUrl = url;
|
||||
}
|
||||
|
||||
Utils.jsonPrettyPrint(body);
|
||||
if (body != null) {
|
||||
Utils.jsonPrettyPrint(body!);
|
||||
}
|
||||
|
||||
Dio dio = Dio();
|
||||
try {
|
||||
@@ -285,14 +283,14 @@ class HttpUtil {
|
||||
|
||||
// Utils.jsonPrettyPrint(response.data);
|
||||
|
||||
int statusCode = response.statusCode;
|
||||
int statusCode = response.statusCode!;
|
||||
if (callback != null) {
|
||||
callback(response);
|
||||
}
|
||||
return response.data;
|
||||
} on DioError catch(e) {
|
||||
} on DioException catch(e) {
|
||||
if (e.response != null) {
|
||||
Utils.jsonPrettyPrint(e.response.data);
|
||||
Utils.jsonPrettyPrint(e.response!.data);
|
||||
}
|
||||
if (returnError) {
|
||||
return e;
|
||||
@@ -301,12 +299,12 @@ class HttpUtil {
|
||||
}
|
||||
}
|
||||
|
||||
static Future<dynamic> httpDelete(String url, PostCallback callback,
|
||||
static Future<dynamic> httpDelete(String url, PostCallback? callback,
|
||||
{
|
||||
Map<String, dynamic> queryParameters,
|
||||
Map<String, dynamic>? queryParameters,
|
||||
int businessId = 0,
|
||||
Map<String, String> additionalHeaders,
|
||||
Map<String, dynamic> body,
|
||||
Map<String, String>? additionalHeaders,
|
||||
Map<String, dynamic>? body,
|
||||
bool returnError = false,
|
||||
}) async {
|
||||
|
||||
@@ -320,8 +318,7 @@ class HttpUtil {
|
||||
}
|
||||
|
||||
localHeaders['Http-Business-Id'] = businessId == 0 ? '${Constants.BUSINESS_ID}' : businessId.toString();
|
||||
Box box = await Utils.getBox();
|
||||
localHeaders['Http-Contact-Authorization'] = box.get(Constants.KEY_ACCESS_TOKEN, defaultValue: '');
|
||||
localHeaders['Http-Contact-Authorization'] = Utils.prefs?.getString(Constants.KEY_ACCESS_TOKEN) ?? '';
|
||||
localHeaders['Http-App-Key'] = platformName;
|
||||
localHeaders['Http-Device-Type'] = platformName;
|
||||
|
||||
@@ -333,7 +330,9 @@ class HttpUtil {
|
||||
requestUrl = url;
|
||||
}
|
||||
|
||||
Utils.jsonPrettyPrint(body);
|
||||
if (body != null) {
|
||||
Utils.jsonPrettyPrint(body);
|
||||
}
|
||||
|
||||
Dio dio = Dio();
|
||||
try {
|
||||
@@ -348,14 +347,14 @@ class HttpUtil {
|
||||
|
||||
// Utils.jsonPrettyPrint(response.data);
|
||||
|
||||
int statusCode = response.statusCode;
|
||||
int statusCode = response.statusCode!;
|
||||
if (callback != null) {
|
||||
callback(response);
|
||||
}
|
||||
return response.data;
|
||||
} on DioError catch(e) {
|
||||
} on DioException catch(e) {
|
||||
if (e.response != null) {
|
||||
Utils.jsonPrettyPrint(e.response.data);
|
||||
Utils.jsonPrettyPrint(e.response!.data);
|
||||
}
|
||||
if(returnError) {
|
||||
return e;
|
||||
|
||||
Reference in New Issue
Block a user