diff --git a/Cargo.lock b/Cargo.lock index e41d159..e2e0e06 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -209,7 +209,7 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "chorus" -version = "0.13.0" +version = "0.14.0" dependencies = [ "async-trait", "base64 0.21.7", diff --git a/Cargo.toml b/Cargo.toml index d77f408..8be8424 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "chorus" description = "A library for interacting with multiple Spacebar-compatible Instances at once." -version = "0.13.0" +version = "0.14.0" license = "AGPL-3.0" edition = "2021" repository = "https://github.com/polyphony-chat/chorus" diff --git a/README.md b/README.md index af2ab6e..a7510ec 100644 --- a/README.md +++ b/README.md @@ -44,7 +44,7 @@ To get started with Chorus, import it into your project by adding the following ```toml [dependencies] -chorus = "0.13.0" +chorus = "0.14.0" ``` ### Establishing a Connection diff --git a/examples/instance.rs b/examples/instance.rs index b8f8518..2ec45a3 100644 --- a/examples/instance.rs +++ b/examples/instance.rs @@ -1,14 +1,8 @@ use chorus::instance::Instance; -use chorus::UrlBundle; #[tokio::main(flavor = "current_thread")] async fn main() { - let bundle = UrlBundle::new( - "https://example.com/api".to_string(), - "wss://example.com/".to_string(), - "https://example.com/cdn".to_string(), - ); - let instance = Instance::new(bundle) + let instance = Instance::new("https://example.com/") .await .expect("Failed to connect to the Spacebar server"); dbg!(instance.instance_info); diff --git a/examples/login.rs b/examples/login.rs index 18b5db4..144030b 100644 --- a/examples/login.rs +++ b/examples/login.rs @@ -1,15 +1,9 @@ use chorus::instance::Instance; use chorus::types::LoginSchema; -use chorus::UrlBundle; #[tokio::main(flavor = "current_thread")] async fn main() { - let bundle = UrlBundle::new( - "https://example.com/api".to_string(), - "wss://example.com/".to_string(), - "https://example.com/cdn".to_string(), - ); - let mut instance = Instance::new(bundle) + let mut instance = Instance::new("https://example.com/") .await .expect("Failed to connect to the Spacebar server"); // Assume, you already have an account created on this instance. Registering an account works diff --git a/src/instance.rs b/src/instance.rs index f151ced..a4967e8 100644 --- a/src/instance.rs +++ b/src/instance.rs @@ -73,7 +73,7 @@ impl PartialEq for LimitsInformation { impl Instance { /// Creates a new [`Instance`] from the [relevant instance urls](UrlBundle). To create an Instance from one singular url, use [`Instance::from_root_url()`]. - pub async fn new(urls: UrlBundle) -> ChorusResult { + async fn from_url_bundle(urls: UrlBundle) -> ChorusResult { let is_limited: Option = Instance::is_limited(&urls.api).await?; let limit_information; @@ -114,9 +114,9 @@ impl Instance { /// Shorthand for `Instance::new(UrlBundle::from_root_domain(root_domain).await?)`. /// /// If `limited` is `true`, then Chorus will track and enforce rate limits for this instance. - pub async fn from_root_url(root_url: &str) -> ChorusResult { + pub async fn new(root_url: &str) -> ChorusResult { let urls = UrlBundle::from_root_url(root_url).await?; - Instance::new(urls).await + Instance::from_url_bundle(urls).await } pub async fn is_limited(api_url: &str) -> ChorusResult> { diff --git a/src/lib.rs b/src/lib.rs index f1a3591..771b5b5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -140,6 +140,12 @@ pub mod voice; /// # Notes /// All the urls can be found on the /api/policies/instance/domains endpoint of a spacebar server pub struct UrlBundle { + /// The root url of an Instance. Usually, this would be the url where `.well-known/spacebar` can + /// be located under. If the instance you are connecting to for some reason does not have a + /// `.well-known` set up (for example, if it is a local/testing instance), you can use the api + /// url as a substitute. + /// Ex: `https://spacebar.chat` + pub root: String, /// The api's url. /// Ex: `https://old.server.spacebar.chat/api` pub api: String, @@ -154,8 +160,9 @@ pub struct UrlBundle { impl UrlBundle { /// Creates a new UrlBundle from the relevant urls. - pub fn new(api: String, wss: String, cdn: String) -> Self { + pub fn new(root: String, api: String, wss: String, cdn: String) -> Self { Self { + root: UrlBundle::parse_url(root), api: UrlBundle::parse_url(api), wss: UrlBundle::parse_url(wss), cdn: UrlBundle::parse_url(cdn), @@ -240,7 +247,12 @@ impl UrlBundle { .json::() .await { - Ok(UrlBundle::new(body.api_endpoint, body.gateway, body.cdn)) + Ok(UrlBundle::new( + url.to_string(), + body.api_endpoint, + body.gateway, + body.cdn, + )) } else { Err(ChorusError::RequestFailed { url: url.to_string(), diff --git a/tests/common/mod.rs b/tests/common/mod.rs index d6aaa34..bce419a 100644 --- a/tests/common/mod.rs +++ b/tests/common/mod.rs @@ -52,12 +52,7 @@ impl TestBundle { // Set up a test by creating an Instance and a User. Reduces Test boilerplate. pub(crate) async fn setup() -> TestBundle { - let urls = UrlBundle::new( - "http://localhost:3001/api".to_string(), - "ws://localhost:3001".to_string(), - "http://localhost:3001".to_string(), - ); - let instance = Instance::new(urls.clone()).await.unwrap(); + let instance = Instance::new("http://localhost:3001/api").await.unwrap(); // Requires the existance of the below user. let reg = RegisterSchema { username: "integrationtestuser".into(), @@ -114,6 +109,12 @@ pub(crate) async fn setup() -> TestBundle { .await .unwrap(); + let urls = UrlBundle::new( + "http://localhost:3001/api".to_string(), + "http://localhost:3001/api".to_string(), + "ws://localhost:3001".to_string(), + "http://localhost:3001".to_string(), + ); TestBundle { urls, user,