use crate::endpoints::{EndpointManager, DEFAULT_HOST};
use derive_builder::Builder;
use tracing::warn;
#[cfg(not(feature = "async-client"))]
mod blocking;
#[cfg(not(feature = "async-client"))]
pub use blocking::client;
#[cfg(not(feature = "async-client"))]
pub use blocking::Client;
#[cfg(feature = "async-client")]
mod async_client;
#[cfg(feature = "async-client")]
pub use async_client::client;
#[cfg(feature = "async-client")]
pub use async_client::Client;
#[derive(Builder, Clone)]
pub struct ClientOptions {
#[builder(setter(into, strip_option), default)]
host: Option<String>,
api_key: String,
#[builder(default = "30")]
request_timeout_seconds: u64,
#[builder(setter(into, strip_option), default)]
personal_api_key: Option<String>,
#[builder(default = "false")]
enable_local_evaluation: bool,
#[builder(default = "30")]
poll_interval_seconds: u64,
#[builder(default = "false")]
disabled: bool,
#[builder(default = "false")]
disable_geoip: bool,
#[builder(default = "3")]
feature_flags_request_timeout_seconds: u64,
#[builder(default = "false")]
local_evaluation_only: bool,
#[builder(setter(skip))]
#[builder(default = "EndpointManager::new(DEFAULT_HOST.to_string())")]
endpoint_manager: EndpointManager,
}
impl ClientOptions {
pub(crate) fn endpoints(&self) -> &EndpointManager {
&self.endpoint_manager
}
pub fn is_disabled(&self) -> bool {
self.disabled
}
fn sanitize(mut self) -> Self {
self.api_key = self.api_key.trim().to_string();
if self.api_key.is_empty() {
warn!("api_key is empty after trimming whitespace; check your project API key");
}
self.host = Some(match self.host {
Some(host) => {
let normalized = host.trim().to_string();
if normalized.is_empty() {
DEFAULT_HOST.to_string()
} else {
normalized
}
}
None => DEFAULT_HOST.to_string(),
});
self.personal_api_key = self.personal_api_key.and_then(|personal_api_key| {
let normalized = personal_api_key.trim().to_string();
if normalized.is_empty() {
None
} else {
Some(normalized)
}
});
self.endpoint_manager = EndpointManager::new(
self.host
.clone()
.expect("host is always normalized in sanitize"),
);
self
}
fn with_endpoint_manager(self) -> Self {
self.sanitize()
}
}
impl From<&str> for ClientOptions {
fn from(api_key: &str) -> Self {
ClientOptionsBuilder::default()
.api_key(api_key.to_string())
.build()
.expect("We always set the API key, so this is infallible")
.with_endpoint_manager()
}
}
impl From<(&str, &str)> for ClientOptions {
fn from((api_key, host): (&str, &str)) -> Self {
ClientOptionsBuilder::default()
.api_key(api_key.to_string())
.host(host.to_string())
.build()
.expect("We always set the API key, so this is infallible")
.with_endpoint_manager()
}
}
#[cfg(test)]
mod tests {
use super::ClientOptionsBuilder;
use crate::endpoints::{EU_INGESTION_ENDPOINT, US_INGESTION_ENDPOINT};
#[test]
fn trims_whitespace_sensitive_options() {
let options = ClientOptionsBuilder::default()
.api_key(" \n test-api-key\t ".to_string())
.host(" \nhttps://eu.posthog.com/\t ")
.personal_api_key(" \n\t ")
.build()
.unwrap()
.sanitize();
assert_eq!(options.api_key, "test-api-key");
assert_eq!(options.host.as_deref(), Some("https://eu.posthog.com/"));
assert_eq!(options.personal_api_key, None);
assert_eq!(options.endpoints().api_host(), EU_INGESTION_ENDPOINT);
}
#[test]
fn defaults_blank_host_after_trimming_whitespace() {
let options = ClientOptionsBuilder::default()
.api_key("test-api-key".to_string())
.host(" \n\t ")
.build()
.unwrap()
.sanitize();
assert_eq!(options.host.as_deref(), Some(US_INGESTION_ENDPOINT));
assert_eq!(options.endpoints().api_host(), US_INGESTION_ENDPOINT);
}
}