request method
Makes an HTTP request to the Kyte API backend and returns model data from JSON.
the fromJson method must be defined for your model and passed along with
method, which is the HTTP method defined as a KyteRequestType, and the
name of your model as a string model.
All other arguments are optional but may be required for the specific type of call you are making to the backend.
Implementation
Future<dynamic> request(dynamic Function(Map<String, dynamic> json) fromJosn,
KyteRequestType method, String model,
{String? body,
String? field,
String? value,
Map<String, String>? customHeaders,
String pageId = "1",
String pageSize = "0",
String contentType = "application/json"}) async {
http.Response response;
switch (method) {
/// Make POST request
case KyteRequestType.post:
if (body == null) {
/// If request body is empty, throw exception
throw Exception('Data body cannot be null for POST request');
}
response = await post(model, body,
customHeaders: customHeaders,
pageId: pageId,
pageSize: pageSize,
contentType: contentType);
break;
/// Make PUT request
case KyteRequestType.put:
if (body == null) {
/// If request body is empty, throw exception
throw Exception('Data body cannot be null for PUT request');
}
response = await put(model, body,
field: field,
value: value,
customHeaders: customHeaders,
pageId: pageId,
pageSize: pageSize,
contentType: contentType);
break;
/// Make GET request
case KyteRequestType.get:
response = await get(model,
field: field,
value: value,
customHeaders: customHeaders,
pageId: pageId,
pageSize: pageSize,
contentType: contentType);
break;
/// Make DELETE request
case KyteRequestType.delete:
response = await delete(model,
field: field,
value: value,
customHeaders: customHeaders,
pageId: pageId,
pageSize: pageSize,
contentType: contentType);
break;
default:
/// Unknown or unsupported HTTP request
throw Exception(
'Unknown or unsupported HTTP request. Must be POST, PUT, GET, or DELETE');
}
try {
if (response.body.isEmpty) {
/// If the response body is empty, throw an exception.
throw Exception("Response body is empty.");
}
if (response.statusCode != 200) {
/// If the response status code was other than 200, return a Kyte Error
return KyteErrorResponse.fromJson(json.decode(response.body));
}
/// Parse JSOn from successful response body and return model
return fromJosn(json.decode(response.body));
} catch (e) {
/// If there was an error with parsing the data, throw exception
throw KyteHttpException("Unable to parse response data. ${e.toString()}",
responseCode: response.statusCode);
}
}