matrix-sdk!: send_request returns Bytes

Prevents unnecessary copy
master
Johannes Becker 2021-04-19 12:26:10 +02:00
parent 3414a59b91
commit 95421f1713
3 changed files with 13 additions and 11 deletions

View File

@ -40,6 +40,7 @@ url = "2.2.0"
zeroize = "1.2.0" zeroize = "1.2.0"
mime = "0.3.16" mime = "0.3.16"
rand = { version = "0.8.2", optional = true } rand = { version = "0.8.2", optional = true }
bytes = "1.0.1"
matrix-sdk-common = { version = "0.2.0", path = "../matrix_sdk_common" } matrix-sdk-common = { version = "0.2.0", path = "../matrix_sdk_common" }

View File

@ -30,7 +30,7 @@ use matrix_sdk_common::{
FromHttpResponseError, IncomingResponse, FromHttpResponseError, IncomingResponse,
}; };
use crate::{error::HttpError, ClientConfig, OutgoingRequest, RequestConfig, Session}; use crate::{error::HttpError, Bytes, ClientConfig, OutgoingRequest, RequestConfig, Session};
/// Abstraction around the http layer. The allows implementors to use different /// Abstraction around the http layer. The allows implementors to use different
/// http libraries. /// http libraries.
@ -51,7 +51,7 @@ pub trait HttpSend: AsyncTraitDeps {
/// ///
/// ``` /// ```
/// use std::convert::TryFrom; /// use std::convert::TryFrom;
/// use matrix_sdk::{HttpSend, async_trait, HttpError, RequestConfig}; /// use matrix_sdk::{HttpSend, async_trait, HttpError, RequestConfig, Bytes};
/// ///
/// #[derive(Debug)] /// #[derive(Debug)]
/// struct Client(reqwest::Client); /// struct Client(reqwest::Client);
@ -60,7 +60,7 @@ pub trait HttpSend: AsyncTraitDeps {
/// async fn response_to_http_response( /// async fn response_to_http_response(
/// &self, /// &self,
/// mut response: reqwest::Response, /// mut response: reqwest::Response,
/// ) -> Result<http::Response<Vec<u8>>, HttpError> { /// ) -> Result<http::Response<Bytes>, HttpError> {
/// // Convert the reqwest response to a http one. /// // Convert the reqwest response to a http one.
/// todo!() /// todo!()
/// } /// }
@ -72,7 +72,7 @@ pub trait HttpSend: AsyncTraitDeps {
/// &self, /// &self,
/// request: http::Request<Vec<u8>>, /// request: http::Request<Vec<u8>>,
/// config: RequestConfig, /// config: RequestConfig,
/// ) -> Result<http::Response<Vec<u8>>, HttpError> { /// ) -> Result<http::Response<Bytes>, HttpError> {
/// Ok(self /// Ok(self
/// .response_to_http_response( /// .response_to_http_response(
/// self.0 /// self.0
@ -87,7 +87,7 @@ pub trait HttpSend: AsyncTraitDeps {
&self, &self,
request: http::Request<Vec<u8>>, request: http::Request<Vec<u8>>,
config: RequestConfig, config: RequestConfig,
) -> Result<http::Response<Vec<u8>>, HttpError>; ) -> Result<http::Response<Bytes>, HttpError>;
} }
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
@ -104,7 +104,7 @@ impl HttpClient {
request: Request, request: Request,
session: Arc<RwLock<Option<Session>>>, session: Arc<RwLock<Option<Session>>>,
config: Option<RequestConfig>, config: Option<RequestConfig>,
) -> Result<http::Response<Vec<u8>>, HttpError> { ) -> Result<http::Response<Bytes>, HttpError> {
let request = { let request = {
let read_guard; let read_guard;
let access_token = match Request::METADATA.authentication { let access_token = match Request::METADATA.authentication {
@ -205,7 +205,7 @@ pub(crate) fn client_with_config(config: &ClientConfig) -> Result<Client, HttpEr
async fn response_to_http_response( async fn response_to_http_response(
mut response: Response, mut response: Response,
) -> Result<http::Response<Vec<u8>>, reqwest::Error> { ) -> Result<http::Response<Bytes>, reqwest::Error> {
let status = response.status(); let status = response.status();
let mut http_builder = HttpResponse::builder().status(status); let mut http_builder = HttpResponse::builder().status(status);
@ -219,7 +219,7 @@ async fn response_to_http_response(
} }
} }
let body = response.bytes().await?.as_ref().to_owned(); let body = response.bytes().await?;
Ok(http_builder Ok(http_builder
.body(body) .body(body)
@ -231,7 +231,7 @@ async fn send_request(
client: &Client, client: &Client,
request: http::Request<Vec<u8>>, request: http::Request<Vec<u8>>,
_: RequestConfig, _: RequestConfig,
) -> Result<http::Response<Vec<u8>>, HttpError> { ) -> Result<http::Response<Bytes>, HttpError> {
let request = reqwest::Request::try_from(request)?; let request = reqwest::Request::try_from(request)?;
let response = client.execute(request).await?; let response = client.execute(request).await?;
@ -243,7 +243,7 @@ async fn send_request(
client: &Client, client: &Client,
request: http::Request<Vec<u8>>, request: http::Request<Vec<u8>>,
config: RequestConfig, config: RequestConfig,
) -> Result<http::Response<Vec<u8>>, HttpError> { ) -> Result<http::Response<Bytes>, HttpError> {
let mut backoff = ExponentialBackoff::default(); let mut backoff = ExponentialBackoff::default();
let mut request = reqwest::Request::try_from(request)?; let mut request = reqwest::Request::try_from(request)?;
let retry_limit = config.retry_limit; let retry_limit = config.retry_limit;
@ -305,7 +305,7 @@ impl HttpSend for Client {
&self, &self,
request: http::Request<Vec<u8>>, request: http::Request<Vec<u8>>,
config: RequestConfig, config: RequestConfig,
) -> Result<http::Response<Vec<u8>>, HttpError> { ) -> Result<http::Response<Bytes>, HttpError> {
send_request(&self, request, config).await send_request(&self, request, config).await
} }
} }

View File

@ -76,6 +76,7 @@ pub use matrix_sdk_base::{
Session, StateChanges, StoreError, Session, StateChanges, StoreError,
}; };
pub use bytes::Bytes;
pub use matrix_sdk_common::*; pub use matrix_sdk_common::*;
pub use reqwest; pub use reqwest;