librespot/metadata/src/request.rs

38 lines
1.1 KiB
Rust
Raw Normal View History

use std::fmt::Write;
use crate::MetadataError;
use librespot_core::{Error, Session};
pub type RequestResult = Result<bytes::Bytes, Error>;
#[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 => "?",
};
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") {
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)?;
let response = request.await?;
match response.payload.first() {
Some(data) => {
let data = data.to_vec().into();
trace!("Received metadata: {data:?}");
Ok(data)
}
None => Err(Error::unavailable(MetadataError::Empty)),
}
}
}