Skip to content

Commit 60c4432

Browse files
authored
Merge pull request #19 from PostHog/olly_global_client
chore: add global functions
2 parents 54c4140 + 6ee2959 commit 60c4432

5 files changed

Lines changed: 45 additions & 7 deletions

File tree

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "posthog-rs"
33
license = "MIT"
4-
version = "0.3.0"
4+
version = "0.3.1"
55
authors = ["christos <christos@openquery.io>"]
66
description = "An unofficial Rust client for Posthog (https://posthog.com/)."
77
repository = "https://github.com/openquery-io/posthog-rs"

src/client/blocking.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,5 @@ pub fn client<C: Into<ClientOptions>>(options: C) -> Client {
5454
.timeout(Duration::from_secs(options.request_timeout_seconds))
5555
.build()
5656
.unwrap(); // Unwrap here is as safe as `HttpClient::new`
57-
Client {
58-
options: options.into(),
59-
client,
60-
}
57+
Client { options, client }
6158
}

src/error.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,17 @@ impl Display for Error {
55
match self {
66
Error::Connection(msg) => write!(f, "Connection Error: {}", msg),
77
Error::Serialization(msg) => write!(f, "Serialization Error: {}", msg),
8+
Error::AlreadyInitialized => write!(f, "Client already initialized"),
9+
Error::NotInitialized => write!(f, "Client not initialized"),
810
}
911
}
1012
}
1113

12-
impl std::error::Error for Error {}
13-
1414
#[derive(Debug)]
15+
#[non_exhaustive]
1516
pub enum Error {
1617
Connection(String),
1718
Serialization(String),
19+
AlreadyInitialized,
20+
NotInitialized,
1821
}

src/global.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
use std::sync::OnceLock;
2+
3+
use crate::{client, Client, ClientOptions, Error, Event};
4+
5+
pub static GLOBAL_CLIENT: OnceLock<Client> = OnceLock::new();
6+
7+
#[cfg(feature = "async-client")]
8+
pub async fn init_global_client<C: Into<ClientOptions>>(options: C) -> Result<(), Error> {
9+
let client = client(options).await;
10+
GLOBAL_CLIENT
11+
.set(client)
12+
.map_err(|_| Error::AlreadyInitialized)
13+
}
14+
15+
#[cfg(not(feature = "async-client"))]
16+
pub fn init_global_client<C: Into<ClientOptions>>(options: C) -> Result<(), Error> {
17+
let client = client(options);
18+
GLOBAL_CLIENT
19+
.set(client)
20+
.map_err(|_| Error::AlreadyInitialized)
21+
}
22+
23+
#[cfg(feature = "async-client")]
24+
pub async fn capture(event: Event) -> Result<(), Error> {
25+
let client = GLOBAL_CLIENT.get().ok_or(Error::NotInitialized)?;
26+
client.capture(event).await
27+
}
28+
29+
#[cfg(not(feature = "async-client"))]
30+
pub fn capture(event: Event) -> Result<(), Error> {
31+
let client = GLOBAL_CLIENT.get().ok_or(Error::NotInitialized)?;
32+
client.capture(event)
33+
}

src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
mod client;
22
mod error;
33
mod event;
4+
mod global;
45

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

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

1617
// Event
1718
pub use event::Event;
19+
20+
// We expose a global capture function as a convenience, that uses a global client
21+
pub use global::capture;
22+
pub use global::init_global_client as init_global;

0 commit comments

Comments
 (0)