From f390f6bb9885da0a77307bf5878bf2f97769ead5 Mon Sep 17 00:00:00 2001 From: Nazar Antoniuk Date: Sat, 6 Sep 2025 09:10:25 +0300 Subject: [PATCH] introduce `Host` enum --- src/client/async_client.rs | 4 ++-- src/client/blocking.rs | 4 ++-- src/client/mod.rs | 41 +++++++++++++++++++++++++++++++++++--- src/lib.rs | 3 +-- 4 files changed, 43 insertions(+), 9 deletions(-) diff --git a/src/client/async_client.rs b/src/client/async_client.rs index 56139fb..2c2e3c5 100644 --- a/src/client/async_client.rs +++ b/src/client/async_client.rs @@ -31,7 +31,7 @@ impl Client { serde_json::to_string(&inner_event).map_err(|e| Error::Serialization(e.to_string()))?; self.client - .post(&self.options.api_endpoint) + .post(self.options.host.as_ref()) .header(CONTENT_TYPE, "application/json") .body(payload) .send() @@ -53,7 +53,7 @@ impl Client { serde_json::to_string(&events).map_err(|e| Error::Serialization(e.to_string()))?; self.client - .post(&self.options.api_endpoint) + .post(self.options.host.as_ref()) .header(CONTENT_TYPE, "application/json") .body(payload) .send() diff --git a/src/client/blocking.rs b/src/client/blocking.rs index 0f9af91..8afa28d 100644 --- a/src/client/blocking.rs +++ b/src/client/blocking.rs @@ -31,7 +31,7 @@ impl Client { serde_json::to_string(&inner_event).map_err(|e| Error::Serialization(e.to_string()))?; self.client - .post(&self.options.api_endpoint) + .post(self.options.host.as_ref()) .header(CONTENT_TYPE, "application/json") .body(payload) .send() @@ -52,7 +52,7 @@ impl Client { serde_json::to_string(&events).map_err(|e| Error::Serialization(e.to_string()))?; self.client - .post(&self.options.api_endpoint) + .post(self.options.host.as_ref()) .header(CONTENT_TYPE, "application/json") .body(payload) .send() diff --git a/src/client/mod.rs b/src/client/mod.rs index a60d772..cd77207 100644 --- a/src/client/mod.rs +++ b/src/client/mod.rs @@ -1,6 +1,41 @@ -use crate::API_ENDPOINT; use derive_builder::Builder; +/// PostHog host configuration for different regions and custom endpoints. +#[derive(Debug, Clone)] +pub enum Host { + /// US PostHog cloud (). + US, + /// EU PostHog cloud (). + EU, + /// Custom PostHog endpoint. + Custom(String), +} + +impl Host { + const US_ENDPOINT: &'static str = "https://us.i.posthog.com/i/v0/e/"; + const EU_ENDPOINT: &'static str = "https://eu.i.posthog.com/i/v0/e/"; +} + +impl AsRef for Host { + fn as_ref(&self) -> &str { + match self { + Host::US => Self::US_ENDPOINT, + Host::EU => Self::EU_ENDPOINT, + Host::Custom(url) => url, + } + } +} + +impl std::fmt::Display for Host { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Host::US => write!(f, "{}", Self::US_ENDPOINT), + Host::EU => write!(f, "{}", Self::EU_ENDPOINT), + Host::Custom(url) => write!(f, "{url}",), + } + } +} + #[cfg(not(feature = "async-client"))] mod blocking; #[cfg(not(feature = "async-client"))] @@ -17,8 +52,8 @@ pub use async_client::Client; #[derive(Builder)] pub struct ClientOptions { - #[builder(default = "API_ENDPOINT.to_string()")] - api_endpoint: String, + #[builder(default = "Host::US")] + host: Host, api_key: String, #[builder(default = "30")] diff --git a/src/lib.rs b/src/lib.rs index 6fe46d5..6250916 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,8 +3,6 @@ mod error; mod event; mod global; -const API_ENDPOINT: &str = "https://us.i.posthog.com/i/v0/e/"; - // Public interface - any change to this is breaking! // Client pub use client::client; @@ -12,6 +10,7 @@ pub use client::Client; pub use client::ClientOptions; pub use client::ClientOptionsBuilder; pub use client::ClientOptionsBuilderError; +pub use client::Host; // Error pub use error::Error;