use std::sync::Arc;
use super::session::SessionBuilder;
use crate::client::endpoint::EndpointConfig;
#[derive(Debug, Clone)]
pub enum AuthMode {
Bearer,
Jwt {
ttl_seconds: i64,
},
}
#[derive(Clone)]
pub struct RealtimeClient {
api_key: Arc<String>,
auth: AuthMode,
endpoint_config: EndpointConfig,
}
impl RealtimeClient {
pub fn new(api_key: impl Into<String>) -> Self {
Self {
api_key: Arc::new(api_key.into()),
auth: AuthMode::Bearer,
endpoint_config: EndpointConfig::defaults()
.unwrap_or_else(|_| EndpointConfig::builder().build(false).unwrap()),
}
}
pub fn with_jwt(mut self, ttl_seconds: i64) -> Self {
self.auth = AuthMode::Jwt { ttl_seconds };
self
}
pub fn with_bearer(mut self) -> Self {
self.auth = AuthMode::Bearer;
self
}
pub fn with_endpoint_config(mut self, config: EndpointConfig) -> Self {
self.endpoint_config = config;
self
}
pub fn with_realtime_base(mut self, base: impl Into<String>) -> Self {
let leaked: &'static str = Box::leak(base.into().into_boxed_str());
self.endpoint_config = EndpointConfig::builder()
.realtime(leaked)
.build(false)
.unwrap();
self
}
pub fn session<M: super::RealtimeModel>(&self, model: M) -> SessionBuilder {
let realtime_url = self.realtime_url();
let model_name: String = model.into();
SessionBuilder::new(
Arc::clone(&self.api_key),
self.auth.clone(),
realtime_url,
model_name,
)
}
pub fn realtime_url(&self) -> String {
self.endpoint_config
.resolve_route(crate::client::routes::REALTIME_CONNECT, &[])
.unwrap_or_default()
}
pub fn auth(&self) -> &AuthMode {
&self.auth
}
pub fn api_key(&self) -> &str {
&self.api_key
}
}