Skip to content

Commit 0dbe10a

Browse files
committed
we now query GET /units-for-slot in the adview manager
1 parent 70a6825 commit 0dbe10a

File tree

2 files changed

+49
-24
lines changed

2 files changed

+49
-24
lines changed

adview-manager/serve/src/routes.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::sync::Arc;
33
use adex_primitives::{
44
sentry::{units_for_slot, IMPRESSION},
55
targeting::{input::Global, Input},
6-
test_util::{DUMMY_CAMPAIGN, DUMMY_IPFS},
6+
test_util::{DUMMY_CAMPAIGN, DUMMY_IPFS, DUMMY_VALIDATOR_FOLLOWER, DUMMY_VALIDATOR_LEADER},
77
ToHex,
88
};
99
use adview_manager::{
@@ -55,6 +55,10 @@ pub async fn get_preview_ad(Extension(state): Extension<Arc<State>>) -> Html<Str
5555
/// Defaulted
5656
disabled_video,
5757
disabled_sticky: false,
58+
validators: vec![
59+
DUMMY_VALIDATOR_LEADER.clone(),
60+
DUMMY_VALIDATOR_FOLLOWER.clone(),
61+
],
5862
};
5963

6064
let manager =
@@ -159,6 +163,10 @@ pub async fn get_preview_video(Extension(state): Extension<Arc<State>>) -> Html<
159163
/// Defaulted
160164
disabled_video,
161165
disabled_sticky: false,
166+
validators: vec![
167+
DUMMY_VALIDATOR_LEADER.clone(),
168+
DUMMY_VALIDATOR_FOLLOWER.clone(),
169+
],
162170
};
163171

164172
// legacy_728x90

adview-manager/src/manager.rs

Lines changed: 40 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ use adex_primitives::{
66
},
77
targeting::{self, input},
88
util::ApiUrl,
9-
Address, BigNum, CampaignId, ToHex, UnifiedNum, IPFS,
9+
validator::ValidatorDesc,
10+
Address, BigNum, CampaignId, UnifiedNum, IPFS,
1011
};
1112
use async_std::{sync::RwLock, task::block_on};
1213
use chrono::{DateTime, Duration, Utc};
@@ -49,10 +50,12 @@ pub static DEFAULT_TOKENS: Lazy<HashSet<Address>> = Lazy::new(|| {
4950

5051
#[derive(Debug, Error)]
5152
pub enum Error {
52-
#[error("Request to the Market failed: status {status} at url {url}")]
53-
Market { status: StatusCode, url: Url },
53+
#[error("Request to the Sentry failed: status {status} at url {url}")]
54+
Sentry { status: StatusCode, url: Url },
5455
#[error(transparent)]
5556
Request(#[from] reqwest::Error),
57+
#[error("No validators provided")]
58+
NoValidators,
5659
}
5760

5861
/// The Ad [`Manager`]'s options for showing ads.
@@ -77,6 +80,11 @@ pub struct Options {
7780
/// default: `false`
7881
#[serde(default)]
7982
pub disabled_sticky: bool,
83+
/// List of validators to query /units-for-slot from
84+
///
85+
/// default: `[]`
86+
#[serde(default)]
87+
pub validators: Vec<ValidatorDesc>,
8088
}
8189

8290
/// [`AdSlot`](adex_primitives::AdSlot) size `width x height` in pixels (`px`)
@@ -240,36 +248,45 @@ impl Manager {
240248
}
241249
}
242250

251+
// Test with different units with price
252+
// Test if first campaign is not overwritten
243253
pub async fn get_market_demand_resp(&self) -> Result<Response, Error> {
244-
let pub_prefix = self.options.publisher_addr.to_hex();
245-
246254
let deposit_asset = self
247255
.options
248256
.whitelisted_tokens
249257
.iter()
250-
.map(|token| format!("depositAsset={}", token))
258+
.map(|token| format!("depositAssets[]={}", token))
251259
.collect::<Vec<_>>()
252260
.join("&");
253261

254-
// ApiUrl handles endpoint path (with or without `/`)
255-
let url = self
262+
let first_validator = self
256263
.options
257-
.market_url
258-
.join(&format!(
259-
"units-for-slot/{ad_slot}?pubPrefix={pub_prefix}&{deposit_asset}",
260-
ad_slot = self.options.market_slot
261-
))
262-
.expect("Valid URL endpoint!");
263-
264-
let market_response = self.client.get(url.clone()).send().await?;
265-
266-
match market_response.status() {
267-
StatusCode::OK => Ok(market_response.json().await?),
268-
_ => Err(Error::Market {
269-
status: market_response.status(),
270-
url,
271-
}),
264+
.validators
265+
.clone()
266+
.into_iter()
267+
.next()
268+
.ok_or(Error::NoValidators)?;
269+
270+
let url = format!(
271+
"{}/units-for-slot/{}?{}",
272+
first_validator.url, self.options.market_slot, deposit_asset
273+
);
274+
let mut first_res: Response = self.client.get(url).send().await?.json().await?;
275+
276+
for validator in self.options.validators.iter().skip(1) {
277+
let url = format!(
278+
"{}/units-for-slot/{}?{}",
279+
validator.url, self.options.market_slot, deposit_asset
280+
);
281+
let new_res: Response = self.client.get(url).send().await?.json().await?;
282+
for campaign in new_res.campaigns {
283+
if !first_res.campaigns.contains(&campaign) {
284+
first_res.campaigns.push(campaign);
285+
}
286+
}
272287
}
288+
289+
Ok(first_res)
273290
}
274291

275292
pub async fn get_next_ad_unit(&self) -> Result<Option<NextAdUnit>, Error> {

0 commit comments

Comments
 (0)