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

This commit is contained in:
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")
};
let http_client = if let Some(client) = config.client {
HttpClient {
homeserver: homeserver.clone(),
inner: client,
}
let client = if let Some(client) = config.client {
client
} else {
HttpClient {
homeserver: homeserver.clone(),
inner: Arc::new(DefaultHttpClient::with_config(&config)?),
}
Arc::new(DefaultHttpClient::with_config(&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 {
homeserver,
@ -1104,9 +1105,7 @@ impl Client {
Request: OutgoingRequest + Debug,
Error: From<FromHttpResponseError<Request::EndpointError>>,
{
self.http_client
.send(request, self.base_client.session().clone())
.await
self.http_client.send(request).await
}
#[cfg(feature = "encryption")]
@ -1428,9 +1427,7 @@ impl Client {
.await
.map(|sas| Sas {
inner: sas,
session: self.base_client.session().clone(),
http_client: self.http_client.clone(),
homeserver: self.homeserver.clone(),
})
}
@ -1455,9 +1452,7 @@ impl Client {
Ok(Sas {
inner: sas,
session: self.base_client.session().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) inner: Arc<dyn HttpSend>,
pub(crate) homeserver: Arc<Url>,
pub(crate) session: Arc<RwLock<Option<Session>>>,
}
impl HttpClient {
@ -120,16 +121,12 @@ impl HttpClient {
Ok(http_builder.body(body).unwrap())
}
pub async fn send<Request>(
&self,
request: Request,
session: Arc<RwLock<Option<Session>>>,
) -> Result<Request::IncomingResponse>
pub async fn send<Request>(&self, request: Request) -> Result<Request::IncomingResponse>
where
Request: OutgoingRequest,
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);

View file

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