matrix-sdk: Implement the HttpSend trait directly on the reqwest client.

master
Damir Jelić 2020-08-26 14:37:48 +02:00
parent deff66ac42
commit 7a418ae09e
2 changed files with 54 additions and 72 deletions

View File

@ -81,7 +81,7 @@ use matrix_sdk_common::{
use crate::{ use crate::{
events::{room::message::MessageEventContent, AnyMessageEventContent}, events::{room::message::MessageEventContent, AnyMessageEventContent},
http_client::{DefaultHttpClient, HttpClient, HttpSend}, http_client::{client_with_config, HttpClient, HttpSend},
identifiers::{EventId, RoomId, RoomIdOrAliasId, UserId}, identifiers::{EventId, RoomId, RoomIdOrAliasId, UserId},
Error, EventEmitter, OutgoingRequest, Result, Error, EventEmitter, OutgoingRequest, Result,
}; };
@ -353,7 +353,7 @@ impl Client {
let client = if let Some(client) = config.client { let client = if let Some(client) = config.client {
client client
} else { } else {
Arc::new(DefaultHttpClient::with_config(&config)?) Arc::new(client_with_config(&config)?)
}; };
let base_client = BaseClient::new_with_config(config.base_config)?; let base_client = BaseClient::new_with_config(config.base_config)?;

View File

@ -128,15 +128,8 @@ impl HttpClient {
} }
} }
/// Default http client used if none is specified using `Client::with_client`.
#[derive(Clone, Debug)]
pub struct DefaultHttpClient {
inner: Client,
}
impl DefaultHttpClient {
/// Build a client with the specified configuration. /// Build a client with the specified configuration.
pub fn with_config(config: &ClientConfig) -> Result<Self> { pub(crate) fn client_with_config(config: &ClientConfig) -> Result<Client> {
let http_client = reqwest::Client::builder(); let http_client = reqwest::Client::builder();
#[cfg(not(target_arch = "wasm32"))] #[cfg(not(target_arch = "wasm32"))]
@ -161,9 +154,7 @@ impl DefaultHttpClient {
let user_agent = match &config.user_agent { let user_agent = match &config.user_agent {
Some(a) => a.clone(), Some(a) => a.clone(),
None => { None => HeaderValue::from_str(&format!("matrix-rust-sdk {}", crate::VERSION)).unwrap(),
HeaderValue::from_str(&format!("matrix-rust-sdk {}", crate::VERSION)).unwrap()
}
}; };
headers.insert(reqwest::header::USER_AGENT, user_agent); headers.insert(reqwest::header::USER_AGENT, user_agent);
@ -175,15 +166,10 @@ impl DefaultHttpClient {
#[allow(unused)] #[allow(unused)]
let _ = config; let _ = config;
Ok(Self { Ok(http_client.build()?)
inner: http_client.build()?,
})
} }
async fn response_to_http_response( async fn response_to_http_response(mut response: Response) -> Result<http::Response<Vec<u8>>> {
&self,
mut response: Response,
) -> Result<http::Response<Vec<u8>>> {
let status = response.status(); let status = response.status();
let mut http_builder = HttpResponse::builder().status(status); let mut http_builder = HttpResponse::builder().status(status);
@ -199,20 +185,16 @@ impl DefaultHttpClient {
Ok(http_builder.body(body).unwrap()) Ok(http_builder.body(body).unwrap())
} }
}
#[async_trait] #[async_trait]
impl HttpSend for DefaultHttpClient { impl HttpSend for Client {
async fn send_request( async fn send_request(
&self, &self,
request: http::Request<Vec<u8>>, request: http::Request<Vec<u8>>,
) -> Result<http::Response<Vec<u8>>> { ) -> Result<http::Response<Vec<u8>>> {
Ok(self Ok(
.response_to_http_response( response_to_http_response(self.execute(reqwest::Request::try_from(request)?).await?)
self.inner
.execute(reqwest::Request::try_from(request)?)
.await?, .await?,
) )
.await?)
} }
} }