use std::sync::Arc;
use super::session::SessionBuilder;
use crate::client::endpoints::{ApiBase, 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::default(),
}
}
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 {
self.endpoint_config = self.endpoint_config.with_realtime_base(base);
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.url(&ApiBase::Realtime, "")
}
pub fn auth(&self) -> &AuthMode {
&self.auth
}
pub fn api_key(&self) -> &str {
&self.api_key
}
}