1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#![allow(clippy::missing_errors_doc)]
use std::future::Future;
use std::iter;
use std::time::Instant;
use futures_util::stream::{FuturesOrdered, FuturesUnordered, StreamExt, TryStreamExt};
use isocountry::CountryCode;
use crate::{Client, Error, Response};
pub use albums::*;
pub use artists::*;
pub use browse::*;
pub use episodes::*;
pub use follow::*;
pub use library::*;
pub use personalization::*;
pub use player::*;
pub use playlists::*;
pub use search::*;
pub use shows::*;
pub use tracks::*;
pub use users_profile::*;
macro_rules! endpoint {
($path:literal) => {
concat!("https://api.spotify.com", $path)
};
($path:literal, $($fmt:tt)*) => {
&format!(endpoint!($path), $($fmt)*)
};
}
mod albums;
mod artists;
mod browse;
mod episodes;
mod follow;
mod library;
mod personalization;
mod player;
mod playlists;
mod search;
mod shows;
mod tracks;
mod users_profile;
impl Client {
#[must_use]
pub const fn albums(&self) -> Albums<'_> {
Albums(self)
}
#[must_use]
pub const fn artists(&self) -> Artists<'_> {
Artists(self)
}
#[must_use]
pub const fn browse(&self) -> Browse<'_> {
Browse(self)
}
#[must_use]
pub const fn episodes(&self) -> Episodes<'_> {
Episodes(self)
}
#[must_use]
pub const fn follow(&self) -> Follow<'_> {
Follow(self)
}
#[must_use]
pub const fn library(&self) -> Library<'_> {
Library(self)
}
#[must_use]
pub const fn personalization(&self) -> Personalization<'_> {
Personalization(self)
}
#[must_use]
pub const fn player(&self) -> Player<'_> {
Player(self)
}
#[must_use]
pub const fn playlists(&self) -> Playlists<'_> {
Playlists(self)
}
#[must_use]
pub const fn search(&self) -> Search<'_> {
Search(self)
}
#[must_use]
pub const fn shows(&self) -> Shows<'_> {
Shows(self)
}
#[must_use]
pub const fn tracks(&self) -> Tracks<'_> {
Tracks(self)
}
#[must_use]
pub const fn users_profile(&self) -> UsersProfile<'_> {
UsersProfile(self)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Market {
Country(CountryCode),
FromToken,
}
impl Market {
fn as_str(self) -> &'static str {
match self {
Market::Country(code) => code.alpha2(),
Market::FromToken => "from_token",
}
}
fn query(self) -> (&'static str, &'static str) {
("market", self.as_str())
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum TimeRange {
Short,
Medium,
Long,
}
impl TimeRange {
fn as_str(self) -> &'static str {
match self {
Self::Long => "long_term",
Self::Medium => "medium_term",
Self::Short => "short_term",
}
}
}
type Chunk<'a, I> = iter::Take<&'a mut iter::Peekable<I>>;
async fn chunked_sequence<I: IntoIterator, Fut, T>(
items: I,
chunk_size: usize,
mut f: impl FnMut(Chunk<'_, I::IntoIter>) -> Fut,
) -> Result<Response<Vec<T>>, Error>
where
Fut: Future<Output = Result<Response<Vec<T>>, Error>>,
{
let mut items = items.into_iter().peekable();
let mut futures = FuturesOrdered::new();
while items.peek().is_some() {
futures.push(f(items.by_ref().take(chunk_size)));
}
let mut response = Response {
data: Vec::new(),
expires: Instant::now(),
};
while let Some(mut r) = futures.next().await.transpose()? {
response.data.append(&mut r.data);
response.expires = r.expires;
}
Ok(response)
}
async fn chunked_requests<I: IntoIterator, Fut>(
items: I,
chunk_size: usize,
mut f: impl FnMut(Chunk<'_, I::IntoIter>) -> Fut,
) -> Result<(), Error>
where
Fut: Future<Output = Result<(), Error>>,
{
let mut items = items.into_iter().peekable();
let futures = FuturesUnordered::new();
while items.peek().is_some() {
futures.push(f(items.by_ref().take(chunk_size)));
}
futures.try_collect().await
}
#[cfg(test)]
fn client() -> crate::Client {
dotenv::dotenv().unwrap();
let mut client = crate::Client::with_refresh(
crate::ClientCredentials::from_env().unwrap(),
std::fs::read_to_string(".refresh_token").unwrap(),
);
client.debug = true;
client
}