matrix-sdk: Move the session into the http client wrapper.

master
Damir Jelić 2020-08-17 15:29:07 +02:00
parent 16a115d27e
commit 9e609a0fdf
3 changed files with 19 additions and 35 deletions

View File

@ -347,19 +347,20 @@ impl Client {
panic!("Error parsing homeserver url") panic!("Error parsing homeserver url")
}; };
let http_client = if let Some(client) = config.client { let client = if let Some(client) = config.client {
HttpClient { client
homeserver: homeserver.clone(),
inner: client,
}
} else { } else {
HttpClient { Arc::new(DefaultHttpClient::with_config(&config)?)
homeserver: homeserver.clone(),
inner: Arc::new(DefaultHttpClient::with_config(&config)?),
}
}; };
let base_client = BaseClient::new_with_config(config.base_config)?; let base_client = BaseClient::new_with_config(config.base_config)?;
let session = base_client.session().clone();
let http_client = HttpClient {
homeserver: homeserver.clone(),
inner: client,
session,
};
Ok(Self { Ok(Self {
homeserver, homeserver,
@ -1104,9 +1105,7 @@ impl Client {
Request: OutgoingRequest + Debug, Request: OutgoingRequest + Debug,
Error: From<FromHttpResponseError<Request::EndpointError>>, Error: From<FromHttpResponseError<Request::EndpointError>>,
{ {
self.http_client self.http_client.send(request).await
.send(request, self.base_client.session().clone())
.await
} }
#[cfg(feature = "encryption")] #[cfg(feature = "encryption")]
@ -1428,9 +1427,7 @@ impl Client {
.await .await
.map(|sas| Sas { .map(|sas| Sas {
inner: sas, inner: sas,
session: self.base_client.session().clone(),
http_client: self.http_client.clone(), http_client: self.http_client.clone(),
homeserver: self.homeserver.clone(),
}) })
} }
@ -1455,9 +1452,7 @@ impl Client {
Ok(Sas { Ok(Sas {
inner: sas, inner: sas,
session: self.base_client.session().clone(),
http_client: self.http_client.clone(), http_client: self.http_client.clone(),
homeserver: self.homeserver.clone(),
}) })
} }

View File

@ -68,6 +68,7 @@ pub trait HttpSend: Sync + Send + Debug {
pub(crate) struct HttpClient { pub(crate) struct HttpClient {
pub(crate) inner: Arc<dyn HttpSend>, pub(crate) inner: Arc<dyn HttpSend>,
pub(crate) homeserver: Arc<Url>, pub(crate) homeserver: Arc<Url>,
pub(crate) session: Arc<RwLock<Option<Session>>>,
} }
impl HttpClient { impl HttpClient {
@ -120,16 +121,12 @@ impl HttpClient {
Ok(http_builder.body(body).unwrap()) Ok(http_builder.body(body).unwrap())
} }
pub async fn send<Request>( pub async fn send<Request>(&self, request: Request) -> Result<Request::IncomingResponse>
&self,
request: Request,
session: Arc<RwLock<Option<Session>>>,
) -> Result<Request::IncomingResponse>
where where
Request: OutgoingRequest, Request: OutgoingRequest,
Error: From<FromHttpResponseError<Request::EndpointError>>, Error: From<FromHttpResponseError<Request::EndpointError>>,
{ {
let response = self.send_request(request, session).await?; let response = self.send_request(request, self.session.clone()).await?;
trace!("Got response: {:?}", response); trace!("Got response: {:?}", response);

View File

@ -12,14 +12,8 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
use std::sync::Arc; use matrix_sdk_base::{Device, Sas as BaseSas};
use matrix_sdk_common::api::r0::to_device::send_event_to_device::Request as ToDeviceRequest;
use url::Url;
use matrix_sdk_base::{Device, Sas as BaseSas, Session};
use matrix_sdk_common::{
api::r0::to_device::send_event_to_device::Request as ToDeviceRequest, locks::RwLock,
};
use crate::{error::Result, http_client::HttpClient}; use crate::{error::Result, http_client::HttpClient};
@ -27,9 +21,7 @@ use crate::{error::Result, http_client::HttpClient};
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct Sas { pub struct Sas {
pub(crate) inner: BaseSas, pub(crate) inner: BaseSas,
pub(crate) homeserver: Arc<Url>,
pub(crate) http_client: HttpClient, pub(crate) http_client: HttpClient,
pub(crate) session: Arc<RwLock<Option<Session>>>,
} }
impl Sas { impl Sas {
@ -42,7 +34,7 @@ impl Sas {
messages: req.messages, messages: req.messages,
}; };
self.http_client.send(request, self.session.clone()).await?; self.http_client.send(request).await?;
} }
Ok(()) Ok(())
} }
@ -56,7 +48,7 @@ impl Sas {
messages: req.messages, messages: req.messages,
}; };
self.http_client.send(request, self.session.clone()).await?; self.http_client.send(request).await?;
} }
Ok(()) Ok(())
@ -71,7 +63,7 @@ impl Sas {
messages: req.messages, messages: req.messages,
}; };
self.http_client.send(request, self.session.clone()).await?; self.http_client.send(request).await?;
} }
Ok(()) Ok(())
} }