use std::fmt::{self};
use axum::extract::rejection::JsonRejection;
use axum::extract::Extension;
use axum::Json;
use futures::{future, FutureExt};
use serde::de::DeserializeOwned;
use tracing::{error, trace, warn};
use crate::rpc_core::error::RpcError;
use crate::rpc_core::request::{Request, RpcCall, RpcMethodCall};
use crate::rpc_core::response::{Response, ResponseResult, RpcResponse};
#[async_trait::async_trait]
pub trait RpcHandler: Clone + Send + Sync + 'static {
type Request: DeserializeOwned + Send + Sync + fmt::Debug;
async fn on_request(&self, request: Self::Request) -> ResponseResult;
async fn on_call(&self, call: RpcMethodCall) -> RpcResponse {
trace!(target: "rpc", id = ?call.id , method = ?call.method, "received method call");
let RpcMethodCall { method, params, id, .. } = call;
let params: serde_json::Value = params.into();
let call = serde_json::json!({
"method": &method,
"params": params
});
match serde_json::from_value::<Self::Request>(call) {
Ok(req) => {
let result = self.on_request(req).await;
RpcResponse::new(id, result)
}
Err(err) => {
let err = err.to_string();
let distinctive_error = format!("unknown variant `{method}`");
if err.contains(&distinctive_error) {
error!(target: "rpc", ?method, "failed to deserialize method due to unknown variant");
RpcResponse::new(id, RpcError::method_not_found())
} else {
error!(target: "rpc", ?method, ?err, "failed to deserialize method");
RpcResponse::new(id, RpcError::invalid_params(err))
}
}
}
}
}
pub async fn handle<THandler: RpcHandler>(
request: Result<Json<Request>, JsonRejection>,
Extension(handler): Extension<THandler>,
) -> Json<Response> {
match request {
Ok(req) => handle_request(req.0, handler)
.await
.unwrap_or_else(|| Response::error(RpcError::invalid_request()))
.into(),
Err(err) => {
warn!(target: "rpc", ?err, "invalid request");
Response::error(RpcError::invalid_request()).into()
}
}
}
pub async fn handle_request<THandler: RpcHandler>(
req: Request,
handler: THandler,
) -> Option<Response> {
fn responses_as_batch(outs: Vec<Option<RpcResponse>>) -> Option<Response> {
let batch: Vec<_> = outs.into_iter().flatten().collect();
(!batch.is_empty()).then_some(Response::Batch(batch))
}
match req {
Request::Single(call) => handle_call(call, handler).await.map(Response::Single),
Request::Batch(calls) => {
future::join_all(calls.into_iter().map(move |call| handle_call(call, handler.clone())))
.map(responses_as_batch)
.await
}
}
}
async fn handle_call<THandler: RpcHandler>(
call: RpcCall,
handler: THandler,
) -> Option<RpcResponse> {
match call {
RpcCall::MethodCall(call) => {
trace!(target: "rpc", id = ?call.id , method = ?call.method, "handling call");
Some(handler.on_call(call).await)
}
RpcCall::Notification(notification) => {
trace!(target: "rpc", method = ?notification.method, "received rpc notification");
None
}
RpcCall::Invalid { id } => {
warn!(target: "rpc", ?id, "invalid rpc call");
Some(RpcResponse::invalid_request(id))
}
}
}