You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
110 lines
3.3 KiB
Dart
110 lines
3.3 KiB
Dart
|
|
import 'dart:convert';
|
|
import 'dart:io' show Platform;
|
|
import 'package:queuing_system/core/config/config.dart';
|
|
import 'package:queuing_system/utils/Utils.dart';
|
|
import 'package:http/http.dart' as http;
|
|
|
|
class BaseAppClient {
|
|
static post(String endPoint,
|
|
{Map<String, dynamic> body,
|
|
Function(dynamic response, int statusCode) onSuccess,
|
|
Function(String error, int statusCode) onFailure}) async {
|
|
String url;
|
|
|
|
url = BASE_URL + endPoint;
|
|
|
|
try {
|
|
|
|
print("URL : $url");
|
|
print("Body : ${json.encode(body)}");
|
|
var asd = json.encode(body);
|
|
var asd2;
|
|
if (await Utils.checkConnection()) {
|
|
|
|
final response = await http.post(Uri.parse(url),
|
|
body: json.encode(body),
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'Accept': 'application/json',
|
|
});
|
|
final int statusCode = response.statusCode;
|
|
if (statusCode < 200 || statusCode >= 400) {
|
|
onFailure(Utils.generateContactAdminMsg(), statusCode);
|
|
} else {
|
|
print("Response: ${response.body.toString()}");
|
|
var parsed = json.decode(response.body.toString());
|
|
onSuccess(parsed, statusCode);
|
|
}
|
|
} else {
|
|
onFailure('Please Check The Internet Connection', -1);
|
|
}
|
|
} catch (e) {
|
|
print(e);
|
|
onFailure(e.toString(), -1);
|
|
}
|
|
}
|
|
|
|
|
|
static get(String endPoint,
|
|
{Map<String, dynamic> body,
|
|
Function(dynamic response, int statusCode) onSuccess,
|
|
Function(String error, int statusCode) onFailure}) async {
|
|
String url;
|
|
|
|
url = BASE_URL + endPoint;
|
|
|
|
try {
|
|
// String token = await sharedPref.getString(TOKEN);
|
|
|
|
print("URL GET: $url");
|
|
print("Body GET: ${json.encode(body)}");
|
|
var asd = json.encode(body);
|
|
var asd2;
|
|
if (await Utils.checkConnection()) {
|
|
final response = await http.get(Uri.parse(url),
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'Accept': 'application/json'
|
|
});
|
|
final int statusCode = response.statusCode;
|
|
if (statusCode < 200 || statusCode >= 400) {
|
|
onFailure(Utils.generateContactAdminMsg(), statusCode);
|
|
} else {
|
|
var parsed = json.decode(response.body.toString());
|
|
onSuccess(parsed, statusCode);
|
|
}
|
|
} else {
|
|
onFailure('Please Check The Internet Connection', -1);
|
|
}
|
|
} catch (e) {
|
|
print(e);
|
|
onFailure(e.toString(), -1);
|
|
}
|
|
}
|
|
|
|
|
|
String getError(parsed) {
|
|
//TODO change this fun
|
|
String error = parsed['ErrorEndUserMessage'] ?? parsed['ErrorMessage'];
|
|
if (parsed["ValidationErrors"] != null) {
|
|
error = parsed["ValidationErrors"]["StatusMessage"].toString() + "\n";
|
|
|
|
if (parsed["ValidationErrors"]["ValidationErrors"] != null &&
|
|
parsed["ValidationErrors"]["ValidationErrors"].length != 0) {
|
|
for (var i = 0;
|
|
i < parsed["ValidationErrors"]["ValidationErrors"].length;
|
|
i++) {
|
|
error = error +
|
|
parsed["ValidationErrors"]["ValidationErrors"][i]["Messages"][0] +
|
|
"\n";
|
|
}
|
|
}
|
|
}
|
|
if (error == null || error == "null" || error == "null\n") {
|
|
return Utils.generateContactAdminMsg();
|
|
}
|
|
return error;
|
|
}
|
|
}
|