2022-07-27 21:31:11 +00:00
|
|
|
use std::fmt::Write;
|
|
|
|
|
2021-12-26 20:18:42 +00:00
|
|
|
use crate::MetadataError;
|
2021-12-07 22:22:24 +00:00
|
|
|
|
2021-12-26 20:18:42 +00:00
|
|
|
use librespot_core::{Error, Session};
|
2021-12-07 22:22:24 +00:00
|
|
|
|
2021-12-26 20:18:42 +00:00
|
|
|
pub type RequestResult = Result<bytes::Bytes, Error>;
|
2021-12-07 22:22:24 +00:00
|
|
|
|
|
|
|
#[async_trait]
|
|
|
|
pub trait MercuryRequest {
|
|
|
|
async fn request(session: &Session, uri: &str) -> RequestResult {
|
2021-12-30 21:36:38 +00:00
|
|
|
let mut metrics_uri = uri.to_owned();
|
|
|
|
|
|
|
|
let separator = match metrics_uri.find('?') {
|
|
|
|
Some(_) => "&",
|
|
|
|
None => "?",
|
|
|
|
};
|
2023-01-27 22:15:51 +00:00
|
|
|
let _ = write!(metrics_uri, "{separator}country={}", session.country());
|
2021-12-30 21:36:38 +00:00
|
|
|
|
|
|
|
if let Some(product) = session.get_user_attribute("type") {
|
2023-01-27 22:15:51 +00:00
|
|
|
let _ = write!(metrics_uri, "&product={product}");
|
2021-12-30 21:36:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
trace!("Requesting {}", metrics_uri);
|
|
|
|
|
|
|
|
let request = session.mercury().get(metrics_uri)?;
|
2021-12-26 20:18:42 +00:00
|
|
|
let response = request.await?;
|
2021-12-07 22:22:24 +00:00
|
|
|
match response.payload.first() {
|
|
|
|
Some(data) => {
|
|
|
|
let data = data.to_vec().into();
|
2023-01-27 22:15:51 +00:00
|
|
|
trace!("Received metadata: {data:?}");
|
2021-12-07 22:22:24 +00:00
|
|
|
Ok(data)
|
|
|
|
}
|
2021-12-26 20:18:42 +00:00
|
|
|
None => Err(Error::unavailable(MetadataError::Empty)),
|
2021-12-07 22:22:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|