call static method

CloudCodeRequest<CloudCodeResponse> call(
  1. String function, {
  2. CloudCodeHttpMethod method = CloudCodeHttpMethod.post,
  3. Map<String, String>? query,
  4. Map<String, dynamic>? body,
  5. Map<String, String>? headers,
  6. Duration? timeout,
})

Implementation

static CloudCodeRequest<CloudCodeResponse> call(
  String function, {
  CloudCodeHttpMethod method = CloudCodeHttpMethod.post,
  Map<String, String>? query,
  Map<String, dynamic>? body,
  Map<String, String>? headers,
  Duration? timeout,
}) {
  impl.registerMethodChannelImplementation();

  // Internal only - never returned to callers. Needed because MethodChannel
  // is a stateless RPC: unlike the native SDKs (which cancel by holding a
  // token/Task object reference), Dart has to hand the native bridge a
  // string key it can use to find this call again on cancel(). Distinct
  // from CloudCodeResponse.requestId, which is the server's request id.
  final correlationId = _nextCorrelationId();
  final snapshot = _snapshotRequest(query, body, headers);
  if (snapshot.error != null) {
    return CloudCodeRequest._create(
      Future<CloudCodeResponse>.error(snapshot.error!),
      () async {},
    );
  }

  final platform = AppAmbitSdkFlutterPlatform.instance;
  Future<void> cancelNative() =>
      platform.cloudCodeCancel(correlationId);
  var operation = _invoke(
    platform.cloudCodeCall(
      correlationId: correlationId,
      function: function,
      method: method.wireName,
      query: snapshot.query,
      body: snapshot.body,
      headers: snapshot.headers,
    ),
  );

  if (timeout != null) {
    operation = operation.timeout(
      timeout,
      onTimeout: () {
        unawaited(cancelNative());
        throw CloudCodeError(
          code: CloudCodeErrorCode.timedOut,
          message: 'Cloud Code request timed out after $timeout.',
          function: function,
          requestId: correlationId,
        );
      },
    );
  }

  return CloudCodeRequest._create(operation, cancelNative);
}