Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/client/async_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions src/client/blocking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand Down
41 changes: 38 additions & 3 deletions src/client/mod.rs
Original file line number Diff line number Diff line change
@@ -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 (<https://us.i.posthog.com/i/v0/e/>).
US,
/// EU PostHog cloud (<https://eu.i.posthog.com/i/v0/e/>).
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<str> 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"))]
Expand All @@ -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")]
Expand Down
3 changes: 1 addition & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@ 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;
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;
Expand Down