httpRequest function
Future<void>
httpRequest({
- required String url,
- required EHttpMethod httpMethod,
- required dynamic action(
- Response response
- required dynamic error(
- Response response
- dynamic body,
- bool encodeBody = true,
- Map<
String, String> ? headers, - Duration timeout = const Duration(seconds: 10),
- bool clearHeaders = false,
- DateTime? cacheExpireDate,
Implementation
Future<void> httpRequest({
required final String url,
required final EHttpMethod httpMethod,
required final Function(Response<dynamic> response) action,
required final Function(Response<dynamic> response) error,
final dynamic body,
final bool encodeBody = true,
final Map<String, String>? headers,
final Duration timeout = const Duration(seconds: 10),
final bool clearHeaders = false,
final DateTime? cacheExpireDate,
}) async {
try {
final Map<String, String> header = <String, String>{
"Authorization": ULocalStorage.getString(UConstants.token) ?? "",
"X-API-Key": UCore.apiKey,
};
if (clearHeaders) header.clear();
if (headers != null) header.addAll(headers);
Response<dynamic> response = const Response<dynamic>();
dynamic params;
if (body != null) {
if (encodeBody)
params = body.toJson();
else
params = body;
}
final GetConnect connect = GetConnect(timeout: timeout);
if (httpMethod == EHttpMethod.get) {
if (cacheExpireDate != null) {
if (ULocalStorage.getString(url).isNullOrEmpty()) {
response = await connect.get(url, headers: header);
ULocalStorage.set(url, response.bodyString);
ULocalStorage.set("${url}___ExpireDate", cacheExpireDate.toIso8601String());
} else {
if (DateTime.parse(ULocalStorage.getString("${url}___ExpireDate")!).isBefore(DateTime.now())) {
ULocalStorage.set(url, null);
ULocalStorage.set("${url}___ExpireDate", null);
await httpRequest(
url: url,
httpMethod: EHttpMethod.get,
action: action,
error: error,
headers: headers,
timeout: timeout,
clearHeaders: clearHeaders,
cacheExpireDate: cacheExpireDate,
);
} else {
action(Response<dynamic>(statusCode: 200, bodyString: ULocalStorage.getString(url)));
return;
}
}
} else
response = await connect.get(url, headers: header);
}
if (httpMethod == EHttpMethod.post) response = await connect.post(url, params, headers: header);
if (httpMethod == EHttpMethod.put) response = await connect.put(url, params, headers: header);
if (httpMethod == EHttpMethod.patch) response = await connect.patch(url, params, headers: header);
if (httpMethod == EHttpMethod.delete) response = await connect.delete(url, headers: header);
if (kDebugMode) response.prettyLog(params: (body == null || !encodeBody) ? "" : body.toJson());
if (response.isSuccessful()) {
action(response);
} else {
if (response.statusCode == 401) ULocalStorage.clearData();
error(response);
}
} catch (e) {
error(const Response<dynamic>(statusCode: 999, body: "{}", bodyString: "{}"));
ULoading.dismissLoading();
}
ULoading.dismissLoading();
}