Add product metrics to requests

This commit is contained in:
Roderick van Domburg 2021-12-30 22:36:38 +01:00
parent 286a031d94
commit 2af34fc674
No known key found for this signature in database
GPG key ID: A9EF5222A26F0451
4 changed files with 31 additions and 2 deletions

View file

@ -129,7 +129,7 @@ impl HttpClient {
} }
pub async fn request(&self, req: Request<Body>) -> Result<Response<Body>, Error> { pub async fn request(&self, req: Request<Body>) -> Result<Response<Body>, Error> {
debug!("Requesting {:?}", req.uri().to_string()); debug!("Requesting {}", req.uri().to_string());
let request = self.request_fut(req)?; let request = self.request_fut(req)?;
let response = request.await; let response = request.await;

View file

@ -334,6 +334,9 @@ impl Session {
&self.0.config &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 { pub fn user_data(&self) -> UserData {
self.0.data.read().user_data.clone() self.0.data.read().user_data.clone()
} }
@ -354,6 +357,10 @@ impl Session {
self.0.data.read().user_data.canonical_username.clone() 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<String> { pub fn set_user_attribute(&self, key: &str, value: &str) -> Option<String> {
let mut dummy_attributes = UserAttributes::new(); let mut dummy_attributes = UserAttributes::new();
dummy_attributes.insert(key.to_owned(), value.to_owned()); dummy_attributes.insert(key.to_owned(), value.to_owned());

View file

@ -136,6 +136,14 @@ impl SpClient {
let mut url = self.base_url().await; let mut url = self.base_url().await;
url.push_str(endpoint); 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() let mut request = Request::builder()
.method(method) .method(method)
.uri(url) .uri(url)

View file

@ -7,7 +7,21 @@ pub type RequestResult = Result<bytes::Bytes, Error>;
#[async_trait] #[async_trait]
pub trait MercuryRequest { pub trait MercuryRequest {
async fn request(session: &Session, uri: &str) -> RequestResult { 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?; let response = request.await?;
match response.payload.first() { match response.payload.first() {
Some(data) => { Some(data) => {