readState method

  1. @override
Future<ReadStateResponse> readState(
  1. Principal canisterId,
  2. ReadStateOptions fields,
  3. Identity? identity
)
override

Send a read state query to the replica. This includes a list of paths to return, and will return a Certificate. This will only reject on communication errors, but the certificate might contain less information than requested. @param effectiveCanisterId A Canister ID related to this call. @param options The options for this call.

Implementation

@override
Future<ReadStateResponse> readState(
    Principal canisterId, ReadStateOptions fields, Identity? identity) async {
  final canister = canisterId is String
      ? Principal.fromText(canisterId as String)
      : canisterId;
  final id = (identity ?? await _identity);
  final sender = id?.getPrincipal() ?? Principal.anonymous();

  var requestBody = ReadStateRequest()
    ..request_type = ReadRequestType.ReadState
    ..paths = fields.paths
    ..sender = sender
    ..ingress_expiry = Expiry(DEFAULT_INGRESS_EXPIRY_DELTA_IN_MSECS);

  var rsRequest = HttpAgentReadStateRequest()
    ..endpoint = Endpoint.ReadState
    ..body = requestBody
    ..request = {
      "method": "POST",
      "headers": {
        'Content-Type': 'application/cbor',
        ..._baseHeaders,
      },
    };

  var transformedRequest = await _transform(rsRequest);

  Map<String, dynamic> newTransformed =
      await id!.transformRequest(transformedRequest);

  var body = cbor.cborEncode(newTransformed["body"]);
  final response = await _fetch!(
    endpoint: "/api/v2/canister/$canister/read_state",
    method: FetchMethod.post,
    headers: newTransformed["request"]["headers"],
    body: body,
  );

  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"]}\n';
  }

  final buffer = response["arrayBuffer"] as Uint8List;

  return ReadStateResponseResult()
    ..certificate = blobFromBuffer(
        ((cbor.cborDecode<Map>(buffer)["certificate"]) as Uint8Buffer)
            .buffer);
}