Make instance GatewayOptions configurable for library consumers (#555)

* Make instance GatewayOptions configurable for library consumers

* Update example in README
This commit is contained in:
Flori 2024-08-26 19:36:42 +02:00 committed by GitHub
parent d846ce9948
commit 76186a08f0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 19 additions and 11 deletions

View File

@ -57,7 +57,7 @@ use chorus::instance::Instance;
#[tokio::main]
async fn main() {
let instance = Instance::new("https://example.com")
let instance = Instance::new("https://example.com", None)
.await
.expect("Failed to connect to the Spacebar server");
// You can create as many instances of `Instance` as you want, but each `Instance` should likely be unique.

View File

@ -6,7 +6,7 @@ use chorus::instance::Instance;
#[tokio::main(flavor = "current_thread")]
async fn main() {
let instance = Instance::new("https://example.com/")
let instance = Instance::new("https://example.com/", None)
.await
.expect("Failed to connect to the Spacebar server");
dbg!(instance.instance_info);

View File

@ -7,7 +7,7 @@ use chorus::types::LoginSchema;
#[tokio::main(flavor = "current_thread")]
async fn main() {
let mut instance = Instance::new("https://example.com/")
let mut instance = Instance::new("https://example.com/", None)
.await
.expect("Failed to connect to the Spacebar server");
// Assume, you already have an account created on this instance. Registering an account works

View File

@ -69,8 +69,13 @@ impl Instance {
/// Creates a new [`Instance`] from the [relevant instance urls](UrlBundle).
///
/// If `options` is `None`, the default [`GatewayOptions`] will be used.
///
/// To create an Instance from one singular url, use [`Instance::new()`].
pub async fn from_url_bundle(urls: UrlBundle) -> ChorusResult<Instance> {
pub async fn from_url_bundle(
urls: UrlBundle,
options: Option<GatewayOptions>,
) -> ChorusResult<Instance> {
let is_limited: Option<LimitsConfiguration> = Instance::is_limited(&urls.api).await?;
let limit_information;
@ -89,7 +94,7 @@ impl Instance {
instance_info: GeneralConfiguration::default(),
limits_information: limit_information,
client: Client::new(),
gateway_options: GatewayOptions::default(),
gateway_options: options.unwrap_or_default(),
};
instance.instance_info = match instance.general_configuration_schema().await {
Ok(schema) => schema,
@ -103,10 +108,12 @@ impl Instance {
/// Creates a new [`Instance`] by trying to get the [relevant instance urls](UrlBundle) from a root url.
///
/// If `options` is `None`, the default [`GatewayOptions`] will be used.
///
/// Shorthand for `Instance::from_url_bundle(UrlBundle::from_root_domain(root_domain).await?)`.
pub async fn new(root_url: &str) -> ChorusResult<Instance> {
pub async fn new(root_url: &str, options: Option<GatewayOptions>) -> ChorusResult<Instance> {
let urls = UrlBundle::from_root_url(root_url).await?;
Instance::from_url_bundle(urls).await
Instance::from_url_bundle(urls, options).await
}
pub async fn is_limited(api_url: &str) -> ChorusResult<Option<LimitsConfiguration>> {
@ -199,10 +206,9 @@ impl ChorusUser {
let settings = Arc::new(RwLock::new(UserSettings::default()));
let object = Arc::new(RwLock::new(User::default()));
let wss_url = &instance.read().unwrap().urls.wss.clone();
let gateway_options = instance.read().unwrap().gateway_options;
// Dummy gateway object
let gateway = Gateway::spawn(wss_url, GatewayOptions::default())
.await
.unwrap();
let gateway = Gateway::spawn(wss_url, gateway_options).await.unwrap();
ChorusUser {
token: token.to_string(),
belongs_to: instance.clone(),

View File

@ -66,7 +66,9 @@ pub(crate) async fn setup() -> TestBundle {
)
.init();
let instance = Instance::new("http://localhost:3001/api").await.unwrap();
let instance = Instance::new("http://localhost:3001/api", None)
.await
.unwrap();
// Requires the existence of the below user.
let reg = RegisterSchema {
username: "integrationtestuser".into(),