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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
use crate::spotify::{
Album, DatedAlbum, Spotify, SpotifyCollection, SpotifyError, SpotifyObject, Track,
};
use crate::srequest::RequestMethod;
use json::JsonValue::Boolean;
use serde_json::Value;
use std::collections::HashMap;
impl Spotify {
/// Get an album: <https://developer.spotify.com/documentation/web-api/reference/#/operations/get-an-album>
///
/// Required scope: none
///
/// # Arguments
/// * `album_id` - The Spotify ID of the album.
/// * `market` - An ISO 3166-1 alpha-2 country code.
///
pub fn get_album(
&self,
album_id: &str,
market: Option<&str>,
) -> Result<Album, SpotifyError> {
let mut url_extension = format!("albums/{}", album_id); // base url
// if market parameter supplied, add to request as query parameter
if let Some(market) = market {
url_extension.push_str(&format!("?market={}", market));
}
let response = self.spotify_request(&url_extension, RequestMethod::Get)?; // make request
return Ok(Album::new(&response)); // format and return result
}
/// Get several albums: <https://developer.spotify.com/documentation/web-api/reference/#/operations/get-multiple-albums>
///
/// Required scope: none
///
/// # Arguments
/// * `album_ids` - A vector of Spotify IDs for the albums.
/// * `market` - An ISO 3166-1 alpha-2 country code.
///
pub fn get_albums(
&self,
album_ids: Vec<&str>,
market: Option<&str>,
) -> Result<Vec<Album>, SpotifyError> {
let mut url_extension = format!("albums/?ids={}", album_ids.join(",")); // base url
// if market parameter supplied, add to request as query parameter
if let Some(market) = market {
url_extension.push_str(&format!("&market={}", market));
}
let response = self.spotify_request(&url_extension, RequestMethod::Get)?; // make request
let mut albums = Vec::new(); // create vector to store albums
for album in response["albums"].members() {
albums.push(Album::new(&album)); // format album and push to vector
}
return Ok(albums); // return vector of albums
}
/// Get an album's tracks: <https://developer.spotify.com/documentation/web-api/reference/#/operations/get-an-albums-tracks>
///
/// Required scope: none
///
/// # Arguments
/// * `album_id` - The Spotify ID of the album.
/// * `market` - An ISO 3166-1 alpha-2 country code.
/// * `limit` - The maximum number of tracks to return. Default: 20. Minimum: 1. Maximum: 50.
/// * `offset` - The index of the first track to return. Default: 0 (the first object). Use with limit to get the next set of tracks.
///
pub fn get_album_tracks(
&self,
album_id: &str,
market: Option<&str>,
limit: Option<u32>,
offset: Option<u32>,
) -> Result<SpotifyCollection<Track>, SpotifyError> {
let mut url_extension = format!("albums/{}/tracks", album_id); // base url
// if any parameter is supplied, add to request as query parameter
if market != None || limit != None || offset != None {
url_extension.push_str("?");
}
// if market parameter supplied, add to request as query parameter
if let Some(market) = market {
url_extension.push_str(&format!("&market={}", market));
}
// if limit parameter supplied, add to request as query parameter
if let Some(limit) = limit {
url_extension.push_str(&format!("&limit={}", limit));
}
// if offset parameter supplied, add to request as query parameter
if let Some(offset) = offset {
url_extension.push_str(&format!("&offset={}", offset));
}
let response = self.spotify_request(&url_extension, RequestMethod::Get)?; // make request
return Ok(SpotifyCollection::<Track>::new(&response)); // format and return result
}
/// Get albums saved in user's library: <https://developer.spotify.com/documentation/web-api/reference/#/operations/get-users-saved-albums>
///
/// Required scope: user-library-read
///
/// # Arguments
/// * `limit` - The maximum number of albums to return. Default: 20. Minimum: 1. Maximum: 50.
/// * `offset` - The index of the first album to return. Default: 0 (the first object). Use with limit to get the next set of albums.
///
pub fn get_saved_albums(
&self,
limit: Option<u32>,
market: Option<&str>,
offset: Option<u32>,
) -> Result<SpotifyCollection<DatedAlbum>, SpotifyError> {
let mut url_extension = String::from("me/albums"); // base url
self.check_scope("user-library-read")?; // check scope
// if any parameter is supplied, add to request as query parameter
if market != None || limit != None || offset != None {
url_extension.push_str("?");
}
// if market parameter supplied, add to request as query parameter
if let Some(market) = market {
url_extension.push_str(&format!("&market={}", market));
}
// if limit parameter supplied, add to request as query parameter
if let Some(limit) = limit {
url_extension.push_str(&format!("&limit={}", limit));
}
// if offset parameter supplied, add to request as query parameter
if let Some(offset) = offset {
url_extension.push_str(&format!("&offset={}", offset));
}
let response = self.spotify_request(&url_extension, RequestMethod::Get)?; // make request
return Ok(SpotifyCollection::<DatedAlbum>::new(&response)); // format and return result
}
/// Save albums for current user: <https://developer.spotify.com/documentation/web-api/reference/#/operations/save-albums-user>
///
/// Required scope: user-library-modify
///
/// # Arguments
/// * `album_ids` - A vector of Spotify IDs for the albums.
///
pub fn save_albums(&self, album_ids: Vec<&str>) -> Result<(), SpotifyError> {
let album_ids_string = album_ids.join(","); // join album ids into string seperated by commas
let url_extension = format!("me/albums?ids={}", album_ids_string); // base url with album ids to add
self.check_scope("user-library-modify")?; // check scope
// create HashMap for request body
let mut body = HashMap::new();
body.insert(
"ids".to_string(),
Value::Array(
album_ids
.iter()
.map(|s| Value::String(s.to_string()))
.collect(),
),
);
self.spotify_request(&url_extension, RequestMethod::Put(body))?; // make request
return Ok(()); // return nothing
}
/// Remove saved albums from current user's library: <https://developer.spotify.com/documentation/web-api/reference/#/operations/remove-albums-user>
///
/// Required scope: user-library-modify
///
/// # Arguments
/// * `album_ids` - A vector of Spotify IDs for the albums.
///
pub fn remove_albums(&self, album_ids: Vec<&str>) -> Result<(), SpotifyError> {
let album_ids_string = album_ids.join(","); // join album ids into string seperated by commas
let url_extension = format!("me/albums?ids={}", album_ids_string); // base url with album ids to remove
self.check_scope("user-library-modify")?; // check scope
// Create HashMap for request body
let mut body = HashMap::new();
body.insert(
"ids".to_string(),
Value::Array(
album_ids
.iter()
.map(|s| Value::String(s.to_string()))
.collect(),
),
);
self.spotify_request(&url_extension, RequestMethod::Delete(body))?; // make request
return Ok(()); // return nothing
}
/// Checks to see if albums are already saved to user's library: <https://developer.spotify.com/documentation/web-api/reference/#/operations/check-users-saved-albums>
///
/// Required scope: user-library-read
///
/// # Arguments
/// * `album_ids` - A vector of Spotify IDs for the albums.
///
pub fn check_saved_albums(&self, album_ids: Vec<&str>) -> Result<Vec<bool>, SpotifyError> {
let album_ids_string = album_ids.join(","); // join album ids into string seperated by commas
let url_extension = format!("me/albums/contains?ids={}", album_ids_string); // base url with album ids to check
self.check_scope("user-library-read")?; // check scope
let response = self.spotify_request(&url_extension, RequestMethod::Get)?; // make request
let mut saved_albums = Vec::new(); // create vector to store saved albums
for album in response.members() {
match album {
Boolean(saved) => saved_albums.push(*saved),
_ => return Err(SpotifyError::RequestError("Invalid response".to_string())),
}
}
return Ok(saved_albums); // return vector of saved albums
}
/// Get a list of new album releases featured in Spotify: <https://developer.spotify.com/documentation/web-api/reference/#/operations/get-new-releases>
///
/// Required scope: none
///
/// # Arguments
/// * `country` - An ISO 3166-1 alpha-2 country code.
/// * `limit` - The maximum number of albums to return. Default: 20. Minimum: 1. Maximum: 50.
/// * `offset` - The index of the first album to return. Default: 0 (the first object). Use with limit to get the next set of albums.
///
pub fn get_new_releases(
&self,
country: Option<&str>,
limit: Option<u32>,
offset: Option<u32>,
) -> Result<SpotifyCollection<Album>, SpotifyError> {
let mut url_extension = String::from("browse/new-releases"); // base url
// if any parameter is supplied, add to request as query parameter
if country != None || limit != None || offset != None {
url_extension.push_str("?");
}
// if country parameter supplied, add to request as query parameter
if let Some(country) = country {
url_extension.push_str(&format!("&country={}", country));
}
// if limit parameter supplied, add to request as query parameter
if let Some(limit) = limit {
url_extension.push_str(&format!("&limit={}", limit));
}
// if offset parameter supplied, add to request as query parameter
if let Some(offset) = offset {
url_extension.push_str(&format!("&offset={}", offset));
}
let response = self.spotify_request(&url_extension, RequestMethod::Get)?; // make request
return Ok(SpotifyCollection::<Album>::new(&response["albums"])); // format and return result
}
}