invoke method

Future<FunctionResponse> invoke(
  1. String functionName, {
  2. Map<String, String>? headers,
  3. Map<String, dynamic>? body,
  4. ResponseType responseType = ResponseType.json,
})

Invokes a function

functionName - the name of the function to invoke

headers: object representing the headers to send with the request

body: the body of the request

responseType: how the response should be parsed. The default is json

Implementation

Future<FunctionResponse> invoke(
  String functionName, {
  Map<String, String>? headers,
  Map<String, dynamic>? body,
  ResponseType responseType = ResponseType.json,
}) async {
  try {
    final response = await http.post(
      Uri.parse('$_url/$functionName'),
      headers: <String, String>{..._headers, if (headers != null) ...headers},
      body: jsonEncode(body),
    );

    dynamic data;
    if (responseType == ResponseType.json) {
      data = json.decode(response.body);
    } else if (responseType == ResponseType.blob) {
      data = response.bodyBytes;
    } else if (responseType == ResponseType.arraybuffer) {
      data = response.bodyBytes;
    } else if (responseType == ResponseType.text) {
      data = response.body;
    } else {
      data = response.body;
    }
    return FunctionResponse(data: data);
  } catch (error) {
    return FunctionResponse(error: error);
  }
}