Skip to content
Merged
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
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "posthog-rs"
license = "MIT"
version = "0.3.0"
version = "0.3.1"
authors = ["christos <[email protected]>"]
description = "An unofficial Rust client for Posthog (https://posthog.com/)."
repository = "https://github.com/openquery-io/posthog-rs"
Expand Down
5 changes: 1 addition & 4 deletions src/client/blocking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,5 @@ pub fn client<C: Into<ClientOptions>>(options: C) -> Client {
.timeout(Duration::from_secs(options.request_timeout_seconds))
.build()
.unwrap(); // Unwrap here is as safe as `HttpClient::new`
Client {
options: options.into(),
client,
}
Client { options, client }
}
7 changes: 5 additions & 2 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@ impl Display for Error {
match self {
Error::Connection(msg) => write!(f, "Connection Error: {}", msg),
Error::Serialization(msg) => write!(f, "Serialization Error: {}", msg),
Error::AlreadyInitialized => write!(f, "Client already initialized"),
Error::NotInitialized => write!(f, "Client not initialized"),
}
}
}

impl std::error::Error for Error {}

#[derive(Debug)]
#[non_exhaustive]
pub enum Error {
Connection(String),
Serialization(String),
AlreadyInitialized,
NotInitialized,
}
33 changes: 33 additions & 0 deletions src/global.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use std::sync::OnceLock;

use crate::{client, Client, ClientOptions, Error, Event};

pub static GLOBAL_CLIENT: OnceLock<Client> = OnceLock::new();

#[cfg(feature = "async-client")]
pub async fn init_global_client<C: Into<ClientOptions>>(options: C) -> Result<(), Error> {
let client = client(options).await;
GLOBAL_CLIENT
.set(client)
.map_err(|_| Error::AlreadyInitialized)
}

#[cfg(not(feature = "async-client"))]
pub fn init_global_client<C: Into<ClientOptions>>(options: C) -> Result<(), Error> {
let client = client(options);
GLOBAL_CLIENT
.set(client)
.map_err(|_| Error::AlreadyInitialized)
}

#[cfg(feature = "async-client")]
pub async fn capture(event: Event) -> Result<(), Error> {
let client = GLOBAL_CLIENT.get().ok_or(Error::NotInitialized)?;
client.capture(event).await
}

#[cfg(not(feature = "async-client"))]
pub fn capture(event: Event) -> Result<(), Error> {
let client = GLOBAL_CLIENT.get().ok_or(Error::NotInitialized)?;
client.capture(event)
}
5 changes: 5 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
mod client;
mod error;
mod event;
mod global;

const API_ENDPOINT: &str = "https://us.i.posthog.com/capture/";

Expand All @@ -15,3 +16,7 @@ pub use error::Error;

// Event
pub use event::Event;

// We expose a global capture function as a convenience, that uses a global client
pub use global::capture;
pub use global::init_global_client as init_global;