Skip to content

Commit

Permalink
Merge pull request #1 from christoshadjiaslanis/feat/generic-properties
Browse files Browse the repository at this point in the history
feat: added generic properties and bumped version to 0.2.0
  • Loading branch information
fuziontech authored Jun 25, 2022
2 parents ab6058f + 1592ed9 commit 9253626
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 28 deletions.
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.1.3"
version = "0.2.2"
authors = ["christos <[email protected]>"]
description = "An unofficial Rust client for Posthog (https://posthog.com/)."
repository = "https://github.com/openquery-io/posthog-rs"
Expand Down
18 changes: 6 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,17 @@ Add `posthog-rs` to your `Cargo.toml`.

```toml
[dependencies]
posthog_rs = "0.1.0"
posthog_rs = "0.2.0"
```

```rust
let client = posthog_rs::client(env!("POSTHOG_API_KEY"));
let client = crate::client(env!("POSTHOG_API_KEY"));

let mut props = HashMap::new();
props.insert("key1".to_string(), "value1".to_string());
props.insert("key2".to_string(), "value2".to_string());
let mut event = Event::new("test", "1234");
event.insert_prop("key1", "value1").unwrap();
event.insert_prop("key2", vec!["a", "b"]).unwrap();

let event = Event {
event: "test".to_string(),
properties: Properties { distinct_id: "1234".to_string(), props },
timestamp: Some(Utc::now().naive_utc()),
};

let res = client.capture(event).unwrap();
client.capture(event).unwrap();

```

72 changes: 57 additions & 15 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::collections::HashMap;
use std::fmt::{Display, Formatter};
use chrono::{NaiveDateTime};
use reqwest::blocking::Client as HttpClient;
use reqwest::header::CONTENT_TYPE;
Expand All @@ -18,9 +19,23 @@ pub fn client<C: Into<ClientOptions>>(options: C) -> Client {
}
}

impl Display for Error {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
Error::Connection(msg) => write!(f, "Connection Error: {}", msg),
Error::Serialization(msg) => write!(f, "Serialization Error: {}", msg)
}
}
}

impl std::error::Error for Error {

}

#[derive(Debug)]
pub enum Error {
Connection(String)
Connection(String),
Serialization(String)
}

pub struct ClientOptions {
Expand Down Expand Up @@ -82,16 +97,43 @@ impl InnerEvent {
}


#[derive(Serialize, Debug, PartialEq, Eq)]
pub struct Event {
pub event: String,
pub properties: Properties,
pub timestamp: Option<NaiveDateTime>,
event: String,
properties: Properties,
timestamp: Option<NaiveDateTime>,
}

#[derive(Serialize)]
#[derive(Serialize, Debug, PartialEq, Eq)]
pub struct Properties {
pub distinct_id: String,
pub props: HashMap<String, String>,
distinct_id: String,
props: HashMap<String, serde_json::Value>,
}

impl Properties {
fn new<S: Into<String>>(distinct_id: S) -> Self {
Self {
distinct_id: distinct_id.into(),
props: Default::default()
}
}
}

impl Event {
pub fn new<S: Into<String>>(event: S, distinct_id: S) -> Self {
Self {
event: event.into(),
properties: Properties::new(distinct_id),
timestamp: None
}
}

/// Errors if `prop` fails to serialize
pub fn insert_prop<K: Into<String>, P: Serialize>(&mut self, key: K, prop: P) -> Result<(), Error> {
let as_json = serde_json::to_value(prop).map_err(|e| Error::Serialization(e.to_string()))?;
let _ = self.properties.props.insert(key.into(), as_json);
Ok(())
}
}


Expand All @@ -104,15 +146,15 @@ pub mod tests {
fn get_client() {
let client = crate::client(env!("POSTHOG_API_KEY"));

let mut props = HashMap::new();
props.insert("key1".to_string(), "value1".to_string());
props.insert("key2".to_string(), "value2".to_string());
let mut child_map = HashMap::new();
child_map.insert("child_key1", "child_value1");


let mut event = Event::new("test", "1234");
event.insert_prop("key1", "value1").unwrap();
event.insert_prop("key2", vec!["a", "b"]).unwrap();
event.insert_prop("key3", child_map).unwrap();

let event = Event {
event: "test".to_string(),
properties: Properties { distinct_id: "1234".to_string(), props },
timestamp: Some(Utc::now().naive_utc()),
};
client.capture(event).unwrap();
}
}

0 comments on commit 9253626

Please sign in to comment.