matrix-sdk: Pass the rwlock to the http client when doing requests.

master
Damir Jelić 2020-07-29 13:50:01 +02:00
parent 4634efc092
commit 2b124d98bc
2 changed files with 8 additions and 6 deletions

View File

@ -1095,7 +1095,7 @@ impl Client {
request: Request, request: Request,
) -> Result<Request::Response> { ) -> Result<Request::Response> {
self.http_client self.http_client
.send(request, self.base_client.session().read().await.as_ref()) .send(request, self.base_client.session().clone())
.await .await
} }
@ -1137,7 +1137,7 @@ impl Client {
request: Request, request: Request,
) -> Result<Request::Response> { ) -> Result<Request::Response> {
self.http_client self.http_client
.send_uiaa(request, self.base_client.session().read().await.as_ref()) .send_uiaa(request, self.base_client.session().clone())
.await .await
} }

View File

@ -20,6 +20,8 @@ use reqwest::{header::AUTHORIZATION, Client, Response};
use tracing::trace; use tracing::trace;
use url::Url; use url::Url;
use matrix_sdk_common::locks::RwLock;
use crate::{api::r0::uiaa::UiaaResponse, Endpoint, Error, Result, Session}; use crate::{api::r0::uiaa::UiaaResponse, Endpoint, Error, Result, Session};
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
@ -34,7 +36,7 @@ impl HttpClient {
requires_auth: bool, requires_auth: bool,
method: HttpMethod, method: HttpMethod,
request: http::Request<Vec<u8>>, request: http::Request<Vec<u8>>,
session: Option<&Session>, session: Arc<RwLock<Option<Session>>>,
) -> Result<Response> { ) -> Result<Response> {
let url = request.uri(); let url = request.uri();
let path_and_query = url.path_and_query().unwrap(); let path_and_query = url.path_and_query().unwrap();
@ -70,7 +72,7 @@ impl HttpClient {
}; };
let request_builder = if requires_auth { let request_builder = if requires_auth {
if let Some(session) = session { if let Some(session) = session.read().await.as_ref() {
let header_value = format!("Bearer {}", &session.access_token); let header_value = format!("Bearer {}", &session.access_token);
request_builder.header(AUTHORIZATION, header_value) request_builder.header(AUTHORIZATION, header_value)
} else { } else {
@ -103,7 +105,7 @@ impl HttpClient {
pub async fn send<Request: Endpoint<ResponseError = crate::api::Error> + std::fmt::Debug>( pub async fn send<Request: Endpoint<ResponseError = crate::api::Error> + std::fmt::Debug>(
&self, &self,
request: Request, request: Request,
session: Option<&Session>, session: Arc<RwLock<Option<Session>>>,
) -> Result<Request::Response> { ) -> Result<Request::Response> {
let request: http::Request<Vec<u8>> = request.try_into()?; let request: http::Request<Vec<u8>> = request.try_into()?;
let response = self let response = self
@ -125,7 +127,7 @@ impl HttpClient {
pub async fn send_uiaa<Request: Endpoint<ResponseError = UiaaResponse> + std::fmt::Debug>( pub async fn send_uiaa<Request: Endpoint<ResponseError = UiaaResponse> + std::fmt::Debug>(
&self, &self,
request: Request, request: Request,
session: Option<&Session>, session: Arc<RwLock<Option<Session>>>,
) -> Result<Request::Response> { ) -> Result<Request::Response> {
let request: http::Request<Vec<u8>> = request.try_into()?; let request: http::Request<Vec<u8>> = request.try_into()?;
let response = self let response = self