near_api/common/query/
query_rpc.rs1use async_trait::async_trait;
2use near_openapi_client::types::{
3 ErrorWrapperForRpcQueryError, JsonRpcRequestForQuery, JsonRpcRequestForQueryMethod,
4 JsonRpcResponseForRpcQueryResponseAndRpcQueryError, RpcQueryError, RpcQueryResponse,
5};
6
7use crate::{
8 advanced::{query_request::QueryRequest, RpcType},
9 common::utils::{is_critical_query_error, to_retry_error},
10 config::RetryResponse,
11 errors::SendRequestError,
12 NetworkConfig,
13};
14use near_api_types::Reference;
15
16#[derive(Clone, Debug)]
17pub struct SimpleQueryRpc {
18 pub request: QueryRequest,
19}
20
21#[async_trait]
22impl RpcType for SimpleQueryRpc {
23 type RpcReference = Reference;
24 type Response = RpcQueryResponse;
25 type Error = RpcQueryError;
26 async fn send_query(
27 &self,
28 client: &near_openapi_client::Client,
29 _network: &NetworkConfig,
30 reference: &Reference,
31 ) -> RetryResponse<RpcQueryResponse, SendRequestError<RpcQueryError>> {
32 let request = self.request.clone().to_rpc_query_request(reference.clone());
33 let response = client
34 .query(&JsonRpcRequestForQuery {
35 id: "0".to_string(),
36 jsonrpc: "2.0".to_string(),
37 method: JsonRpcRequestForQueryMethod::Query,
38 params: request,
39 })
40 .await
41 .map(|r| r.into_inner())
42 .map_err(SendRequestError::from);
43
44 match response {
45 Ok(JsonRpcResponseForRpcQueryResponseAndRpcQueryError::Variant0 { result, .. }) => {
46 RetryResponse::Ok(result)
47 }
48 Ok(JsonRpcResponseForRpcQueryResponseAndRpcQueryError::Variant1 { error, .. }) => {
49 let error = SendRequestError::from(error);
50 to_retry_error(error, is_critical_query_error)
51 }
52 Err(err) => to_retry_error(err, is_critical_query_error),
53 }
54 }
55}
56
57impl From<ErrorWrapperForRpcQueryError> for SendRequestError<RpcQueryError> {
58 fn from(err: ErrorWrapperForRpcQueryError) -> Self {
59 match err {
60 ErrorWrapperForRpcQueryError::InternalError(internal_error) => {
61 Self::InternalError(internal_error)
62 }
63 ErrorWrapperForRpcQueryError::RequestValidationError(
64 rpc_request_validation_error_kind,
65 ) => Self::RequestValidationError(rpc_request_validation_error_kind),
66 ErrorWrapperForRpcQueryError::HandlerError(server_error) => {
67 Self::ServerError(server_error)
68 }
69 }
70 }
71}