2023-05-26 16:16:08 +02:00
|
|
|
#[cfg(feature = "client")]
|
2023-05-09 14:04:46 +02:00
|
|
|
pub mod api;
|
|
|
|
pub mod errors;
|
2023-05-26 16:16:08 +02:00
|
|
|
#[cfg(feature = "client")]
|
2023-05-09 14:04:46 +02:00
|
|
|
pub mod gateway;
|
2023-05-26 16:16:08 +02:00
|
|
|
#[cfg(feature = "client")]
|
2023-05-09 14:04:46 +02:00
|
|
|
pub mod instance;
|
2023-05-26 16:16:08 +02:00
|
|
|
#[cfg(feature = "client")]
|
2023-05-09 14:04:46 +02:00
|
|
|
pub mod limit;
|
2023-05-25 21:11:08 +02:00
|
|
|
pub mod types;
|
2023-05-26 16:16:08 +02:00
|
|
|
#[cfg(feature = "client")]
|
2023-05-09 14:04:46 +02:00
|
|
|
pub mod voice;
|
2023-04-05 21:54:27 +02:00
|
|
|
|
|
|
|
use url::{ParseError, Url};
|
2023-04-16 22:16:39 +02:00
|
|
|
#[derive(Clone, Default, Debug, PartialEq, Eq)]
|
2023-04-05 21:54:27 +02:00
|
|
|
|
|
|
|
/// A URLBundle is a struct which bundles together the API-, Gateway- and CDN-URLs of a Spacebar
|
|
|
|
/// instance.
|
|
|
|
pub struct URLBundle {
|
|
|
|
pub api: String,
|
|
|
|
pub wss: String,
|
|
|
|
pub cdn: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl URLBundle {
|
|
|
|
pub fn new(api: String, wss: String, cdn: String) -> Self {
|
|
|
|
Self {
|
|
|
|
api: URLBundle::parse_url(api),
|
|
|
|
wss: URLBundle::parse_url(wss),
|
|
|
|
cdn: URLBundle::parse_url(cdn),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// parse(url: String) parses a URL using the Url library and formats it in a standardized
|
|
|
|
/// way. If no protocol is given, HTTP (not HTTPS) is assumed.
|
|
|
|
/// # Example:
|
2023-04-05 22:19:15 +02:00
|
|
|
/// ```rs
|
2023-04-05 21:54:27 +02:00
|
|
|
/// let url = parse_url("localhost:3000");
|
|
|
|
/// ```
|
|
|
|
/// `-> Outputs "http://localhost:3000".`
|
|
|
|
pub fn parse_url(url: String) -> String {
|
|
|
|
let url = match Url::parse(&url) {
|
|
|
|
Ok(url) => {
|
|
|
|
if url.scheme() == "localhost" {
|
2023-04-15 13:27:34 +02:00
|
|
|
return URLBundle::parse_url(format!("http://{}", url));
|
2023-04-05 21:54:27 +02:00
|
|
|
}
|
|
|
|
url
|
|
|
|
}
|
|
|
|
Err(ParseError::RelativeUrlWithoutBase) => {
|
2023-04-15 13:27:34 +02:00
|
|
|
let url_fmt = format!("http://{}", url);
|
|
|
|
return URLBundle::parse_url(url_fmt);
|
2023-04-05 21:54:27 +02:00
|
|
|
}
|
|
|
|
Err(_) => panic!("Invalid URL"),
|
|
|
|
};
|
2023-04-15 13:03:51 +02:00
|
|
|
// if the last character of the string is a slash, remove it.
|
|
|
|
let mut url_string = url.to_string();
|
2023-04-25 17:41:14 +02:00
|
|
|
if url_string.ends_with('/') {
|
2023-04-15 13:03:51 +02:00
|
|
|
url_string.pop();
|
|
|
|
}
|
2023-04-25 17:41:14 +02:00
|
|
|
url_string
|
2023-04-05 21:54:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_api(&self) -> &str {
|
|
|
|
&self.api
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_cdn(&self) -> &str {
|
|
|
|
&self.cdn
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_wss(&self) -> &str {
|
|
|
|
&self.wss
|
|
|
|
}
|
2023-04-04 17:37:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
2023-04-15 13:27:34 +02:00
|
|
|
mod lib {
|
2023-04-04 17:37:11 +02:00
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
2023-04-05 21:54:27 +02:00
|
|
|
fn test_parse_url() {
|
2023-04-15 13:27:34 +02:00
|
|
|
let mut result = URLBundle::parse_url(String::from("localhost:3000/"));
|
2023-04-05 21:54:27 +02:00
|
|
|
assert_eq!(result, String::from("http://localhost:3000"));
|
2023-04-15 13:27:34 +02:00
|
|
|
result = URLBundle::parse_url(String::from("https://some.url.com/"));
|
|
|
|
assert_eq!(result, String::from("https://some.url.com"));
|
|
|
|
result = URLBundle::parse_url(String::from("https://some.url.com/"));
|
|
|
|
assert_eq!(result, String::from("https://some.url.com"));
|
|
|
|
result = URLBundle::parse_url(String::from("https://some.url.com"));
|
|
|
|
assert_eq!(result, String::from("https://some.url.com"));
|
2023-04-04 17:37:11 +02:00
|
|
|
}
|
|
|
|
}
|