Skip to main content

posthog_rs/client/
mod.rs

1use crate::endpoints::EndpointManager;
2use derive_builder::Builder;
3
4#[cfg(not(feature = "async-client"))]
5mod blocking;
6#[cfg(not(feature = "async-client"))]
7pub use blocking::client;
8#[cfg(not(feature = "async-client"))]
9pub use blocking::Client;
10
11#[cfg(feature = "async-client")]
12mod async_client;
13#[cfg(feature = "async-client")]
14pub use async_client::client;
15#[cfg(feature = "async-client")]
16pub use async_client::Client;
17
18/// Configuration options for the PostHog client.
19///
20/// Use [`ClientOptionsBuilder`] to construct options with custom settings,
21/// or create directly from an API key using `ClientOptions::from("your-api-key")`.
22///
23/// # Example
24///
25/// ```ignore
26/// use posthog_rs::ClientOptionsBuilder;
27///
28/// let options = ClientOptionsBuilder::default()
29///     .api_key("your-api-key".to_string())
30///     .host("https://eu.posthog.com")
31///     .build()
32///     .unwrap();
33/// ```
34#[derive(Builder, Clone)]
35pub struct ClientOptions {
36    /// Host URL for the PostHog API (defaults to US ingestion endpoint)
37    #[builder(setter(into, strip_option), default)]
38    host: Option<String>,
39
40    /// Project API key (required)
41    api_key: String,
42
43    /// Request timeout in seconds
44    #[builder(default = "30")]
45    request_timeout_seconds: u64,
46
47    /// Personal API key for fetching flag definitions (required for local evaluation)
48    #[builder(setter(into, strip_option), default)]
49    personal_api_key: Option<String>,
50
51    /// Enable local evaluation of feature flags
52    #[builder(default = "false")]
53    enable_local_evaluation: bool,
54
55    /// Interval for polling flag definitions (in seconds)
56    #[builder(default = "30")]
57    poll_interval_seconds: u64,
58
59    /// Disable tracking (useful for development)
60    #[builder(default = "false")]
61    disabled: bool,
62
63    /// Disable automatic geoip enrichment
64    #[builder(default = "false")]
65    disable_geoip: bool,
66
67    /// Feature flags request timeout in seconds
68    #[builder(default = "3")]
69    feature_flags_request_timeout_seconds: u64,
70
71    #[builder(setter(skip))]
72    #[builder(default = "EndpointManager::new(None)")]
73    endpoint_manager: EndpointManager,
74}
75
76impl ClientOptions {
77    /// Get the endpoint manager
78    pub(crate) fn endpoints(&self) -> &EndpointManager {
79        &self.endpoint_manager
80    }
81
82    /// Check if the client is disabled
83    pub fn is_disabled(&self) -> bool {
84        self.disabled
85    }
86
87    /// Create ClientOptions with properly initialized endpoint_manager
88    fn with_endpoint_manager(mut self) -> Self {
89        self.endpoint_manager = EndpointManager::new(self.host.clone());
90        self
91    }
92}
93
94impl From<&str> for ClientOptions {
95    fn from(api_key: &str) -> Self {
96        ClientOptionsBuilder::default()
97            .api_key(api_key.to_string())
98            .build()
99            .expect("We always set the API key, so this is infallible")
100            .with_endpoint_manager()
101    }
102}
103
104impl From<(&str, &str)> for ClientOptions {
105    /// Create options from API key and host
106    fn from((api_key, host): (&str, &str)) -> Self {
107        ClientOptionsBuilder::default()
108            .api_key(api_key.to_string())
109            .host(host.to_string())
110            .build()
111            .expect("We always set the API key, so this is infallible")
112            .with_endpoint_manager()
113    }
114}