am_api/resource/
history.rs

1//! History implementation
2
3use crate::error::Error;
4use crate::primitive::TrackType;
5use crate::request::builder::MusicRequestBuilder;
6use crate::request::paginated::paginate;
7use crate::resource::Resource;
8use crate::ApiClient;
9use futures::Stream;
10
11/// History
12pub struct History;
13
14impl History {
15    /// Get history information
16    pub fn get<'a>() -> HistoryGetRequestBuilder<'a> {
17        HistoryGetRequestBuilder::default()
18    }
19}
20
21/// History request builder
22pub struct HistoryRequestBuilder;
23
24/// History get request builder
25pub type HistoryGetRequestBuilder<'a> = MusicRequestBuilder<'a, HistoryRequestBuilder>;
26
27impl<'a> HistoryGetRequestBuilder<'a> {
28    /// Fetch heavy rotation content
29    ///
30    /// # Params
31    ///
32    /// * limit - limit of entries per query
33    ///
34    /// * offset - query offset
35    ///
36    /// Possible resources: any
37    pub async fn heavy_rotation(
38        mut self,
39        client: &ApiClient,
40        limit: usize,
41        offset: usize,
42    ) -> impl Stream<Item = Result<Resource, Error>> {
43        let mut request_context = self.get_request_context_drain(client);
44        request_context
45            .query
46            .push((String::from("limit"), limit.to_string()));
47
48        paginate(
49            client.clone(),
50            String::from("/v1/me/history/heavy-rotation"),
51            request_context,
52            offset,
53        )
54    }
55
56    /// Fetch recently played resources
57    ///
58    /// # Params
59    ///
60    /// * limit - limit of entries per query
61    ///
62    /// * offset - query offset
63    ///
64    /// Possible resources: any
65    pub async fn recently_played(
66        mut self,
67        client: &ApiClient,
68        limit: usize,
69        offset: usize,
70    ) -> impl Stream<Item = Result<Resource, Error>> {
71        let mut request_context = self.get_request_context_drain(client);
72        request_context
73            .query
74            .push((String::from("limit"), limit.to_string()));
75
76        paginate(
77            client.clone(),
78            String::from("/v1/me/recent/played"),
79            request_context,
80            offset,
81        )
82    }
83
84    /// Fetch recently played tracks
85    ///
86    /// # Params
87    ///
88    /// * limit - limit of entries per query
89    ///
90    /// * offset - query offset
91    ///
92    /// Possible resources: [`LibraryMusicVideo`], [`LibrarySong`], [`MusicVideo`], [`Song`]
93    pub async fn recently_played_tracks(
94        mut self,
95        client: &ApiClient,
96        tracks: &[TrackType],
97        limit: usize,
98        offset: usize,
99    ) -> impl Stream<Item = Result<Resource, Error>> {
100        let mut request_context = self.get_request_context_drain(client);
101        request_context
102            .query
103            .push((String::from("limit"), limit.to_string()));
104        request_context.query.push((
105            String::from("types"),
106            tracks
107                .iter()
108                .map(|e| e.to_string())
109                .collect::<Vec<_>>()
110                .join(","),
111        ));
112
113        paginate(
114            client.clone(),
115            String::from("/v1/me/recent/played/tracks"),
116            request_context,
117            offset,
118        )
119    }
120
121    /// Fetch recently played radio stations
122    ///
123    /// # Params
124    ///
125    /// * limit - limit of entries per query
126    ///
127    /// * offset - query offset
128    ///
129    /// Possible resources: [`Station`]
130    pub async fn recently_played_stations(
131        mut self,
132        client: &ApiClient,
133        limit: usize,
134        offset: usize,
135    ) -> impl Stream<Item = Result<Resource, Error>> {
136        let mut request_context = self.get_request_context_drain(client);
137        request_context
138            .query
139            .push((String::from("limit"), limit.to_string()));
140
141        paginate(
142            client.clone(),
143            String::from("/v1/me/recent/radio-stations"),
144            request_context,
145            offset,
146        )
147    }
148
149    /// Fetch resources recently added to the library
150    /// # Params
151    ///
152    /// * limit - limit of entries per query
153    ///
154    /// * offset - query offset
155    ///
156    /// Possible resources: [`LibraryAlbum`], [`LibraryArtist`], [`LibraryPlaylist`], [`LibrarySong`]
157    pub async fn recently_added_to_library(
158        mut self,
159        client: &ApiClient,
160        limit: usize,
161        offset: usize,
162    ) -> impl Stream<Item = Result<Resource, Error>> {
163        let mut request_context = self.get_request_context_drain(client);
164        request_context
165            .query
166            .push((String::from("limit"), limit.to_string()));
167
168        paginate(
169            client.clone(),
170            String::from("/v1/me/library/recently-added"),
171            request_context,
172            offset,
173        )
174    }
175}