-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlib.rs
222 lines (203 loc) · 7.78 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
use crate::exports::edgee::components::data_collection::{Dict, EdgeeRequest, Event, HttpMethod};
use exports::edgee::components::data_collection::Guest;
use std::collections::HashMap;
wit_bindgen::generate!({world: "data-collection", path: ".edgee/wit", generate_all});
export!(Component);
struct Component;
/*
* Implement the Guest trait for the Component struct
* to create the required functions for the data collection protocol
* for your provider.
* The functions are page, track, and user.
* The page function is called when the page event is triggered.
* The track function is called when the track event is triggered.
* The user function is called when the user event is triggered.
* The functions should return an EdgeeRequest or an error message.
* The EdgeeRequest contains the method, url, headers, and body of the request.
*/
impl Guest for Component {
#[allow(unused_variables)]
fn page(edgee_event: Event, settings_dict: Dict) -> Result<EdgeeRequest, String> {
let settings = Settings::new(settings_dict).map_err(|e| e.to_string())?;
Ok(EdgeeRequest {
method: HttpMethod::Post,
url: format!("https://example.com/{}", "page"),
headers: vec![
("Content-Type".to_string(), "application/json".to_string()),
("Authorization".to_string(), "Bearer XYZ".to_string()),
],
body: settings.example,
forward_client_headers: true,
})
}
#[allow(unused_variables)]
fn track(edgee_event: Event, settings_dict: Dict) -> Result<EdgeeRequest, String> {
let settings = Settings::new(settings_dict).map_err(|e| e.to_string())?;
Ok(EdgeeRequest {
method: HttpMethod::Post,
url: format!("https://example.com/{}", "track"),
headers: vec![
("Content-Type".to_string(), "application/json".to_string()),
("Authorization".to_string(), "Bearer XYZ".to_string()),
],
body: settings.example,
forward_client_headers: true,
})
}
#[allow(unused_variables)]
fn user(edgee_event: Event, settings_dict: Dict) -> Result<EdgeeRequest, String> {
let settings = Settings::new(settings_dict).map_err(|e| e.to_string())?;
Ok(EdgeeRequest {
method: HttpMethod::Post,
url: format!("https://example.com/{}", "user"),
headers: vec![
("Content-Type".to_string(), "application/json".to_string()),
("Authorization".to_string(), "Bearer XYZ".to_string()),
],
body: settings.example,
forward_client_headers: true,
})
}
}
pub struct Settings {
pub example: String,
}
impl Settings {
pub fn new(settings_dict: Dict) -> anyhow::Result<Self> {
let settings_map: HashMap<String, String> = settings_dict
.iter()
.map(|(key, value)| (key.to_string(), value.to_string()))
.collect();
/*
// required setting
// also needs -> use anyhow::Context;
let example = settings_map
.get("example")
.context("Missing example setting")?
.to_string();
*/
// optional setting
let example = settings_map
.get("example")
.map(String::to_string)
.unwrap_or_default();
Ok(Self { example })
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::exports::edgee::components::data_collection::{
Campaign, Client, Context, Data, EventType, PageData, Session, UserData,
};
use exports::edgee::components::data_collection::Consent;
use pretty_assertions::assert_eq;
use uuid::Uuid;
fn sample_user_data(edgee_id: String) -> UserData {
UserData {
user_id: "123".to_string(),
anonymous_id: "456".to_string(),
edgee_id,
properties: vec![
("prop1".to_string(), "value1".to_string()),
("prop2".to_string(), "10".to_string()),
],
}
}
fn sample_context(edgee_id: String, locale: String, session_start: bool) -> Context {
Context {
page: sample_page_data(),
user: sample_user_data(edgee_id),
client: Client {
city: "Paris".to_string(),
ip: "192.168.0.1".to_string(),
locale,
timezone: "CET".to_string(),
user_agent: "Chrome".to_string(),
user_agent_architecture: "fuck knows".to_string(),
user_agent_bitness: "64".to_string(),
user_agent_full_version_list: "abc".to_string(),
user_agent_version_list: "abc".to_string(),
user_agent_mobile: "mobile".to_string(),
user_agent_model: "don't know".to_string(),
os_name: "MacOS".to_string(),
os_version: "latest".to_string(),
screen_width: 1024,
screen_height: 768,
screen_density: 2.0,
continent: "Europe".to_string(),
country_code: "FR".to_string(),
country_name: "France".to_string(),
region: "West Europe".to_string(),
},
campaign: Campaign {
name: "random".to_string(),
source: "random".to_string(),
medium: "random".to_string(),
term: "random".to_string(),
content: "random".to_string(),
creative_format: "random".to_string(),
marketing_tactic: "random".to_string(),
},
session: Session {
session_id: "random".to_string(),
previous_session_id: "random".to_string(),
session_count: 2,
session_start,
first_seen: 123,
last_seen: 123,
},
}
}
fn sample_page_data() -> PageData {
PageData {
name: "page name".to_string(),
category: "category".to_string(),
keywords: vec!["value1".to_string(), "value2".into()],
title: "page title".to_string(),
url: "https://example.com/full-url?test=1".to_string(),
path: "/full-path".to_string(),
search: "?test=1".to_string(),
referrer: "https://example.com/another-page".to_string(),
properties: vec![
("prop1".to_string(), "value1".to_string()),
("prop2".to_string(), "10".to_string()),
("currency".to_string(), "USD".to_string()),
],
}
}
fn sample_page_event(
consent: Option<Consent>,
edgee_id: String,
locale: String,
session_start: bool,
) -> Event {
Event {
uuid: Uuid::new_v4().to_string(),
timestamp: 123,
timestamp_millis: 123,
timestamp_micros: 123,
event_type: EventType::Page,
data: Data::Page(sample_page_data()),
context: sample_context(edgee_id, locale, session_start),
consent,
}
}
#[test]
fn page_works_fine() {
let event = sample_page_event(
Some(Consent::Granted),
"abc".to_string(),
"fr".to_string(),
true,
);
let settings = vec![("your-credentials".to_string(), "abc".to_string())];
let result = Component::page(event, settings);
assert_eq!(result.is_err(), false);
let edgee_request = result.unwrap();
assert_eq!(edgee_request.method, HttpMethod::Post);
assert_eq!(edgee_request.body.is_empty(), true);
assert_eq!(edgee_request.url.starts_with("https://example.com/"), true);
// add more checks (headers, querystring, etc.)
}
}