Skip to content

Commit bad29e1

Browse files
committed
more code examples + fixes
1 parent ea5b20f commit bad29e1

File tree

7 files changed

+223
-88
lines changed

7 files changed

+223
-88
lines changed

Cargo.lock

Lines changed: 44 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

primitives/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ required-features = ["test-util"]
4040
# (De)Serialization
4141
serde = { version = "1.0", features = ["derive"] }
4242
serde_json = "1.0"
43+
# TODO: Remove once we change `ChannelId` Serialize impl
44+
serde-hex = "0.1"
4345
serde_millis = "0.1"
4446
# Used prefixes on field for targeting::Input, and `campaign::Active`
4547
serde_with = "1"
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
use primitives::{
2+
analytics::{AnalyticsQuery, Metric, Timeframe, query::{AllowedKey, Time}, OperatingSystem},
3+
sentry::{EventType, DateHour},
4+
Address, CampaignId, ChainId, IPFS
5+
};
6+
use std::str::FromStr;
7+
8+
fn main() {
9+
// Empty query - default values only
10+
{
11+
let empty_query = "";
12+
let query: AnalyticsQuery = serde_qs::from_str(empty_query).unwrap();
13+
14+
assert_eq!(100, query.limit);
15+
assert_eq!(EventType::Impression, query.event_type);
16+
assert!(matches!(query.metric, Metric::Count));
17+
assert!(matches!(query.time.timeframe, Timeframe::Day));
18+
}
19+
// Query with different metric/chain/eventType
20+
{
21+
let query_str = "limit=200&eventType=CLICK&metric=paid&timeframe=month";
22+
let query: AnalyticsQuery = serde_qs::from_str(query_str).unwrap();
23+
24+
assert_eq!(200, query.limit);
25+
assert_eq!(EventType::Click, query.event_type);
26+
assert!(matches!(query.metric, Metric::Paid));
27+
assert!(matches!(query.time.timeframe, Timeframe::Month));
28+
}
29+
30+
// Query with allowed keys for guest - country, slotType
31+
{
32+
let query_str = "country=Bulgaria&adSlotType=legacy_300x100";
33+
let query: AnalyticsQuery = serde_qs::from_str(query_str).unwrap();
34+
35+
assert_eq!(Some("Bulgaria".to_string()), query.country);
36+
assert_eq!(Some("legacy_300x100".to_string()), query.ad_slot_type);
37+
}
38+
39+
// Query with all possible fields (publisher/advertiser/admin)
40+
{
41+
let query_str = r#"limit=200
42+
&eventType=CLICK
43+
&metric=paid
44+
&segmentBy=country
45+
&timeframe=week
46+
&start=420
47+
&campaignId=0x936da01f9abd4d9d80c702af85c822a8
48+
&adUnit=Qmasg8FrbuSQpjFu3kRnZF9beg8rEBFrqgi1uXDRwCbX5f
49+
&adSlot=QmcUVX7fvoLMM93uN2bD3wGTH8MXSxeL8hojYfL2Lhp7mR
50+
&adSlotType=legacy_300x100
51+
&avertiser=0xDd589B43793934EF6Ad266067A0d1D4896b0dff0
52+
&publisher=0xE882ebF439207a70dDcCb39E13CA8506c9F45fD9
53+
&hostname=localhost
54+
&country=Bulgaria
55+
&osName=Windows
56+
&chains[0]=1&chains[1]=1337
57+
"#;
58+
let query: AnalyticsQuery = serde_qs::from_str(query_str).unwrap();
59+
60+
assert_eq!(query.limit, 200);
61+
assert_eq!(query.event_type, EventType::Click);
62+
assert!(matches!(query.metric, Metric::Paid));
63+
assert_eq!(query.segment_by, Some(AllowedKey::Country));
64+
assert_eq!(query.time, Time {
65+
timeframe: Timeframe::Week,
66+
start: DateHour::from_ymdh(2021, 12, 31, 22),
67+
end: None,
68+
});
69+
assert_eq!(query.campaign_id, Some(CampaignId::from_str("0x936da01f9abd4d9d80c702af85c822a8").expect("should be valid")));
70+
assert_eq!(query.ad_unit, Some(IPFS::from_str("Qmasg8FrbuSQpjFu3kRnZF9beg8rEBFrqgi1uXDRwCbX5f").expect("should be valid")));
71+
assert_eq!(query.ad_slot, Some(IPFS::from_str("QmcUVX7fvoLMM93uN2bD3wGTH8MXSxeL8hojYfL2Lhp7mR").expect("should be valid")));
72+
assert_eq!(query.ad_slot_type, Some("legacy_300x100".to_string()));
73+
assert_eq!(query.advertiser, Some(Address::from_str("0xDd589B43793934EF6Ad266067A0d1D4896b0dff0").expect("should be valid")));
74+
assert_eq!(query.publisher, Some(Address::from_str("0xE882ebF439207a70dDcCb39E13CA8506c9F45fD9").expect("should be valid")));
75+
assert_eq!(query.hostname, Some("localhost".to_string()));
76+
assert_eq!(query.country, Some("Bulgaria".to_string()));
77+
assert_eq!(query.os_name, Some(OperatingSystem::Whitelisted("Windows".to_string())));
78+
assert_eq!(query.chains, vec!(ChainId::new(1), ChainId::new(1337)));
79+
}
80+
}

primitives/examples/create_campaign.rs

Lines changed: 54 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -7,80 +7,82 @@ fn main() {
77
{
88
let create_campaign = CreateCampaign::from_campaign_erased(DUMMY_CAMPAIGN.clone(), None);
99

10-
let create_campaign_as_json_str = json!("{
11-
\"id\":null,
12-
\"channel\":{
13-
\"leader\":\"0x80690751969B234697e9059e04ed72195c3507fa\",
14-
\"follower\":\"0xf3f583AEC5f7C030722Fe992A5688557e1B86ef7\",
15-
\"guardian\":\"0xe061E1EB461EaBE512759aa18A201B20Fe90631D\",
16-
\"token\":\"0x2BCaf6968aEC8A3b5126FBfAb5Fd419da6E8AD8E\",
17-
\"nonce\":\"987654321\"
10+
let create_campaign_json = json!({
11+
"id":null,
12+
"channel":{
13+
"leader":"0x80690751969B234697e9059e04ed72195c3507fa",
14+
"follower":"0xf3f583AEC5f7C030722Fe992A5688557e1B86ef7",
15+
"guardian":"0xe061E1EB461EaBE512759aa18A201B20Fe90631D",
16+
"token":"0x2BCaf6968aEC8A3b5126FBfAb5Fd419da6E8AD8E",
17+
"nonce":"987654321"
1818
},
19-
\"creator\":\"0xaCBaDA2d5830d1875ae3D2de207A1363B316Df2F\",
20-
\"budget\":\"100000000000\",
21-
\"validators\":[
19+
"creator":"0xaCBaDA2d5830d1875ae3D2de207A1363B316Df2F",
20+
"budget":"100000000000",
21+
"validators":[
2222
{
23-
\"id\":\"0x80690751969B234697e9059e04ed72195c3507fa\",
24-
\"fee\":\"2000000\",
25-
\"url\":\"http://localhost:8005\"
23+
"id":"0x80690751969B234697e9059e04ed72195c3507fa",
24+
"fee":"2000000",
25+
"url":"http://localhost:8005"
2626
},
2727
{
28-
\"id\":\"0xf3f583AEC5f7C030722Fe992A5688557e1B86ef7\",
29-
\"fee\":\"3000000\",
30-
\"url\":\"http://localhost:8006\"
28+
"id":"0xf3f583AEC5f7C030722Fe992A5688557e1B86ef7",
29+
"fee":"3000000",
30+
"url":"http://localhost:8006"
3131
}
3232
],
33-
\"title\":\"Dummy Campaign\",
34-
\"pricingBounds\":{\"CLICK\":{\"min\":\"0\",\"max\":\"0\"},\"IMPRESSION\":{\"min\":\"1\",\"max\":\"10\"}},
35-
\"eventSubmission\":{\"allow\":[]},
36-
\"targetingRules\":[],
37-
\"created\":1612162800000,
38-
\"active_to\":4073414400000
39-
}");
33+
"title":"Dummy Campaign",
34+
"pricingBounds":{"CLICK":{"min":"0","max":"0"},"IMPRESSION":{"min":"1","max":"10"}},
35+
"eventSubmission":{"allow":[]},
36+
"targetingRules":[],
37+
"created":1612162800000_u64,
38+
"activeTo":4073414400000_u64
39+
});
4040

41-
let create_campaign_from_json = serde_json::from_str(create_campaign_as_json_str).expect("should deserialize");
41+
let create_campaign_json = serde_json::to_string(&create_campaign_json).expect("should serialize");
42+
let deserialized: CreateCampaign = serde_json::from_str(&create_campaign_json).expect("should deserialize");
4243

43-
assert_eq!(create_campaign, create_campaign_from_json);
44+
assert_eq!(create_campaign, deserialized);
4445
}
4546

4647
// CreateCampaign with a provided ID
4748
{
4849
let mut create_campaign = CreateCampaign::from_campaign_erased(DUMMY_CAMPAIGN.clone(), None);
4950
create_campaign.id = Some(CampaignId::from_str("0x936da01f9abd4d9d80c702af85c822a8").expect("Should be valid id"));
5051

51-
let create_campaign_as_json_str = "{
52-
\"id\":\"0x936da01f9abd4d9d80c702af85c822a8\",
53-
\"channel\":{
54-
\"leader\":\"0x80690751969B234697e9059e04ed72195c3507fa\",
55-
\"follower\":\"0xf3f583AEC5f7C030722Fe992A5688557e1B86ef7\",
56-
\"guardian\":\"0xe061E1EB461EaBE512759aa18A201B20Fe90631D\",
57-
\"token\":\"0x2BCaf6968aEC8A3b5126FBfAb5Fd419da6E8AD8E\",
58-
\"nonce\":\"987654321\"
52+
let create_campaign_json = json!({
53+
"id":"0x936da01f9abd4d9d80c702af85c822a8",
54+
"channel":{
55+
"leader":"0x80690751969B234697e9059e04ed72195c3507fa",
56+
"follower":"0xf3f583AEC5f7C030722Fe992A5688557e1B86ef7",
57+
"guardian":"0xe061E1EB461EaBE512759aa18A201B20Fe90631D",
58+
"token":"0x2BCaf6968aEC8A3b5126FBfAb5Fd419da6E8AD8E",
59+
"nonce":"987654321"
5960
},
60-
\"creator\":\"0xaCBaDA2d5830d1875ae3D2de207A1363B316Df2F\",
61-
\"budget\":\"100000000000\",
62-
\"validators\":[
61+
"creator":"0xaCBaDA2d5830d1875ae3D2de207A1363B316Df2F",
62+
"budget":"100000000000",
63+
"validators":[
6364
{
64-
\"id\":\"0x80690751969B234697e9059e04ed72195c3507fa\",
65-
\"fee\":\"2000000\",
66-
\"url\":\"http://localhost:8005\"
65+
"id":"0x80690751969B234697e9059e04ed72195c3507fa",
66+
"fee":"2000000",
67+
"url":"http://localhost:8005"
6768
},
6869
{
69-
\"id\":\"0xf3f583AEC5f7C030722Fe992A5688557e1B86ef7\",
70-
\"fee\":\"3000000\",
71-
\"url\":\"http://localhost:8006\"
70+
"id":"0xf3f583AEC5f7C030722Fe992A5688557e1B86ef7",
71+
"fee":"3000000",
72+
"url":"http://localhost:8006"
7273
}
7374
],
74-
\"title\":\"Dummy Campaign\",
75-
\"pricingBounds\":{\"CLICK\":{\"min\":\"0\",\"max\":\"0\"},\"IMPRESSION\":{\"min\":\"1\",\"max\":\"10\"}},
76-
\"eventSubmission\":{\"allow\":[]},
77-
\"targetingRules\":[],
78-
\"created\":1612162800000,
79-
\"active_to\":4073414400000
80-
}";
75+
"title":"Dummy Campaign",
76+
"pricingBounds":{"CLICK":{"min":"0","max":"0"},"IMPRESSION":{"min":"1","max":"10"}},
77+
"eventSubmission":{"allow":[]},
78+
"targetingRules":[],
79+
"created":1612162800000_u64,
80+
"activeTo":4073414400000_u64
81+
});
8182

82-
let create_campaign_from_json = serde_json::from_str(create_campaign_as_json_str).expect("should deserialize");
83+
let create_campaign_json = serde_json::to_string(&create_campaign_json).expect("should serialize");
84+
let deserialized: CreateCampaign = serde_json::from_str(&create_campaign_json).expect("should deserialize");
8385

84-
assert_eq!(create_campaign, create_campaign_from_json);
86+
assert_eq!(create_campaign, deserialized);
8587
}
8688
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
use primitives::{sentry::campaign_modify::ModifyCampaign, unified_num::FromWhole, UnifiedNum};
2+
use std::str::FromStr;
3+
use serde_json::json;
4+
5+
fn main() {
6+
{
7+
let modify_campaign = ModifyCampaign {
8+
ad_units: None,
9+
budget: Some(UnifiedNum::from_whole(100)),
10+
validators: None,
11+
title: None,
12+
pricing_bounds: None,
13+
event_submission: None,
14+
targeting_rules: None,
15+
};
16+
17+
let modify_campaign_json = json!({
18+
"ad_units": null,
19+
"budget": "10000000000",
20+
"validators": null,
21+
"title": null,
22+
"pricing_bounds": null,
23+
"event_submission": null,
24+
"targeting_rules": null,
25+
});
26+
27+
let modify_campaign_json = serde_json::to_string(&modify_campaign_json).expect("should serialize");
28+
let deserialized: ModifyCampaign = serde_json::from_str(&modify_campaign_json).expect("should deserialize");
29+
30+
assert_eq!(modify_campaign, deserialized);
31+
}
32+
}

sentry/src/routes/analytics.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,16 @@ use primitives::analytics::{
1515
AnalyticsQuery, AuthenticateAs,
1616
};
1717

18-
/// `GET /v5/analytics` request
18+
/// GET `/v5/analytics` routes
1919
/// with query parameters: [`primitives::analytics::AnalyticsQuery`].
2020
///
21-
/// # Tutorial:
22-
#[doc = include_str!("../../docs/create_campaign/tutorial.md")]
21+
/// Returns `Vec<[FetchedAnalytics](primitives::sentry::FethedAnalytics)>`
22+
/// Analytics routes:
23+
/// - GET `/v5/analytics`
24+
/// - GET `/v5/analytics/for-publisher`
25+
/// - GET `/v5/analytics/for-advertiser`
26+
/// - GET `/v5/analytics/for-admin`
2327
///
24-
/// # Examples:
25-
/// ```
26-
#[doc = include_str!("../../docs/create_campaign/examples.rs")]
27-
/// ```
28-
///
29-
/// # Glossary:
30-
#[doc = include_str!("../../docs/create_campaign/glossary.md")]
3128
pub async fn analytics<C: Locked + 'static>(
3229
req: Request<Body>,
3330
app: &Application<C>,

0 commit comments

Comments
 (0)