toEncodingResponse<T> method

T toEncodingResponse<T>(
  1. BaseServiceResponse response, {
  2. ServiceReponseEncoding? encoding,
})

Converts a BaseServiceResponse into the specified encoding type.

The target encoding can be provided via encoding or inferred from the responseEncoding. Common output types include bytes, strings, maps, and arrays.

Implementation

T toEncodingResponse<T>(
  BaseServiceResponse response, {
  ServiceReponseEncoding? encoding,
}) {
  return switch (response) {
    BaseServiceErrorResponse() => throw _error(response),
    BaseServiceSuccessRespose() => () {
      try {
        return response.toEncodingResponse(encoding ?? responseEncoding);
      } catch (e) {
        throw RPCError(
          message: "Parsing response failed.",
          request: toJson(),
          relatedNetwork: network,
          statusCode: response.statusCode,
          details: {
            "encoding": encoding?.name ?? responseEncoding.name,
            "response": response.runtimeType.toString(),
          },
        );
      }
    }(),
  };
}