pub mod event;
pub mod minter;
pub mod recorder;
#[cfg(feature = "actix")]
pub mod actix;
#[cfg(feature = "axum")]
pub mod axum;
#[cfg(feature = "tonic")]
pub mod tonic;
pub use event::Event;
use minter::MinterOptions;
use recorder::{BufferedRecorder, RecorderOptions};
pub const EVERSCRIBE_PROJECT_ID: &str = "EVERSCRIBE_PROJECT_ID";
pub const EVERSCRIBE_API_KEY: &str = "EVERSCRIBE_API_KEY";
#[derive(Debug, thiserror::Error)]
pub enum ConfigError {
#[error("everscribe: project_id is empty")]
EmptyProjectId,
#[error("everscribe: api_key is empty")]
EmptyApiKey,
#[error("everscribe: {0} is not set or empty")]
MissingEnv(&'static str),
}
#[derive(Clone, Debug)]
pub struct Client {
project_id: String,
api_key: String,
}
impl Client {
pub fn new(
project_id: impl Into<String>,
api_key: impl Into<String>,
) -> Result<Self, ConfigError> {
let project_id = project_id.into().trim().to_string();
if project_id.is_empty() {
return Err(ConfigError::EmptyProjectId);
}
let api_key = api_key.into().trim().to_string();
if api_key.is_empty() {
return Err(ConfigError::EmptyApiKey);
}
Ok(Client {
project_id,
api_key,
})
}
pub fn from_env() -> Result<Self, ConfigError> {
let project_id = std::env::var(EVERSCRIBE_PROJECT_ID).unwrap_or_default();
if project_id.trim().is_empty() {
return Err(ConfigError::MissingEnv(EVERSCRIBE_PROJECT_ID));
}
let api_key = std::env::var(EVERSCRIBE_API_KEY).unwrap_or_default();
if api_key.trim().is_empty() {
return Err(ConfigError::MissingEnv(EVERSCRIBE_API_KEY));
}
Client::new(project_id, api_key)
}
pub fn project_id(&self) -> &str {
&self.project_id
}
pub fn new_recorder(&self, opts: RecorderOptions) -> BufferedRecorder {
recorder::new(self.project_id.clone(), self.api_key.clone(), opts)
}
pub fn new_minter(&self, opts: MinterOptions) -> minter::Client {
minter::Client::new(self.project_id.clone(), self.api_key.clone(), opts)
}
}
pub fn new(
project_id: impl Into<String>,
api_key: impl Into<String>,
) -> Result<Client, ConfigError> {
Client::new(project_id, api_key)
}
pub fn new_from_env() -> Result<Client, ConfigError> {
Client::from_env()
}