use crate::{
api::{Endpoint, prelude::*},
model::QueryRange,
};
#[derive(Debug, Clone)]
pub struct GetRecentlyPlayedTracks {
pub limit: Option<u8>,
pub timeframe: QueryRange,
}
impl Endpoint for GetRecentlyPlayedTracks {
fn method(&self) -> Method {
Method::GET
}
fn endpoint(&self) -> Cow<'static, str> {
"me/player/recently-played".into()
}
fn parameters(&self) -> QueryParams<'_> {
let limit = self.limit.unwrap_or(20).clamp(1, 50);
let mut params = QueryParams::default();
params.push("limit", &limit);
match self.timeframe {
QueryRange::Before(time) => {
params.push("before", &time);
}
QueryRange::After(time) => {
params.push("after", &time);
}
}
params
}
}
impl From<QueryRange> for GetRecentlyPlayedTracks {
fn from(timeframe: QueryRange) -> Self {
Self {
limit: None,
timeframe,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{
api::{self, Query as _},
test::client::{ExpectedUrl, SingleTestClient},
};
#[test]
fn test_get_recently_played_tracks_endpoint() {
let endpoint = ExpectedUrl::builder()
.endpoint("me/player/recently-played")
.add_query_params(&[("limit", "20")])
.add_query_params(&[("before", "1733877079")])
.build();
let client = SingleTestClient::new_raw(endpoint, "");
api::ignore(GetRecentlyPlayedTracks::from(QueryRange::Before(
1733877079,
)))
.query(&client)
.unwrap();
}
}