use crate::config::{DEFAULT_BASE_URL, DEFAULT_WS_URL, RadionConfig};
use crate::error::{RadionError, Result};
#[derive(Debug)]
#[non_exhaustive]
pub struct Radion {
pub config: RadionConfig,
#[cfg(feature = "realtime")]
#[cfg_attr(docsrs, doc(cfg(feature = "realtime")))]
pub realtime: crate::realtime::RealtimeClient,
}
impl Radion {
pub fn builder() -> RadionBuilder {
RadionBuilder::default()
}
}
#[derive(Debug, Default)]
pub struct RadionBuilder {
api_key: Option<String>,
base_url: Option<String>,
ws_url: Option<String>,
#[cfg(feature = "realtime")]
token_provider: Option<crate::realtime::TokenProvider>,
#[cfg(feature = "realtime")]
auth_in_query: bool,
#[cfg(feature = "compression")]
compression: bool,
}
impl RadionBuilder {
#[must_use]
pub fn api_key(mut self, api_key: impl Into<String>) -> Self {
self.api_key = Some(api_key.into());
self
}
#[must_use]
pub fn base_url(mut self, base_url: impl Into<String>) -> Self {
self.base_url = Some(base_url.into());
self
}
#[must_use]
pub fn ws_url(mut self, ws_url: impl Into<String>) -> Self {
self.ws_url = Some(ws_url.into());
self
}
#[cfg(feature = "realtime")]
#[cfg_attr(docsrs, doc(cfg(feature = "realtime")))]
#[must_use]
pub fn token(mut self, token: impl Into<String>) -> Self {
self.token_provider = Some(crate::realtime::TokenProvider::from_static(token));
self
}
#[cfg(feature = "realtime")]
#[cfg_attr(docsrs, doc(cfg(feature = "realtime")))]
#[must_use]
pub fn token_provider(mut self, provider: crate::realtime::TokenProvider) -> Self {
self.token_provider = Some(provider);
self
}
#[cfg(feature = "realtime")]
#[cfg_attr(docsrs, doc(cfg(feature = "realtime")))]
#[must_use]
pub fn auth_in_query(mut self, enabled: bool) -> Self {
self.auth_in_query = enabled;
self
}
#[cfg(feature = "compression")]
#[cfg_attr(docsrs, doc(cfg(feature = "compression")))]
#[must_use]
pub fn compression(mut self, enabled: bool) -> Self {
self.compression = enabled;
self
}
pub fn build(self) -> Result<Radion> {
let api_key = self.api_key.unwrap_or_default();
if api_key.is_empty() {
return Err(RadionError::connection("api_key is required"));
}
let config = RadionConfig {
api_key: api_key.clone(),
base_url: self
.base_url
.unwrap_or_else(|| DEFAULT_BASE_URL.to_string()),
ws_url: self.ws_url.unwrap_or_else(|| DEFAULT_WS_URL.to_string()),
};
#[cfg(feature = "realtime")]
let realtime = {
let mut options = crate::realtime::RealtimeOptions::new(api_key)
.url(config.ws_url.clone())
.auth_in_query(self.auth_in_query);
#[cfg(feature = "compression")]
{
options = options.compression(self.compression);
}
if let Some(provider) = self.token_provider {
options = options.token_provider(provider);
}
crate::realtime::RealtimeClient::new(options)
};
Ok(Radion {
config,
#[cfg(feature = "realtime")]
realtime,
})
}
}
#[cfg(all(test, feature = "realtime"))]
mod builder_tests {
use super::*;
#[test]
fn builder_accepts_token_and_query_flag() {
let radion = Radion::builder()
.api_key("pk_jwt_x")
.token("jwt")
.auth_in_query(true)
.build()
.expect("builds");
let _ = radion;
}
#[cfg(feature = "compression")]
#[test]
fn builder_forwards_compression_to_the_realtime_options() {
let radion = Radion::builder()
.api_key("sk_x")
.compression(true)
.build()
.expect("builds");
let _ = radion;
}
}