appservice: Rely on cfg-toggle in send_request
parent
eece920953
commit
3b24d33822
|
@ -405,6 +405,7 @@ pub struct RequestConfig {
|
|||
pub(crate) retry_limit: Option<u64>,
|
||||
pub(crate) retry_timeout: Option<Duration>,
|
||||
pub(crate) force_auth: bool,
|
||||
#[cfg(feature = "appservice")]
|
||||
pub(crate) assert_identity: bool,
|
||||
}
|
||||
|
||||
|
@ -427,6 +428,7 @@ impl Default for RequestConfig {
|
|||
retry_limit: Default::default(),
|
||||
retry_timeout: Default::default(),
|
||||
force_auth: false,
|
||||
#[cfg(feature = "appservice")]
|
||||
assert_identity: false,
|
||||
}
|
||||
}
|
||||
|
|
|
@ -115,7 +115,26 @@ impl HttpClient {
|
|||
None => self.request_config,
|
||||
};
|
||||
|
||||
#[cfg(not(feature = "appservice"))]
|
||||
let request = self.try_into_http_request(request, session, config).await?;
|
||||
|
||||
#[cfg(feature = "appservice")]
|
||||
let request = if !self.request_config.assert_identity {
|
||||
self.try_into_http_request(request, session, config).await?
|
||||
} else {
|
||||
self.try_into_http_request_with_identy_assertion(request, session, config)
|
||||
.await?
|
||||
};
|
||||
|
||||
self.inner.send_request(request, config).await
|
||||
}
|
||||
|
||||
async fn try_into_http_request<Request: OutgoingRequest>(
|
||||
&self,
|
||||
request: Request,
|
||||
session: Arc<RwLock<Option<Session>>>,
|
||||
config: RequestConfig,
|
||||
) -> Result<http::Request<Bytes>, HttpError> {
|
||||
let read_guard;
|
||||
let access_token = if config.force_auth {
|
||||
read_guard = session.read().await;
|
||||
|
@ -140,17 +159,20 @@ impl HttpClient {
|
|||
}
|
||||
};
|
||||
|
||||
request
|
||||
let http_request = request
|
||||
.try_into_http_request::<BytesMut>(&self.homeserver.to_string(), access_token)?
|
||||
.map(|body| body.freeze())
|
||||
} else {
|
||||
// this should be unreachable since assert_identity on request_config can only be set
|
||||
// with the appservice feature active
|
||||
#[cfg(not(feature = "appservice"))]
|
||||
return Err(HttpError::NeedsAppserviceFeature);
|
||||
.map(|body| body.freeze());
|
||||
|
||||
Ok(http_request)
|
||||
}
|
||||
|
||||
#[cfg(feature = "appservice")]
|
||||
{
|
||||
async fn try_into_http_request_with_identy_assertion<Request: OutgoingRequest>(
|
||||
&self,
|
||||
request: Request,
|
||||
session: Arc<RwLock<Option<Session>>>,
|
||||
_: RequestConfig,
|
||||
) -> Result<http::Request<Bytes>, HttpError> {
|
||||
let read_guard = session.read().await;
|
||||
let access_token = if let Some(session) = read_guard.as_ref() {
|
||||
SendAccessToken::Always(session.access_token.as_str())
|
||||
|
@ -164,17 +186,15 @@ impl HttpClient {
|
|||
return Err(HttpError::UserIdRequired);
|
||||
};
|
||||
|
||||
request
|
||||
let http_request = request
|
||||
.try_into_http_request_with_user_id::<BytesMut>(
|
||||
&self.homeserver.to_string(),
|
||||
access_token,
|
||||
user_id,
|
||||
)?
|
||||
.map(|body| body.freeze())
|
||||
}
|
||||
};
|
||||
.map(|body| body.freeze());
|
||||
|
||||
self.inner.send_request(request, config).await
|
||||
Ok(http_request)
|
||||
}
|
||||
|
||||
pub async fn upload(
|
||||
|
|
Loading…
Reference in New Issue