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::{
events::{room::message::MessageEventContent, AnyMessageEventContent},
http_client::{DefaultHttpClient, HttpClient, HttpSend},
http_client::{client_with_config, HttpClient, HttpSend},
identifiers::{EventId, RoomId, RoomIdOrAliasId, UserId},
Error, EventEmitter, OutgoingRequest, Result,
};
@ -353,7 +353,7 @@ impl Client {
let client = if let Some(client) = config.client {
client
} else {
Arc::new(DefaultHttpClient::with_config(&config)?)
Arc::new(client_with_config(&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.
pub fn with_config(config: &ClientConfig) -> Result<Self> {
/// Build a client with the specified configuration.
pub(crate) fn client_with_config(config: &ClientConfig) -> Result<Client> {
let http_client = reqwest::Client::builder();
#[cfg(not(target_arch = "wasm32"))]
@ -161,9 +154,7 @@ impl DefaultHttpClient {
let user_agent = match &config.user_agent {
Some(a) => a.clone(),
None => {
HeaderValue::from_str(&format!("matrix-rust-sdk {}", crate::VERSION)).unwrap()
}
None => HeaderValue::from_str(&format!("matrix-rust-sdk {}", crate::VERSION)).unwrap(),
};
headers.insert(reqwest::header::USER_AGENT, user_agent);
@ -175,15 +166,10 @@ impl DefaultHttpClient {
#[allow(unused)]
let _ = config;
Ok(Self {
inner: http_client.build()?,
})
}
Ok(http_client.build()?)
}
async fn response_to_http_response(
&self,
mut response: Response,
) -> Result<http::Response<Vec<u8>>> {
async fn response_to_http_response(mut response: Response) -> Result<http::Response<Vec<u8>>> {
let status = response.status();
let mut http_builder = HttpResponse::builder().status(status);
@ -198,21 +184,17 @@ impl DefaultHttpClient {
let body = response.bytes().await?.as_ref().to_owned();
Ok(http_builder.body(body).unwrap())
}
}
#[async_trait]
impl HttpSend for DefaultHttpClient {
impl HttpSend for Client {
async fn send_request(
&self,
request: http::Request<Vec<u8>>,
) -> Result<http::Response<Vec<u8>>> {
Ok(self
.response_to_http_response(
self.inner
.execute(reqwest::Request::try_from(request)?)
Ok(
response_to_http_response(self.execute(reqwest::Request::try_from(request)?).await?)
.await?,
)
.await?)
}
}