Skip to content

Commit

Permalink
Merge pull request #12 from edgee-cloud/fix/hardcoded-endpoint
Browse files Browse the repository at this point in the history
Fix: Amplitude endpoint was hardcoded
  • Loading branch information
flyaruu authored Nov 21, 2024
2 parents 81b3bdf + 46dd20a commit 62a2cee
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
10 changes: 9 additions & 1 deletion src/amplitude_payload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ use std::collections::HashMap;
#[derive(Serialize, Debug, Default)]
pub(crate) struct AmplitudePayload {
api_key: String,
#[serde(skip)]
pub endpoint: String,
pub(crate) events: Vec<AmplitudeEvent>,
options: AmplitudeOptions,
}
Expand All @@ -23,8 +25,14 @@ impl AmplitudePayload {
}
.to_string();

let endpoint = cred
.get("endpoint")
.cloned()
.unwrap_or(crate::DEFAULT_ENDPOINT.to_owned());

Ok(Self {
api_key,
endpoint,
options: AmplitudeOptions {
min_id_length: Option::from(1),
},
Expand Down Expand Up @@ -298,7 +306,7 @@ pub fn parse_value(value: &str) -> serde_json::Value {
serde_json::Value::from(true)
} else if value == "false" {
serde_json::Value::from(false)
} else if let Some(_v) = value.parse::<f64>().ok() {
} else if let Ok(_v) = value.parse::<f64>() {
serde_json::Value::Number(value.parse().unwrap())
} else {
serde_json::Value::String(value.to_string())
Expand Down
8 changes: 5 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ use std::vec;
wit_bindgen::generate!({world: "data-collection"});
export!(AmplitudeComponent);

const DEFAULT_ENDPOINT: &str = "https://api2.amplitude.com/2/httpapi";

struct AmplitudeComponent;

impl Guest for AmplitudeComponent {
Expand Down Expand Up @@ -193,7 +195,7 @@ impl Guest for AmplitudeComponent {
properties.insert(key.clone(), parse_value(value));
}
}
if properties.len() > 0 {
if !properties.is_empty() {
event.event_properties = Some(serde_json::to_value(properties).unwrap());
}

Expand Down Expand Up @@ -254,7 +256,7 @@ impl Guest for AmplitudeComponent {
}
}

if properties.len() > 0 {
if !properties.is_empty() {
event.user_properties = Some(serde_json::to_value(properties).unwrap());
}

Expand All @@ -277,7 +279,7 @@ fn build_edgee_request(amplitude_payload: AmplitudePayload) -> EdgeeRequest {

EdgeeRequest {
method: exports::provider::HttpMethod::Post,
url: String::from("https://api2.amplitude.com/2/httpapi"),
url: amplitude_payload.endpoint.clone(),
headers,
body: serde_json::to_string(&amplitude_payload).unwrap(),
}
Expand Down

0 comments on commit 62a2cee

Please sign in to comment.