Methods

We're ready to deploy our JsonRpcInterface. But right now now it just returns JsonError::MethodNotFound. So before testing out the JSON-RPC, let's implement some methods.

We'll start with a simple pong method that replies to ping.

#![allow(unused)]
fn main() {
impl JsonRpcInterface {
    // RPCAPI:
    // Replies to a ping method.
    // --> {"jsonrpc": "2.0", "method": "ping", "params": [], "id": 42}
    // <-- {"jsonrpc": "2.0", "result": "pong", "id": 42}
    async fn pong(&self, id: Value, _params: Value) -> JsonResult {
        JsonResponse::new(json!("pong"), id).into()
    }
}
}

And add it to handle_request():

#![allow(unused)]
fn main() {
#[async_trait]
impl RequestHandler for JsonRpcInterface {
    async fn handle_request(&self, req: JsonRequest) -> JsonResult {
        //...
        match req.method.as_str() {
            Some("ping") => self.pong(req.id, req.params).await,
            Some(_) | None => JsonError::new(ErrorCode::MethodNotFound, None, req.id).into(),
        }
    }
}
}