pub struct JrRequestCx<T: JrResponsePayload = 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
connection_cxto access the underlyingJrConnectionCx, giving access tosend_request,send_notification, andspawn
§Example
connection.on_receive_request(async |req: ProcessRequest, request_cx, 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
request_cx.respond(ProcessResponse { result })
})§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 JrRequestCx<Value>
impl JrRequestCx<Value>
Sourcepub fn cast<T: JrResponsePayload>(self) -> JrRequestCx<T>
pub fn cast<T: JrResponsePayload>(self) -> JrRequestCx<T>
Cast this request context to a different response type
Source§impl<T: JrResponsePayload> JrRequestCx<T>
impl<T: JrResponsePayload> JrRequestCx<T>
Sourcepub fn erase_to_json(self) -> JrRequestCx<Value>
pub fn erase_to_json(self) -> JrRequestCx<Value>
Convert to a JrRequestCx 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) -> JrRequestCx<T>
pub fn wrap_method(self, method: String) -> JrRequestCx<T>
Return a new JrResponse that expects a response of type U and serializes it.
Sourcepub fn wrap_params<U: JrResponsePayload>(
self,
wrap_fn: impl FnOnce(&str, Result<U, Error>) -> Result<T, Error> + Send + 'static,
) -> JrRequestCx<U>
pub fn wrap_params<U: JrResponsePayload>( self, wrap_fn: impl FnOnce(&str, Result<U, Error>) -> Result<T, Error> + Send + 'static, ) -> JrRequestCx<U>
Return a new JrResponse that expects a response of type U and serializes it.
wrap_fn will be invoked with the method name and the result of the wrapped function.
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.