makeApiRequest method
Helper method to make API requests to the server.
endpoint
: The URL of the API endpoint.
body
: The request payload in the form of a Map
.
method
: The HTTP method for the request (e.g., 'POST', 'GET', etc.).
This method encrypts the request payload using the EncryptionService
,
prepares the headers and body, and sends the request. It returns the
response from the server as an http.Response
.
Implementation
Future<http.Response> makeApiRequest(
String endpoint,
Map<String, dynamic> body,
String method,
) async {
// Convert the request body into a JSON string
String bodyString = json.encode(body);
// Encrypt the JSON string payload
Uint8List encryptedBody = encryptionService.encryptPayload(bodyString);
String encryptedBase64 = base64.encode(encryptedBody);
// Define the request headers including the API key
var headers = {
'api-key': apiKey,
'Content-Type': 'application/json',
};
// Create an HTTP request object using the specified method and endpoint
var request = http.Request(method, Uri.parse(endpoint));
request.headers.addAll(headers);
// Add the encrypted body to the request as a JSON object
request.body = json.encode({"data": encryptedBase64});
// Send the request and return the response
return await http.Client().send(request).then((response) async {
return await http.Response.fromStream(response);
});
}