pub struct Responder<T: JsonRpcResponse = Value> { /* private fields */ }Expand description
The context to respond to an incoming request.
This context is provided to request handlers and serves a dual role:
- Respond to the request - Use
respondorrespond_with_resultto send the response - Send other messages - Use the
ConnectionToparameter passed to your handler, which providessend_request,send_notification, andspawn
§Example
connection.on_receive_request(async |req: ProcessRequest, responder, cx| {
// Send a notification while processing
cx.send_notification(StatusUpdate {
message: "processing".into(),
})?;
// Do some work...
let result = process(&req.data)?;
// Respond to the request
responder.respond(ProcessResponse { result })
}, agent_client_protocol::on_receive_request!())§Event Loop Considerations
Like all handlers, request handlers run on the event loop. Use
spawn for expensive operations to avoid blocking
the connection.
See the Event Loop and Concurrency section for more details.
Implementations§
Source§impl Responder<Value>
impl Responder<Value>
Sourcepub fn cast<T: JsonRpcResponse>(self) -> Responder<T>
pub fn cast<T: JsonRpcResponse>(self) -> Responder<T>
Cast this request context to a different response type.
The provided type T will be serialized to JSON before sending.
Source§impl<T: JsonRpcResponse> Responder<T>
impl<T: JsonRpcResponse> Responder<T>
Sourcepub fn erase_to_json(self) -> Responder<Value>
pub fn erase_to_json(self) -> Responder<Value>
Convert to a Responder that expects a JSON value
and which checks (dynamically) that the JSON value it receives
can be converted to T.
Sourcepub fn wrap_method(self, method: String) -> Responder<T>
pub fn wrap_method(self, method: String) -> Responder<T>
Return a new Responder with a different method name.
Sourcepub fn wrap_params<U: JsonRpcResponse>(
self,
wrap_fn: impl FnOnce(&str, Result<U, Error>) -> Result<T, Error> + Send + 'static,
) -> Responder<U>
pub fn wrap_params<U: JsonRpcResponse>( self, wrap_fn: impl FnOnce(&str, Result<U, Error>) -> Result<T, Error> + Send + 'static, ) -> Responder<U>
Return a new Responder that expects a response of type U.
wrap_fn will be invoked with the method name and the result to transform
type U into type T before sending.
Sourcepub fn respond_with_result(
self,
response: Result<T, Error>,
) -> Result<(), Error>
pub fn respond_with_result( self, response: Result<T, Error>, ) -> Result<(), Error>
Respond to the JSON-RPC request with either a value (Ok) or an error (Err).
Sourcepub fn respond(self, response: T) -> Result<(), Error>
pub fn respond(self, response: T) -> Result<(), Error>
Respond to the JSON-RPC request with a value.