Remove session from HttpClient

This commit is contained in:
Jonas Platte
2022-06-10 15:32:54 +02:00
committed by Jonas Platte
parent 6c8b520f14
commit 12d1607cdc
5 changed files with 21 additions and 30 deletions

View File

@@ -37,6 +37,7 @@ use matrix_sdk_crypto::{
store::{CryptoStore, MemoryStore as MemoryCryptoStore},
EncryptionSettings, MegolmError, OlmError, OlmMachine, ToDeviceRequest,
};
#[cfg(feature = "e2e-encryption")]
use once_cell::sync::OnceCell;
#[cfg(feature = "e2e-encryption")]
use ruma::events::{
@@ -137,7 +138,7 @@ impl BaseClient {
///
/// Returns a session object if the client is logged in. Otherwise returns
/// `None`.
pub fn session(&self) -> Arc<OnceCell<Session>> {
pub fn session(&self) -> Option<&Session> {
self.store.session()
}

View File

@@ -432,8 +432,8 @@ impl Store {
/// The current [`Session`] containing our user id, device id and access
/// token.
pub fn session(&self) -> Arc<OnceCell<Session>> {
self.session.clone()
pub fn session(&self) -> Option<&Session> {
self.session.get()
}
/// Get all the rooms this store knows about.

View File

@@ -293,15 +293,9 @@ impl ClientBuilder {
};
let base_client = BaseClient::with_store_config(self.store_config);
let session_cell = base_client.session();
let mk_http_client = |homeserver| {
HttpClient::new(
inner_http_client.clone(),
homeserver,
session_cell.clone(),
self.request_config,
)
HttpClient::new(inner_http_client.clone(), homeserver, self.request_config)
};
let homeserver = match homeserver_cfg {
@@ -313,6 +307,7 @@ impl ClientBuilder {
.send(
discover_homeserver::Request::new(),
None,
None,
[MatrixVersion::V1_0].into_iter().collect(),
)
.await

View File

@@ -283,7 +283,7 @@ impl Client {
/// Can be used with [`Client::restore_login`] to restore a previously
/// logged in session.
pub fn session(&self) -> Option<&Session> {
self.inner.http_client.session.get()
self.store().session()
}
/// Get a reference to the store.
@@ -1438,7 +1438,7 @@ impl Client {
Ok(self
.inner
.http_client
.send(request, Some(request_config), self.server_versions().await?)
.send(request, Some(request_config), self.session(), self.server_versions().await?)
.await?)
}
@@ -1491,7 +1491,10 @@ impl Client {
Request: OutgoingRequest + Debug,
HttpError: From<FromHttpResponseError<Request::EndpointError>>,
{
self.inner.http_client.send(request, config, self.server_versions().await?).await
self.inner
.http_client
.send(request, config, self.session(), self.server_versions().await?)
.await
}
async fn request_server_versions(&self) -> HttpResult<Arc<[MatrixVersion]>> {
@@ -1501,6 +1504,7 @@ impl Client {
.send(
get_supported_versions::Request::new(),
None,
None,
[MatrixVersion::V1_0].into_iter().collect(),
)
.await?
@@ -2398,7 +2402,6 @@ pub(crate) mod tests {
let logged_in = client.logged_in();
assert!(logged_in, "Client should be logged in");
assert!(client.inner.http_client.session.get().is_some());
assert_eq!(client.homeserver().await, homeserver);
}

View File

@@ -17,7 +17,6 @@ use std::{any::type_name, convert::TryFrom, fmt::Debug, sync::Arc, time::Duratio
use async_trait::async_trait;
use bytes::{Bytes, BytesMut};
use http::Response as HttpResponse;
use matrix_sdk_base::once_cell::sync::OnceCell;
use matrix_sdk_common::{locks::RwLock, AsyncTraitDeps};
use reqwest::Response;
use ruma::api::{
@@ -96,7 +95,6 @@ pub trait HttpSend: AsyncTraitDeps {
pub(crate) struct HttpClient {
pub(crate) inner: Arc<dyn HttpSend>,
pub(crate) homeserver: Arc<RwLock<Url>>,
pub(crate) session: Arc<OnceCell<Session>>,
pub(crate) request_config: RequestConfig,
}
@@ -104,10 +102,9 @@ impl HttpClient {
pub(crate) fn new(
inner: Arc<dyn HttpSend>,
homeserver: Arc<RwLock<Url>>,
session: Arc<OnceCell<Session>>,
request_config: RequestConfig,
) -> Self {
HttpClient { inner, homeserver, session, request_config }
HttpClient { inner, homeserver, request_config }
}
#[tracing::instrument(skip(self, request), fields(request_type = type_name::<Request>()))]
@@ -115,6 +112,7 @@ impl HttpClient {
&self,
request: Request,
config: Option<RequestConfig>,
session: Option<&Session>,
server_versions: Arc<[MatrixVersion]>,
) -> Result<Request::IncomingResponse, HttpError>
where
@@ -138,12 +136,12 @@ impl HttpClient {
// isn't going to be used anyways.
SendAccessToken::None
} else {
match self.session() {
Some(session) => {
match session {
Some(sess) => {
if config.force_auth {
SendAccessToken::Always(&session.access_token)
SendAccessToken::Always(&sess.access_token)
} else {
SendAccessToken::IfRequired(&session.access_token)
SendAccessToken::IfRequired(&sess.access_token)
}
}
None => SendAccessToken::None,
@@ -158,10 +156,8 @@ impl HttpClient {
} else {
request.try_into_http_request_with_user_id::<BytesMut>(
&self.homeserver.read().await.to_string(),
SendAccessToken::Always(
&self.session().ok_or(HttpError::UserIdRequired)?.access_token,
),
&self.session().ok_or(HttpError::UserIdRequired)?.user_id,
SendAccessToken::Always(&session.ok_or(HttpError::UserIdRequired)?.access_token),
&session.ok_or(HttpError::UserIdRequired)?.user_id,
&server_versions,
)?
};
@@ -176,10 +172,6 @@ impl HttpClient {
let response = Request::IncomingResponse::try_from_http_response(response)?;
Ok(response)
}
fn session(&self) -> Option<&Session> {
self.session.get()
}
}
#[derive(Debug)]