call method

  1. @override
Future<SubmitResponse> call(
  1. Principal canisterId,
  2. CallOptions fields,
  3. Identity? identity
)
override

Implementation

@override
Future<SubmitResponse> call(
    Principal canisterId, CallOptions fields, Identity? identity) async {
  final id = (identity ?? await _identity);

  final canister = Principal.from(canisterId);

  final ecid = fields.effectiveCanisterId != null
      ? Principal.from(fields.effectiveCanisterId)
      : canister;

  // ignore: unnecessary_null_comparison
  final sender = id != null ? id.getPrincipal() : Principal.anonymous();

  CallRequest submit = CallRequest()
    ..request_type = SubmitRequestType.Call
    ..canister_id = canister
    ..method_name = fields.methodName
    ..arg = fields.arg
    ..sender = sender
    ..ingress_expiry = Expiry(DEFAULT_INGRESS_EXPIRY_DELTA_IN_MSECS);

  var rsRequest = HttpAgentCallRequest()
    ..endpoint = Endpoint.Call
    ..body = submit
    ..request = {
      "method": "POST",
      "headers": {
        'Content-Type': 'application/cbor',
        ..._baseHeaders,
      },
    };
  var transformedRequest = await _transform(rsRequest);

  var newTransformed = await id!.transformRequest(transformedRequest);
  var body = cbor.cborEncode(newTransformed["body"]);
  var list = await Future.wait([
    _fetch!(
        endpoint: "/api/v2/canister/${ecid.toText()}/call",
        method: FetchMethod.post,
        headers: newTransformed["request"]["headers"],
        body: body),
    Future.value(requestIdOf(submit.toJson()))
  ]);

  var response = list[0] as Map<String, dynamic>;
  var requestId = list[1] as Uint8List;
  if (!(response["ok"] as bool)) {
    // ignore: prefer_adjacent_string_concatenation
    throw 'Server returned an error:\n' +
        '  Code: ${response["statusCode"]} (${response["statusText"]})\n' +
        '  Body: ${response["body"] is Uint8List ? (response["body"] as Uint8List).u8aToString() : response["body"]}\n';
  }

  return CallResponseBody.fromJson({...response, "requestId": requestId});
}