callOp method

Future<(int, Object?, String?)> callOp(
  1. String op,
  2. Map<String, String> params,
  3. Object? body,
  4. String? ifMatch,
  5. String? token,
  6. String idempotencyKey,
)

Executes call messages of realtime sessions through the same routes.

Implementation

Future<(int, Object?, String?)> callOp(
  String op,
  Map<String, String> params,
  Object? body,
  String? ifMatch,
  String? token,
  String idempotencyKey,
) async {
  final o = Operations.byId(op);
  if (o == null) {
    return (
      404,
      _problemJson(404, 'not-found', 'unknown operation $op'),
      null,
    );
  }
  final FreedomeResponse res;
  try {
    res = await handle(
      FreedomeRequest(
        o.method,
        o.expand(params),
        headers: {
          if (token != null) 'authorization': 'Bearer $token',
          'if-match': ?ifMatch,
          if (o.method == 'POST') 'idempotency-key': idempotencyKey,
          if (body != null)
            'content-type':
                operationSchemas[o.id]?.request.keys
                    .where((k) => k.contains('json'))
                    .firstOrNull ??
                'application/json',
        },
        body: body == null ? null : utf8.encode(jsonEncode(body)),
        operationId: o.id,
      ),
    );
  } on NetworkException {
    return (503, _problemJson(503, 'internal', 'network down'), null);
  }
  Object? json;
  try {
    json = res.json;
  } on FormatException {
    json = null;
  }
  return (res.status, json, res.headers['etag']);
}