execute method
Implementation
Future<ARSDataAPIResponse> execute(
String method, {
Map<String, dynamic>? parameters,
String methodType = "POST",
void Function(int transferred, int total)? onProgress,
int attempt = 0,
bool isUpload = false,
}) async {
final isAuth = method == auth;
String url = "";
url =
"${address.startsWith("http") ? "" : "http://"}$address:$port/ARSDataAPI/$method";
if (parameters != null && parameters is String && methodType == "GET") {
url = "$url${Uri.encodeComponent("/${parameters.toString()}")}";
}
final http.Request request = http.Request(methodType, Uri.parse(url));
request.headers.addAll({"Authorization": token});
if (methodType == "GET") {
} else {
parameters ??= {};
parameters["token"] = token;
request.body = json.encode(parameters);
}
try {
logger.verbose("Comms executing $methodType $url");
final http.StreamedResponse response = await client.send(request);
if (response.statusCode == 403 && attempt++ < 3) {
if (!isAuth) {
await _authenticate();
}
return await execute(
method,
parameters: parameters,
methodType: methodType,
onProgress: onProgress,
attempt: attempt,
);
}
final http.Response httpResponse = await parseResponse(
response: response,
onProgress: onProgress,
);
logger.verbose(
"""
url: $url,
httpResponse.contentLength: ${httpResponse.contentLength},
httpResponse.body:${httpResponse.body},
""",
);
final Map<String, dynamic> apiResponse =
jsonDecode(httpResponse.body) as Map<String, dynamic>;
return ARSDataAPIResponse(
code: apiResponse["Code"] as int,
data: jsonDecode(apiResponse["Data"] as String),
);
} on Exception catch (ex) {
logger.error("Error in comms execute", ex: ex);
return ARSDataAPIResponse(
code: 500,
data: ex.toString(),
);
}
}