spotifyrs/categories.rs
1use crate::spotify::{Category, Spotify, SpotifyCollection, SpotifyError, SpotifyObject};
2use crate::srequest::RequestMethod;
3
4impl Spotify {
5 /// Get a set of categories used to tag items in Spotify: <https://developer.spotify.com/documentation/web-api/reference/#/operations/get-categories>
6 ///
7 /// Requires scope: None
8 ///
9 /// # Arguments
10 /// * `country` - An ISO 3166-1 alpha-2 country code.
11 /// * `locale` - The desired language, consisting of an ISO 639 language code and an ISO 3166-1 alpha-2 country code, joined by an underscore.
12 /// * `limit` - The maximum number of categories to return. Default: 20. Minimum: 1. Maximum: 50.
13 /// * `offset` - The index of the first category to return. Default: 0 (the first object). Use with limit to get the next set of categories.
14 ///
15 pub fn get_several_browse_categories(
16 &mut self,
17 country: Option<&str>,
18 locale: Option<&str>,
19 limit: Option<i32>,
20 offset: Option<i32>,
21 ) -> Result<SpotifyCollection<Category>, SpotifyError> {
22 let mut url_extension = String::from("browse/categories"); // base url
23
24 if !country.is_none() || !locale.is_none() || !limit.is_none() || !offset.is_none() {
25 // if any optional arguments are present
26 url_extension.push_str("?"); // add ? to url
27 }
28
29 if let Some(country) = country {
30 url_extension.push_str(&format!("country={}&", country)); // add country to url
31 }
32
33 if let Some(locale) = locale {
34 url_extension.push_str(&format!("locale={}&", locale)); // add locale to url
35 }
36
37 if let Some(limit) = limit {
38 url_extension.push_str(&format!("limit={}&", limit)); // add limit to url
39 }
40
41 if let Some(offset) = offset {
42 url_extension.push_str(&format!("offset={}&", offset)); // add offset to url
43 }
44
45 let response = self.spotify_request(&url_extension, RequestMethod::Get)?; // send request
46
47 Ok(SpotifyCollection::<Category>::new(&response["categories"])) // return collection
48 }
49
50 /// Gets a single Spotify category: <https://developer.spotify.com/documentation/web-api/reference/#/operations/get-a-category>
51 ///
52 /// Requires scope: None
53 ///
54 /// # Arguments
55 /// * `category_id` - The Spotify category ID for the category.
56 /// * `country` - An ISO 3166-1 alpha-2 country code.
57 /// * `locale` - The desired language, consisting of an ISO 639 language code and an ISO 3166-1 alpha-2 country code, joined by an underscore.
58 ///
59 pub fn get_single_browse_category(
60 &mut self,
61 category_id: &str,
62 country: Option<&str>,
63 locale: Option<&str>,
64 ) -> Result<Category, SpotifyError> {
65 let mut url_extension = format!("browse/categories/{}", category_id); // base url
66
67 if !country.is_none() || !locale.is_none() {
68 // if any optional arguments are present
69 url_extension.push_str("?"); // add ? to url
70 }
71
72 if let Some(country) = country {
73 url_extension.push_str(&format!("country={}&", country)); // add country to url
74 }
75
76 if let Some(locale) = locale {
77 url_extension.push_str(&format!("locale={}&", locale)); // add locale to url
78 }
79
80 let response = self.spotify_request(&url_extension, RequestMethod::Get)?; // send request
81
82 Ok(Category::new(&response)) // return category
83 }
84}