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 ?? _identity;
  final sender = id?.getPrincipal() ?? Principal.anonymous();

  final requestBody = ReadStateRequest(
    paths: fields.paths,
    sender: sender,
    ingressExpiry: Expiry(_defaultIngressExpiryDeltaInMilliseconds),
  );

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

  final transformedRequest = await _transform(rsRequest);
  final newTransformed = await id!.transformRequest(
    transformedRequest,
  );

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

  if (!(response['ok'] as bool)) {
    throw AgentFetchError(
      statusCode: response['statusCode'],
      statusText: response['statusText'],
      body: response['body'],
    );
  }

  final buffer = response['arrayBuffer'] as Uint8List;
  final decoded = cbor.cborDecode<Map>(buffer);
  return ReadStateResponseResult(
    certificate: blobFromBuffer(
      (decoded['certificate'] as Uint8Buffer).buffer,
    ),
  );
}