Skip to content

Commit

Permalink
feat(appservice): Add default request config
Browse files Browse the repository at this point in the history
This adds a default RequestConfig to use when creating new virtual
clients.
  • Loading branch information
emgrav committed Sep 22, 2022
1 parent 2818551 commit 7417d86
Showing 1 changed file with 34 additions and 3 deletions.
37 changes: 34 additions & 3 deletions crates/matrix-sdk-appservice/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ use event_handler::AppserviceFn;
pub use matrix_sdk;
#[doc(no_inline)]
pub use matrix_sdk::ruma;
use matrix_sdk::{reqwest::Url, Client, ClientBuilder};
use matrix_sdk::{config::RequestConfig, reqwest::Url, Client, ClientBuilder};
use ruma::{
api::{
appservice::{
Expand Down Expand Up @@ -141,6 +141,7 @@ pub struct AppService {
namespaces: Arc<NamespaceCache>,
clients: Arc<DashMap<Localpart, Client>>,
event_handler: event_handler::EventHandler,
default_request_config: Option<RequestConfig>,
}

/// Builder for an AppService
Expand All @@ -150,6 +151,7 @@ pub struct AppServiceBuilder {
server_name: OwnedServerName,
registration: AppServiceRegistration,
client_builder: Option<ClientBuilder>,
default_request_config: Option<RequestConfig>,
}

impl AppServiceBuilder {
Expand All @@ -169,7 +171,13 @@ impl AppServiceBuilder {
server_name: OwnedServerName,
registration: AppServiceRegistration,
) -> Self {
AppServiceBuilder { homeserver_url, server_name, registration, client_builder: None }
AppServiceBuilder {
homeserver_url,
server_name,
registration,
client_builder: None,
default_request_config: None,
}
}

/// Set the client builder to use for the virtual user.
Expand All @@ -178,6 +186,12 @@ impl AppServiceBuilder {
self
}

/// Set the default `[RequestConfig]` to use for virtual users.
pub fn default_request_config(mut self, default_request_config: RequestConfig) -> Self {
self.default_request_config = Some(default_request_config);
self
}

/// Build the AppService.
///
/// This will also construct a [`virtual_user()`][AppService::virtual_user]
Expand All @@ -193,6 +207,7 @@ impl AppServiceBuilder {
let clients = Arc::new(DashMap::new());
let sender_localpart = registration.sender_localpart.clone();
let event_handler = event_handler::EventHandler::default();
let default_request_config = self.default_request_config;

let appservice = AppService {
homeserver_url,
Expand All @@ -201,6 +216,7 @@ impl AppServiceBuilder {
namespaces,
clients,
event_handler,
default_request_config,
};
if let Some(client_builder) = self.client_builder {
appservice
Expand Down Expand Up @@ -248,7 +264,13 @@ impl AppService {
/// [assert the identity]: https://matrix.org/docs/spec/application_service/r0.1.2#identity-assertion
pub async fn virtual_user(&self, localpart: Option<&str>) -> Result<Client> {
let localpart = localpart.unwrap_or_else(|| self.registration.sender_localpart.as_ref());
self.virtual_user_builder(localpart).build().await
let builder = match self.default_request_config {
Some(config) => self
.virtual_user_builder(localpart)
.client_builder(Client::builder().request_config(config)),
None => self.virtual_user_builder(localpart),
};
builder.build().await
}

/// Same as [`virtual_user()`][Self::virtual_user] but with
Expand Down Expand Up @@ -542,6 +564,15 @@ impl AppService {
webserver::run_server(self.clone(), host, port).await?;
Ok(())
}

/// Set the default RequestConfig
pub fn set_default_request_config(
&mut self,
request_config: Option<RequestConfig>,
) -> Result<()> {
self.default_request_config = request_config;
Ok(())
}
}

#[cfg(test)]
Expand Down

0 comments on commit 7417d86

Please sign in to comment.