From 2af34fc674a9e5c1c0c844a18d2908e75b615e17 Mon Sep 17 00:00:00 2001 From: Roderick van Domburg Date: Thu, 30 Dec 2021 22:36:38 +0100 Subject: [PATCH] Add product metrics to requests --- core/src/http_client.rs | 2 +- core/src/session.rs | 7 +++++++ core/src/spclient.rs | 8 ++++++++ metadata/src/request.rs | 16 +++++++++++++++- 4 files changed, 31 insertions(+), 2 deletions(-) diff --git a/core/src/http_client.rs b/core/src/http_client.rs index 1cdfcf75..e445b953 100644 --- a/core/src/http_client.rs +++ b/core/src/http_client.rs @@ -129,7 +129,7 @@ impl HttpClient { } pub async fn request(&self, req: Request) -> Result, Error> { - debug!("Requesting {:?}", req.uri().to_string()); + debug!("Requesting {}", req.uri().to_string()); let request = self.request_fut(req)?; let response = request.await; diff --git a/core/src/session.rs b/core/src/session.rs index 6b5a06df..aecdaada 100644 --- a/core/src/session.rs +++ b/core/src/session.rs @@ -334,6 +334,9 @@ impl Session { &self.0.config } + // This clones a fairly large struct, so use a specific getter or setter unless + // you need more fields at once, in which case this can spare multiple `read` + // locks. pub fn user_data(&self) -> UserData { self.0.data.read().user_data.clone() } @@ -354,6 +357,10 @@ impl Session { self.0.data.read().user_data.canonical_username.clone() } + pub fn country(&self) -> String { + self.0.data.read().user_data.country.clone() + } + pub fn set_user_attribute(&self, key: &str, value: &str) -> Option { let mut dummy_attributes = UserAttributes::new(); dummy_attributes.insert(key.to_owned(), value.to_owned()); diff --git a/core/src/spclient.rs b/core/src/spclient.rs index addb547d..ffc2ebba 100644 --- a/core/src/spclient.rs +++ b/core/src/spclient.rs @@ -136,6 +136,14 @@ impl SpClient { let mut url = self.base_url().await; url.push_str(endpoint); + // Add metrics. There is also an optional `partner` key with a value like + // `vodafone-uk` but we've yet to discover how we can find that value. + let separator = match url.find('?') { + Some(_) => "&", + None => "?", + }; + url.push_str(&format!("{}product=0", separator)); + let mut request = Request::builder() .method(method) .uri(url) diff --git a/metadata/src/request.rs b/metadata/src/request.rs index 2ebd4037..df276722 100644 --- a/metadata/src/request.rs +++ b/metadata/src/request.rs @@ -7,7 +7,21 @@ pub type RequestResult = Result; #[async_trait] pub trait MercuryRequest { async fn request(session: &Session, uri: &str) -> RequestResult { - let request = session.mercury().get(uri)?; + let mut metrics_uri = uri.to_owned(); + + let separator = match metrics_uri.find('?') { + Some(_) => "&", + None => "?", + }; + metrics_uri.push_str(&format!("{}country={}", separator, session.country())); + + if let Some(product) = session.get_user_attribute("type") { + metrics_uri.push_str(&format!("&product={}", product)); + } + + trace!("Requesting {}", metrics_uri); + + let request = session.mercury().get(metrics_uri)?; let response = request.await?; match response.payload.first() { Some(data) => {