diff --git a/ruma-client/src/http_client.rs b/ruma-client/src/http_client.rs index 73aa6dbc..030445f3 100644 --- a/ruma-client/src/http_client.rs +++ b/ruma-client/src/http_client.rs @@ -7,7 +7,7 @@ use async_trait::async_trait; use bytes::BufMut; use ruma_api::{OutgoingRequest, SendAccessToken}; -use crate::ResponseResult; +use crate::{ResponseError, ResponseResult}; #[cfg(feature = "hyper")] mod hyper; @@ -33,7 +33,7 @@ pub trait HttpClient: Sync { type ResponseBody: AsRef<[u8]>; /// The error type for the `send_request` function. - type Error: Unpin; + type Error: Send + Unpin; /// Send an `http::Request` to get back an `http::Response`. async fn send_http_request( @@ -60,8 +60,11 @@ pub trait HttpClientExt: HttpClient { homeserver_url: &str, access_token: SendAccessToken<'_>, request: R, - ) -> Pin> + 'a>> { - self.send_customized_request(homeserver_url, access_token, request, |_| {}) + ) -> Pin> + 'a>> + where + ::EndpointError: Send, + { + self.send_customized_request(homeserver_url, access_token, request, |_| Ok(())) } /// Turn a strongly-typed matrix request into an `http::Request`, customize it and send it to @@ -76,7 +79,8 @@ pub trait HttpClientExt: HttpClient { ) -> Pin> + 'a>> where R: OutgoingRequest + 'a, - F: FnOnce(&mut http::Request) + 'a, + ::EndpointError: Send, + F: FnOnce(&mut http::Request) -> Result<(), ResponseError> + 'a, { Box::pin(crate::send_customized_request( self, diff --git a/ruma-client/src/lib.rs b/ruma-client/src/lib.rs index ea86cdb9..eef15488 100644 --- a/ruma-client/src/lib.rs +++ b/ruma-client/src/lib.rs @@ -95,11 +95,13 @@ pub use self::{ http_client::{DefaultConstructibleHttpClient, HttpClient, HttpClientExt}, }; +/// The error type for sending the request `R` with the http client `C`. +pub type ResponseError = + Error<::Error, ::EndpointError>; + /// The result of sending the request `R` with the http client `C`. -pub type ResponseResult = Result< - ::IncomingResponse, - Error<::Error, ::EndpointError>, ->; +pub type ResponseResult = + Result<::IncomingResponse, ResponseError>; /// A client for the Matrix client-server API. #[derive(Clone, Debug)] @@ -155,8 +157,11 @@ impl Client { impl Client { /// Makes a request to a Matrix API endpoint. - pub async fn send_request(&self, request: R) -> ResponseResult { - self.send_customized_request(request, |_| {}).await + pub async fn send_request(&self, request: R) -> ResponseResult + where + ::EndpointError: Send, + { + self.send_customized_request(request, |_| Ok(())).await } /// Makes a request to a Matrix API endpoint including additional URL parameters. @@ -167,7 +172,8 @@ impl Client { ) -> ResponseResult where R: OutgoingRequest, - F: FnOnce(&mut http::Request), + ::EndpointError: Send, + F: FnOnce(&mut http::Request) -> Result<(), ResponseError>, { let access_token = self.access_token(); let send_access_token = match access_token.as_deref() { @@ -196,12 +202,16 @@ fn send_customized_request<'a, C, R, F>( where C: HttpClient + ?Sized, R: OutgoingRequest, - F: FnOnce(&mut http::Request), + ::EndpointError: Send, + F: FnOnce(&mut http::Request) -> Result<(), ResponseError>, { - let mut http_req = request.try_into_http_request(homeserver_url, send_access_token); - if let Ok(req) = &mut http_req { - customize(req); - } + let http_req = request + .try_into_http_request(homeserver_url, send_access_token) + .map_err(ResponseError::::from) + .and_then(|mut req| { + customize(&mut req)?; + Ok(req) + }); async move { let http_res = http_client.send_http_request(http_req?).await.map_err(Error::Response)?;