httpRequest method
Future<HttpResponse>
httpRequest(
- HttpIntent intent,
- String message, {
- bool dialog = true,
- BuildContext? context,
- HttpMethod method = HttpMethod.post,
Implementation
Future<HttpResponse> httpRequest(
HttpIntent intent, String message,
{bool dialog = true, BuildContext? context, HttpMethod method = HttpMethod.post}) async {
if (dialog && context != null) {
Dialogs().showLoading(context, message);
}
try {
var dio = Dio();
var response = await dio.request(
'${CtzonApiSettings.getApiUrl()}${intent._intent}',
options: Options(
headers: {
"accept": "application/json",
"Commercial-Token": CtzonApiSettings.commercialKey,
"Accept-Language": "it",
if (userToken != null) "Authorization": "Bearer ${userToken?.token}"
},
method: method.name,
),
queryParameters: method.name == "get" ? intent._data : null,
data: intent._data,
);
if (dialog && (context?.mounted ?? false)) {
Dialogs().stopLoading(context!);
}
if (kDebugMode) {
debugPrint("[HTTP] Response (${intent._intent}): ${response.data}");
}
return HttpResponse(null, response.data);
} on DioException catch (ex) {
if (dialog && (context?.mounted ?? false)) {
Dialogs().stopLoading(context!);
}
if (ex.response?.statusCode == 401) {
SharedPreferences.getInstance().then((value) {
value.remove("account_logged");
value.remove("api_token");
});
if ((context?.mounted ?? false) && CtzonApiSettings.homePage != null) {
Navigator.pushReplacement(
context!,
MaterialPageRoute(
builder: (context) => CtzonApiSettings.homePage!));
}
}
return HttpResponse(ex, ex.response?.data,
error: ex.response?.data["error"],
errors: ex.response?.data["errors"],
dioException: ex);
} on Exception catch (e) {
if (dialog && (context?.mounted ?? false)) {
Dialogs().stopLoading(context!);
}
return HttpResponse(e, null);
}
}