mirror of
https://github.com/librespot-org/librespot.git
synced 2024-11-08 16:45:43 +00:00
Add product metrics to requests
This commit is contained in:
parent
286a031d94
commit
2af34fc674
4 changed files with 31 additions and 2 deletions
|
@ -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;
|
||||||
|
|
|
@ -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());
|
||||||
|
|
|
@ -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)
|
||||||
|
|
|
@ -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) => {
|
||||||
|
|
Loading…
Reference in a new issue