invoke method

Future invoke(
  1. String? cmd,
  2. IContext? context,
  3. Map args
)

Performs Azure Function invocation.

  • invocationType an invocation type: 'RequestResponse' or 'Event'
  • cmd an action name to be called.
  • context (optional) a context to trace execution through call chain.
  • args action arguments Return Future that receives action result Throws error.

Implementation

Future invoke(String? cmd, IContext? context, Map args) async {
  if (cmd == null || cmd.isEmpty) {
    var err = UnknownException(
        context != null ? ContextResolver.getTraceId(context) : null,
        'NO_COMMAND',
        'Missing Seneca pattern cmd');
    logger.error(context, err, 'Failed to call %s', [cmd]);
    throw err;
  }

  var cloneArgs = Map.from(args);
  cloneArgs['cmd'] = cmd;
  cloneArgs['trace_id'] = context != null
      ? ContextResolver.getTraceId(context)
      : IdGenerator.nextShort();
  headers['Content-Type'] = 'application/json';
  http.Response? response;

  try {
    response = await client!.post(Uri.parse(uri!),
        headers: headers, body: json.encode(cloneArgs));

    // ignore: unnecessary_null_comparison
    if (response == null) {
      throw ApplicationExceptionFactory.create(ErrorDescriptionFactory.create(
          UnknownException(
              context != null ? ContextResolver.getTraceId(context) : null,
              'Unable to get a result from uri $uri')));
    }

    if (response.statusCode == 204) {
      return null;
    }

    return response.body;
  } catch (err) {
    logger.error(context, InvocationException().wrap(err),
        'Failed to invoke AzureFunction function');

    throw InvocationException(
            context != null ? ContextResolver.getTraceId(context) : null,
            'CALL_FAILED',
            'Failed to invoke AzureFunction function')
        .withCause(err);
  }
}