Skip to content

Commit 5347689

Browse files
committed
requested changes
1 parent 6a47525 commit 5347689

File tree

2 files changed

+52
-60
lines changed

2 files changed

+52
-60
lines changed

adview-manager/serve/src/routes.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use adex_primitives::{
44
sentry::{units_for_slot, IMPRESSION},
55
targeting::{input::Global, Input},
66
test_util::{DUMMY_CAMPAIGN, DUMMY_IPFS, DUMMY_VALIDATOR_FOLLOWER, DUMMY_VALIDATOR_LEADER},
7+
util::ApiUrl,
78
ToHex,
89
};
910
use adview_manager::{
@@ -56,8 +57,8 @@ pub async fn get_preview_ad(Extension(state): Extension<Arc<State>>) -> Html<Str
5657
disabled_video,
5758
disabled_sticky: false,
5859
validators: vec![
59-
DUMMY_VALIDATOR_LEADER.clone(),
60-
DUMMY_VALIDATOR_FOLLOWER.clone(),
60+
ApiUrl::parse(&DUMMY_VALIDATOR_LEADER.url).expect("should parse"),
61+
ApiUrl::parse(&DUMMY_VALIDATOR_FOLLOWER.url).expect("should parse"),
6162
],
6263
};
6364

@@ -90,15 +91,15 @@ pub async fn get_preview_ad(Extension(state): Extension<Arc<State>>) -> Html<Str
9091

9192
// Mock the `get_units_for_slot_resp` call
9293
let mock_call = Mock::given(method("GET"))
93-
// &depositAsset={}&depositAsset={}
94+
// &depositAssets[]={}&depositAssets[]={}
9495
.and(path(format!("units-for-slot/{}", options.market_slot)))
95-
// pubPrefix=HEX&depositAsset=0xASSET1&depositAsset=0xASSET2
96+
// pubPrefix=HEX&depositAssets[]=0xASSET1&depositAssets[]=0xASSET2
9697
.and(query_param("pubPrefix", pub_prefix))
9798
.and(query_param(
98-
"depositAsset",
99+
"depositAssets[]",
99100
"0x6B175474E89094C44Da98b954EedeAC495271d0F",
100101
))
101-
// .and(query_param("depositAsset[]", "0x6B175474E89094C44Da98b954EedeAC495271d03"))
102+
// .and(query_param("depositAssets[]", "0x6B175474E89094C44Da98b954EedeAC495271d03"))
102103
.respond_with(ResponseTemplate::new(200).set_body_json(units_for_slot_resp))
103104
.expect(1)
104105
.named("get_units_for_slot_resp");
@@ -164,8 +165,8 @@ pub async fn get_preview_video(Extension(state): Extension<Arc<State>>) -> Html<
164165
disabled_video,
165166
disabled_sticky: false,
166167
validators: vec![
167-
DUMMY_VALIDATOR_LEADER.clone(),
168-
DUMMY_VALIDATOR_FOLLOWER.clone(),
168+
ApiUrl::parse(&DUMMY_VALIDATOR_LEADER.url).expect("should parse"),
169+
ApiUrl::parse(&DUMMY_VALIDATOR_FOLLOWER.url).expect("should parse"),
169170
],
170171
};
171172

adview-manager/src/manager.rs

Lines changed: 43 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use adex_primitives::{
66
},
77
targeting::{self, input},
88
util::ApiUrl,
9-
validator::ValidatorDesc,
109
Address, BigNum, CampaignId, UnifiedNum, IPFS,
1110
};
1211
use async_std::{sync::RwLock, task::block_on};
@@ -56,6 +55,8 @@ pub enum Error {
5655
Request(#[from] reqwest::Error),
5756
#[error("No validators provided")]
5857
NoValidators,
58+
#[error("Invalid validator URL")]
59+
InvalidValidatorUrl,
5960
}
6061

6162
/// The Ad [`Manager`]'s options for showing ads.
@@ -84,7 +85,7 @@ pub struct Options {
8485
///
8586
/// default: `[]`
8687
#[serde(default)]
87-
pub validators: Vec<ValidatorDesc>,
88+
pub validators: Vec<ApiUrl>,
8889
}
8990

9091
/// [`AdSlot`](adex_primitives::AdSlot) size `width x height` in pixels (`px`)
@@ -251,7 +252,7 @@ impl Manager {
251252
// Test with different units with price
252253
// Test if first campaign is not overwritten
253254
pub async fn get_units_for_slot_resp(&self) -> Result<Response, Error> {
254-
let deposit_asset = self
255+
let deposit_assets = self
255256
.options
256257
.whitelisted_tokens
257258
.iter()
@@ -267,25 +268,32 @@ impl Manager {
267268
.next()
268269
.ok_or(Error::NoValidators)?;
269270

270-
let url = format!(
271-
"{}/v5/units-for-slot/{}?{}",
272-
first_validator.url, self.options.market_slot, deposit_asset
273-
);
274-
271+
let url = first_validator
272+
.join(&format!(
273+
"v5/units-for-slot/{}?{}",
274+
self.options.market_slot, deposit_assets
275+
))
276+
.map_err(|_| Error::InvalidValidatorUrl)?;
275277
// Ordering of the campaigns matters so we will just push them to the first result
276278
// We reuse `targeting_input_base`, `accepted_referrers` and `fallback_unit`
277-
let json_res: String = self.client.get(url).send().await?.text().await?;
278-
let mut first_res: Response = serde_json::from_str(&json_res).expect("Should convert");
279-
279+
let mut first_res: Response = self.client.get(url.as_str()).send().await?.json().await?;
280+
// let mut first_res: Response = serde_json::from_str(&json_res).expect("Should convert");
280281
for validator in self.options.validators.iter().skip(1) {
281-
let url = format!(
282-
"{}/v5/units-for-slot/{}?{}",
283-
validator.url, self.options.market_slot, deposit_asset
284-
);
285-
let new_res: Response = self.client.get(url).send().await?.json().await?;
286-
for campaign in new_res.campaigns {
287-
if !first_res.campaigns.contains(&campaign) {
288-
first_res.campaigns.push(campaign);
282+
let url = validator
283+
.join(&format!(
284+
"v5/units-for-slot/{}?{}",
285+
self.options.market_slot, deposit_assets
286+
))
287+
.map_err(|_| Error::InvalidValidatorUrl)?;
288+
let new_res: Response = self.client.get(url.as_str()).send().await?.json().await?;
289+
for response_campaign in new_res.campaigns {
290+
if !first_res
291+
.campaigns
292+
.iter()
293+
.map(|rc| rc.campaign.id)
294+
.any(|x| x == response_campaign.campaign.id)
295+
{
296+
first_res.campaigns.push(response_campaign);
289297
}
290298
}
291299
}
@@ -446,10 +454,7 @@ mod test {
446454
use crate::manager::input::Input;
447455
use adex_primitives::{
448456
sentry::CLICK,
449-
test_util::{
450-
CAMPAIGNS, DUMMY_AD_UNITS, DUMMY_IPFS, DUMMY_VALIDATOR_FOLLOWER,
451-
DUMMY_VALIDATOR_LEADER, IDS, LEADER_2, PUBLISHER,
452-
},
457+
test_util::{CAMPAIGNS, DUMMY_AD_UNITS, DUMMY_IPFS, PUBLISHER},
453458
};
454459
use wiremock::{
455460
matchers::{method, path},
@@ -517,34 +522,21 @@ mod test {
517522
let modified_referrers =
518523
vec![Url::parse("https://www.google.com/adsense/start/").expect("should parse")];
519524

520-
let unit_0 = DUMMY_AD_UNITS[0].clone();
521-
let original_ad_unit = AdUnit {
522-
ipfs: unit_0.ipfs,
523-
media_url: unit_0.media_url,
524-
media_mime: unit_0.media_mime,
525-
target_url: unit_0.target_url,
526-
};
527-
528-
let unit_1 = DUMMY_AD_UNITS[1].clone();
529-
let modified_ad_unit = AdUnit {
530-
ipfs: unit_1.ipfs,
531-
media_url: unit_1.media_url,
532-
media_mime: unit_1.media_mime,
533-
target_url: unit_1.target_url,
534-
};
525+
let original_ad_unit = AdUnit::from(&DUMMY_AD_UNITS[0]);
526+
let modified_ad_unit = AdUnit::from(&DUMMY_AD_UNITS[1]);
535527

536528
let campaign_0 = Campaign {
537-
campaign: CAMPAIGNS[0].clone().context,
529+
campaign: CAMPAIGNS[0].context.clone(),
538530
units_with_price: Vec::new(),
539531
};
540532

541533
let campaign_1 = Campaign {
542-
campaign: CAMPAIGNS[1].clone().context,
534+
campaign: CAMPAIGNS[1].context.clone(),
543535
units_with_price: Vec::new(),
544536
};
545537

546538
let campaign_2 = Campaign {
547-
campaign: CAMPAIGNS[2].clone().context,
539+
campaign: CAMPAIGNS[2].context.clone(),
548540
units_with_price: Vec::new(),
549541
};
550542

@@ -573,19 +565,19 @@ mod test {
573565
};
574566

575567
Mock::given(method("GET"))
576-
.and(path(format!("1/v5/units-for-slot/{}", slot,)))
568+
.and(path(format!("validator-1/v5/units-for-slot/{}", slot)))
577569
.respond_with(ResponseTemplate::new(200).set_body_json(&response_1))
578570
.mount(&server)
579571
.await;
580572

581573
Mock::given(method("GET"))
582-
.and(path(format!("2/v5/units-for-slot/{}", slot,)))
574+
.and(path(format!("validator-2/v5/units-for-slot/{}", slot)))
583575
.respond_with(ResponseTemplate::new(200).set_body_json(&response_2))
584576
.mount(&server)
585577
.await;
586578

587579
Mock::given(method("GET"))
588-
.and(path(format!("3/v5/units-for-slot/{}", slot,)))
580+
.and(path(format!("validator-3/v5/units-for-slot/{}", slot,)))
589581
.respond_with(ResponseTemplate::new(200).set_body_json(&response_3))
590582
.mount(&server)
591583
.await;
@@ -596,13 +588,12 @@ mod test {
596588
let publisher_addr = "0x0000000000000000626f62627973686d75726461"
597589
.parse()
598590
.unwrap();
599-
let mut validator_1 = DUMMY_VALIDATOR_LEADER.clone();
600-
validator_1.url = format!("{}/1", server.uri());
601-
let mut validator_2 = DUMMY_VALIDATOR_FOLLOWER.clone();
602-
validator_2.url = format!("{}/2", server.uri());
603-
let mut validator_3 = DUMMY_VALIDATOR_LEADER.clone();
604-
validator_3.id = IDS[&LEADER_2];
605-
validator_3.url = format!("{}/3", server.uri());
591+
let validator_1_url =
592+
ApiUrl::parse(&format!("{}/validator-1", server.uri())).expect("should parse");
593+
let validator_2_url =
594+
ApiUrl::parse(&format!("{}/validator-2", server.uri())).expect("should parse");
595+
let validator_3_url =
596+
ApiUrl::parse(&format!("{}/validator-3", server.uri())).expect("should parse");
606597
let options = Options {
607598
market_url,
608599
market_slot: DUMMY_IPFS[0],
@@ -613,7 +604,7 @@ mod test {
613604
navigator_language: Some("bg".into()),
614605
disabled_video: false,
615606
disabled_sticky: false,
616-
validators: vec![validator_1, validator_2, validator_3],
607+
validators: vec![validator_1_url, validator_2_url, validator_3_url],
617608
};
618609

619610
let manager = Manager::new(options.clone(), Default::default())

0 commit comments

Comments
 (0)