| 
									
										
										
										
											2021-01-13 10:52:58 +00:00
										 |  |  | use serde::de::DeserializeOwned;
 | 
					
						
							|  |  |  | use serde::Serialize;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | use ureq::Agent;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | use crate::endpoint::Endpoint;
 | 
					
						
							| 
									
										
										
										
											2021-01-19 22:41:54 +00:00
										 |  |  | use crate::raw::request::*;
 | 
					
						
							|  |  |  | use crate::raw::response::*;
 | 
					
						
							| 
									
										
										
										
											2021-01-13 13:28:59 +00:00
										 |  |  | use crate::Error;
 | 
					
						
							| 
									
										
										
										
											2021-01-13 10:52:58 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | const API_ROOT_URL: &str = "https://api.listenbrainz.org/1/";
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-13 12:06:48 +00:00
										 |  |  | /// Low-level client that directly wraps the ListenBrainz HTTP API.
 | 
					
						
							| 
									
										
										
										
											2021-01-13 10:52:58 +00:00
										 |  |  | ///
 | 
					
						
							|  |  |  | /// Client exposes functions that map one-to-one to the API methods described
 | 
					
						
							|  |  |  | /// in the [ListenBrainz API docs](https://listenbrainz.readthedocs.io/en/production/dev/api/).
 | 
					
						
							| 
									
										
										
										
											2021-01-15 22:34:53 +00:00
										 |  |  | ///
 | 
					
						
							|  |  |  | /// # Errors
 | 
					
						
							|  |  |  | ///
 | 
					
						
							|  |  |  | /// Client's methods can return the following errors:
 | 
					
						
							|  |  |  | /// - [`Error::Api`]: the API returned a non-`2XX` status.
 | 
					
						
							|  |  |  | /// - [`Error::RequestJson`]: the request data could not be converted into JSON.
 | 
					
						
							|  |  |  | /// - [`Error::ResponseJson`]: the response data could not be converted into JSON.
 | 
					
						
							|  |  |  | /// - [`Error::Http`]: there was some other HTTP error while interacting with the API.
 | 
					
						
							| 
									
										
										
										
											2021-01-13 10:52:58 +00:00
										 |  |  | pub struct Client {
 | 
					
						
							|  |  |  |     agent: Agent,
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | impl Client {
 | 
					
						
							|  |  |  |     /// Construct a new client.
 | 
					
						
							|  |  |  |     pub fn new() -> Self {
 | 
					
						
							|  |  |  |         Self {
 | 
					
						
							|  |  |  |             agent: ureq::agent(),
 | 
					
						
							|  |  |  |         }
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-15 22:34:53 +00:00
										 |  |  |     /// Helper method to perform a GET request against an endpoint
 | 
					
						
							|  |  |  |     /// without any query parameters.
 | 
					
						
							| 
									
										
										
										
											2021-01-14 22:48:27 +00:00
										 |  |  |     fn get<R: DeserializeOwned>(&self, endpoint: Endpoint) -> Result<R, Error> {
 | 
					
						
							| 
									
										
										
										
											2021-01-13 10:52:58 +00:00
										 |  |  |         let endpoint = format!("{}{}", API_ROOT_URL, endpoint);
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         self.agent
 | 
					
						
							|  |  |  |             .get(&endpoint)
 | 
					
						
							|  |  |  |             .call()?
 | 
					
						
							|  |  |  |             .into_json()
 | 
					
						
							|  |  |  |             .map_err(Error::ResponseJson)
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-15 22:34:53 +00:00
										 |  |  |     /// Helper method to perform a GET request against (most) statistics
 | 
					
						
							|  |  |  |     /// endpoints that share common query parameters.
 | 
					
						
							| 
									
										
										
										
											2021-01-13 16:28:12 +00:00
										 |  |  |     fn get_stats<R: DeserializeOwned>(
 | 
					
						
							| 
									
										
										
										
											2021-01-14 22:48:27 +00:00
										 |  |  |         &self,
 | 
					
						
							| 
									
										
										
										
											2021-01-13 16:28:12 +00:00
										 |  |  |         endpoint: Endpoint,
 | 
					
						
							|  |  |  |         count: Option<u64>,
 | 
					
						
							|  |  |  |         offset: Option<u64>,
 | 
					
						
							|  |  |  |         range: Option<&str>,
 | 
					
						
							| 
									
										
										
										
											2021-01-14 01:42:33 +00:00
										 |  |  |     ) -> Result<Option<R>, Error> {
 | 
					
						
							| 
									
										
										
										
											2021-01-13 16:28:12 +00:00
										 |  |  |         let endpoint = format!("{}{}", API_ROOT_URL, endpoint);
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         let mut request = self.agent.get(&endpoint);
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if let Some(count) = count {
 | 
					
						
							|  |  |  |             request = request.query("count", &count.to_string());
 | 
					
						
							|  |  |  |         }
 | 
					
						
							|  |  |  |         if let Some(offset) = offset {
 | 
					
						
							|  |  |  |             request = request.query("offset", &offset.to_string());
 | 
					
						
							|  |  |  |         }
 | 
					
						
							|  |  |  |         if let Some(range) = range {
 | 
					
						
							|  |  |  |             request = request.query("range", range);
 | 
					
						
							|  |  |  |         }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-14 01:42:33 +00:00
										 |  |  |         let response = request.call()?;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // API returns 204 and an empty document if there are no statistics
 | 
					
						
							|  |  |  |         if response.status() == 204 {
 | 
					
						
							|  |  |  |             Ok(None)
 | 
					
						
							|  |  |  |         } else {
 | 
					
						
							|  |  |  |             response.into_json().map(Some).map_err(Error::ResponseJson)
 | 
					
						
							|  |  |  |         }
 | 
					
						
							| 
									
										
										
										
											2021-01-13 16:28:12 +00:00
										 |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-15 22:34:53 +00:00
										 |  |  |     /// Helper method to perform a POST request against an endpoint
 | 
					
						
							|  |  |  |     /// that expects `Serialize`-able input data.
 | 
					
						
							| 
									
										
										
										
											2021-01-14 22:48:27 +00:00
										 |  |  |     fn post<D, R>(&self, endpoint: Endpoint, token: &str, data: D) -> Result<R, Error>
 | 
					
						
							| 
									
										
										
										
											2021-01-13 10:52:58 +00:00
										 |  |  |     where
 | 
					
						
							|  |  |  |         D: Serialize,
 | 
					
						
							|  |  |  |         R: DeserializeOwned,
 | 
					
						
							|  |  |  |     {
 | 
					
						
							|  |  |  |         let data = serde_json::to_value(data).map_err(Error::RequestJson)?;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         let endpoint = format!("{}{}", API_ROOT_URL, endpoint);
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         self.agent
 | 
					
						
							|  |  |  |             .post(&endpoint)
 | 
					
						
							|  |  |  |             .set("Authorization", &format!("Token {}", token))
 | 
					
						
							|  |  |  |             .send_json(data)?
 | 
					
						
							|  |  |  |             .into_json()
 | 
					
						
							|  |  |  |             .map_err(Error::ResponseJson)
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-14 02:03:04 +00:00
										 |  |  |     /// Endpoint: [`submit-listens`](https://listenbrainz.readthedocs.io/en/production/dev/api/#post--1-submit-listens)
 | 
					
						
							| 
									
										
										
										
											2021-01-13 10:52:58 +00:00
										 |  |  |     pub fn submit_listens(
 | 
					
						
							| 
									
										
										
										
											2021-01-14 22:48:27 +00:00
										 |  |  |         &self,
 | 
					
						
							| 
									
										
										
										
											2021-01-13 10:52:58 +00:00
										 |  |  |         token: &str,
 | 
					
						
							|  |  |  |         data: SubmitListens,
 | 
					
						
							|  |  |  |     ) -> Result<SubmitListensResponse, Error> {
 | 
					
						
							|  |  |  |         self.post(Endpoint::SubmitListens, token, data)
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-14 02:03:04 +00:00
										 |  |  |     /// Endpoint: [`validate-token`](https://listenbrainz.readthedocs.io/en/production/dev/api/#get--1-validate-token)
 | 
					
						
							| 
									
										
										
										
											2021-01-14 22:48:27 +00:00
										 |  |  |     pub fn validate_token(&self, token: &str) -> Result<ValidateTokenResponse, Error> {
 | 
					
						
							| 
									
										
										
										
											2021-01-13 10:52:58 +00:00
										 |  |  |         let endpoint = format!("{}{}", API_ROOT_URL, Endpoint::ValidateToken);
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         self.agent
 | 
					
						
							|  |  |  |             .get(&endpoint)
 | 
					
						
							|  |  |  |             .query("token", token)
 | 
					
						
							|  |  |  |             .call()?
 | 
					
						
							|  |  |  |             .into_json()
 | 
					
						
							|  |  |  |             .map_err(Error::ResponseJson)
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-14 02:03:04 +00:00
										 |  |  |     /// Endpoint: [`delete-listen`](https://listenbrainz.readthedocs.io/en/production/dev/api/#post--1-delete-listen)
 | 
					
						
							| 
									
										
										
										
											2021-01-13 10:52:58 +00:00
										 |  |  |     pub fn delete_listen(
 | 
					
						
							| 
									
										
										
										
											2021-01-14 22:48:27 +00:00
										 |  |  |         &self,
 | 
					
						
							| 
									
										
										
										
											2021-01-13 10:52:58 +00:00
										 |  |  |         token: &str,
 | 
					
						
							|  |  |  |         data: DeleteListen,
 | 
					
						
							|  |  |  |     ) -> Result<DeleteListenResponse, Error> {
 | 
					
						
							|  |  |  |         self.post(Endpoint::DeleteListen, token, data)
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-14 02:03:04 +00:00
										 |  |  |     /// Endpoint: [`users/{user_list}/recent-listens`](https://listenbrainz.readthedocs.io/en/production/dev/api/#get--1-users-(user_list)-recent-listens)
 | 
					
						
							| 
									
										
										
										
											2021-01-13 10:52:58 +00:00
										 |  |  |     pub fn users_recent_listens(
 | 
					
						
							| 
									
										
										
										
											2021-01-14 22:48:27 +00:00
										 |  |  |         &self,
 | 
					
						
							| 
									
										
										
										
											2021-01-13 10:52:58 +00:00
										 |  |  |         user_list: &[&str],
 | 
					
						
							|  |  |  |     ) -> Result<UsersRecentListensResponse, Error> {
 | 
					
						
							|  |  |  |         self.get(Endpoint::UsersRecentListens(user_list))
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-14 02:03:04 +00:00
										 |  |  |     /// Endpoint: [`user/{user_name}/listen-count`](https://listenbrainz.readthedocs.io/en/production/dev/api/#get--1-user-(user_name)-listen-count)
 | 
					
						
							| 
									
										
										
										
											2021-01-14 22:48:27 +00:00
										 |  |  |     pub fn user_listen_count(&self, user_name: &str) -> Result<UserListenCountResponse, Error> {
 | 
					
						
							| 
									
										
										
										
											2021-01-13 10:52:58 +00:00
										 |  |  |         self.get(Endpoint::UserListenCount(user_name))
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-14 02:03:04 +00:00
										 |  |  |     /// Endpoint: [`user/{user_name}/playing-now`](https://listenbrainz.readthedocs.io/en/production/dev/api/#get--1-user-(user_name)-playing-now)
 | 
					
						
							| 
									
										
										
										
											2021-01-14 22:48:27 +00:00
										 |  |  |     pub fn user_playing_now(&self, user_name: &str) -> Result<UserPlayingNowResponse, Error> {
 | 
					
						
							| 
									
										
										
										
											2021-01-13 10:52:58 +00:00
										 |  |  |         self.get(Endpoint::UserPlayingNow(user_name))
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-14 02:03:04 +00:00
										 |  |  |     /// Endpoint: [`user/{user_name}/listens`](https://listenbrainz.readthedocs.io/en/production/dev/api/#get--1-user-(user_name)-listens)
 | 
					
						
							| 
									
										
										
										
											2021-01-13 10:52:58 +00:00
										 |  |  |     pub fn user_listens(
 | 
					
						
							| 
									
										
										
										
											2021-01-14 22:48:27 +00:00
										 |  |  |         &self,
 | 
					
						
							| 
									
										
										
										
											2021-01-13 10:52:58 +00:00
										 |  |  |         user_name: &str,
 | 
					
						
							|  |  |  |         min_ts: Option<i64>,
 | 
					
						
							|  |  |  |         max_ts: Option<i64>,
 | 
					
						
							| 
									
										
										
										
											2021-01-13 16:28:12 +00:00
										 |  |  |         count: Option<u64>,
 | 
					
						
							| 
									
										
										
										
											2021-01-13 13:28:59 +00:00
										 |  |  |         time_range: Option<u64>,
 | 
					
						
							| 
									
										
										
										
											2021-01-13 10:52:58 +00:00
										 |  |  |     ) -> Result<UserListensResponse, Error> {
 | 
					
						
							|  |  |  |         let endpoint = format!("{}{}", API_ROOT_URL, Endpoint::UserListens(user_name));
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         let mut request = self.agent.get(&endpoint);
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if let Some(min_ts) = min_ts {
 | 
					
						
							|  |  |  |             request = request.query("min_ts", &min_ts.to_string());
 | 
					
						
							|  |  |  |         }
 | 
					
						
							|  |  |  |         if let Some(max_ts) = max_ts {
 | 
					
						
							|  |  |  |             request = request.query("max_ts", &max_ts.to_string());
 | 
					
						
							|  |  |  |         }
 | 
					
						
							|  |  |  |         if let Some(count) = count {
 | 
					
						
							|  |  |  |             request = request.query("count", &count.to_string());
 | 
					
						
							|  |  |  |         }
 | 
					
						
							|  |  |  |         if let Some(time_range) = time_range {
 | 
					
						
							|  |  |  |             request = request.query("time_range", &time_range.to_string());
 | 
					
						
							|  |  |  |         }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         request.call()?.into_json().map_err(Error::ResponseJson)
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-14 02:03:04 +00:00
										 |  |  |     /// Endpoint: [`latest-import`](https://listenbrainz.readthedocs.io/en/production/dev/api/#get--1-latest-import) (`GET`)
 | 
					
						
							| 
									
										
										
										
											2021-01-14 22:48:27 +00:00
										 |  |  |     pub fn get_latest_import(&self, user_name: &str) -> Result<GetLatestImportResponse, Error> {
 | 
					
						
							| 
									
										
										
										
											2021-01-13 10:52:58 +00:00
										 |  |  |         let endpoint = format!("{}{}", API_ROOT_URL, Endpoint::LatestImport);
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         self.agent
 | 
					
						
							|  |  |  |             .get(&endpoint)
 | 
					
						
							|  |  |  |             .query("user_name", user_name)
 | 
					
						
							|  |  |  |             .call()?
 | 
					
						
							|  |  |  |             .into_json()
 | 
					
						
							|  |  |  |             .map_err(Error::ResponseJson)
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-14 02:03:04 +00:00
										 |  |  |     /// Endpoint: [`latest-import`](https://listenbrainz.readthedocs.io/en/production/dev/api/#post--1-latest-import) (`POST`)
 | 
					
						
							| 
									
										
										
										
											2021-01-13 10:52:58 +00:00
										 |  |  |     pub fn update_latest_import(
 | 
					
						
							| 
									
										
										
										
											2021-01-14 22:48:27 +00:00
										 |  |  |         &self,
 | 
					
						
							| 
									
										
										
										
											2021-01-13 10:52:58 +00:00
										 |  |  |         token: &str,
 | 
					
						
							|  |  |  |         data: UpdateLatestImport,
 | 
					
						
							|  |  |  |     ) -> Result<UpdateLatestImportResponse, Error> {
 | 
					
						
							|  |  |  |         self.post(Endpoint::LatestImport, token, data)
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-14 02:03:04 +00:00
										 |  |  |     /// Endpoint: [`stats/sitewide/artists`](https://listenbrainz.readthedocs.io/en/production/dev/api/#get--1-stats-sitewide-artists)
 | 
					
						
							| 
									
										
										
										
											2021-01-13 10:52:58 +00:00
										 |  |  |     pub fn stats_sitewide_artists(
 | 
					
						
							| 
									
										
										
										
											2021-01-14 22:48:27 +00:00
										 |  |  |         &self,
 | 
					
						
							| 
									
										
										
										
											2021-01-13 10:52:58 +00:00
										 |  |  |         count: Option<u64>,
 | 
					
						
							|  |  |  |         offset: Option<u64>,
 | 
					
						
							| 
									
										
										
										
											2021-01-13 13:28:59 +00:00
										 |  |  |         range: Option<&str>,
 | 
					
						
							| 
									
										
										
										
											2021-01-14 01:42:33 +00:00
										 |  |  |     ) -> Result<Option<StatsSitewideArtistsResponse>, Error> {
 | 
					
						
							| 
									
										
										
										
											2021-01-13 16:28:12 +00:00
										 |  |  |         self.get_stats(Endpoint::StatsSitewideArtists, count, offset, range)
 | 
					
						
							| 
									
										
										
										
											2021-01-13 10:52:58 +00:00
										 |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-14 02:03:04 +00:00
										 |  |  |     /// Endpoint: [`stats/user/{user_name}/listening-activity`](https://listenbrainz.readthedocs.io/en/production/dev/api/#get--1-stats-user-(user_name)-listening-activity)
 | 
					
						
							| 
									
										
										
										
											2021-01-13 12:06:48 +00:00
										 |  |  |     pub fn stats_user_listening_activity(
 | 
					
						
							| 
									
										
										
										
											2021-01-14 22:48:27 +00:00
										 |  |  |         &self,
 | 
					
						
							| 
									
										
										
										
											2021-01-13 12:06:48 +00:00
										 |  |  |         user_name: &str,
 | 
					
						
							|  |  |  |         range: Option<&str>,
 | 
					
						
							| 
									
										
										
										
											2021-01-14 01:42:33 +00:00
										 |  |  |     ) -> Result<Option<StatsUserListeningActivityResponse>, Error> {
 | 
					
						
							| 
									
										
										
										
											2021-01-13 13:28:59 +00:00
										 |  |  |         let endpoint = format!(
 | 
					
						
							|  |  |  |             "{}{}",
 | 
					
						
							|  |  |  |             API_ROOT_URL,
 | 
					
						
							|  |  |  |             Endpoint::StatsUserListeningActivity(user_name)
 | 
					
						
							|  |  |  |         );
 | 
					
						
							| 
									
										
										
										
											2021-01-13 12:06:48 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         let mut request = self.agent.get(&endpoint);
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if let Some(range) = range {
 | 
					
						
							|  |  |  |             request = request.query("range", range);
 | 
					
						
							|  |  |  |         }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-14 01:42:33 +00:00
										 |  |  |         let response = request.call()?;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // API returns 204 and an empty document if there are no statistics
 | 
					
						
							|  |  |  |         if response.status() == 204 {
 | 
					
						
							|  |  |  |             Ok(None)
 | 
					
						
							|  |  |  |         } else {
 | 
					
						
							|  |  |  |             response.into_json().map(Some).map_err(Error::ResponseJson)
 | 
					
						
							|  |  |  |         }
 | 
					
						
							| 
									
										
										
										
											2021-01-13 12:06:48 +00:00
										 |  |  |     }
 | 
					
						
							| 
									
										
										
										
											2021-01-13 10:52:58 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-14 02:03:04 +00:00
										 |  |  |     /// Endpoint: [`stats/user/{user_name}/daily-activity`](https://listenbrainz.readthedocs.io/en/production/dev/api/#get--1-stats-user-(user_name)-daily-activity)
 | 
					
						
							| 
									
										
										
										
											2021-01-13 12:06:48 +00:00
										 |  |  |     pub fn stats_user_daily_activity(
 | 
					
						
							| 
									
										
										
										
											2021-01-14 22:48:27 +00:00
										 |  |  |         &self,
 | 
					
						
							| 
									
										
										
										
											2021-01-13 12:06:48 +00:00
										 |  |  |         user_name: &str,
 | 
					
						
							|  |  |  |         range: Option<&str>,
 | 
					
						
							| 
									
										
										
										
											2021-01-14 01:42:33 +00:00
										 |  |  |     ) -> Result<Option<StatsUserDailyActivityResponse>, Error> {
 | 
					
						
							| 
									
										
										
										
											2021-01-13 13:28:59 +00:00
										 |  |  |         let endpoint = format!(
 | 
					
						
							|  |  |  |             "{}{}",
 | 
					
						
							|  |  |  |             API_ROOT_URL,
 | 
					
						
							|  |  |  |             Endpoint::StatsUserDailyActivity(user_name)
 | 
					
						
							|  |  |  |         );
 | 
					
						
							| 
									
										
										
										
											2021-01-13 12:06:48 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         let mut request = self.agent.get(&endpoint);
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if let Some(range) = range {
 | 
					
						
							|  |  |  |             request = request.query("range", range);
 | 
					
						
							|  |  |  |         }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-14 01:42:33 +00:00
										 |  |  |         let response = request.call()?;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // API returns 204 and an empty document if there are no statistics
 | 
					
						
							|  |  |  |         if response.status() == 204 {
 | 
					
						
							|  |  |  |             Ok(None)
 | 
					
						
							|  |  |  |         } else {
 | 
					
						
							|  |  |  |             response.into_json().map(Some).map_err(Error::ResponseJson)
 | 
					
						
							|  |  |  |         }
 | 
					
						
							| 
									
										
										
										
											2021-01-13 12:06:48 +00:00
										 |  |  |     }
 | 
					
						
							| 
									
										
										
										
											2021-01-13 10:52:58 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-14 02:03:04 +00:00
										 |  |  |     /// Endpoint: [`stats/user/{user_name}/recordings`](https://listenbrainz.readthedocs.io/en/production/dev/api/#get--1-stats-user-(user_name)-recordings)
 | 
					
						
							| 
									
										
										
										
											2021-01-13 12:46:29 +00:00
										 |  |  |     pub fn stats_user_recordings(
 | 
					
						
							| 
									
										
										
										
											2021-01-14 22:48:27 +00:00
										 |  |  |         &self,
 | 
					
						
							| 
									
										
										
										
											2021-01-13 12:46:29 +00:00
										 |  |  |         user_name: &str,
 | 
					
						
							|  |  |  |         count: Option<u64>,
 | 
					
						
							|  |  |  |         offset: Option<u64>,
 | 
					
						
							|  |  |  |         range: Option<&str>,
 | 
					
						
							| 
									
										
										
										
											2021-01-14 01:42:33 +00:00
										 |  |  |     ) -> Result<Option<StatsUserRecordingsResponse>, Error> {
 | 
					
						
							| 
									
										
										
										
											2021-01-13 16:28:12 +00:00
										 |  |  |         self.get_stats(
 | 
					
						
							|  |  |  |             Endpoint::StatsUserRecordings(user_name),
 | 
					
						
							|  |  |  |             count,
 | 
					
						
							|  |  |  |             offset,
 | 
					
						
							|  |  |  |             range,
 | 
					
						
							|  |  |  |         )
 | 
					
						
							| 
									
										
										
										
											2021-01-13 12:46:29 +00:00
										 |  |  |     }
 | 
					
						
							| 
									
										
										
										
											2021-01-13 10:52:58 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-14 02:03:04 +00:00
										 |  |  |     /// Endpoint: [`stats/user/{user_name}/artist-map`](https://listenbrainz.readthedocs.io/en/production/dev/api/#get--1-stats-user-(user_name)-artist-map)
 | 
					
						
							| 
									
										
										
										
											2021-01-13 13:02:23 +00:00
										 |  |  |     pub fn stats_user_artist_map(
 | 
					
						
							| 
									
										
										
										
											2021-01-14 22:48:27 +00:00
										 |  |  |         &self,
 | 
					
						
							| 
									
										
										
										
											2021-01-13 13:02:23 +00:00
										 |  |  |         user_name: &str,
 | 
					
						
							|  |  |  |         range: Option<&str>,
 | 
					
						
							|  |  |  |         force_recalculate: Option<bool>,
 | 
					
						
							|  |  |  |     ) -> Result<StatsUserArtistMapResponse, Error> {
 | 
					
						
							| 
									
										
										
										
											2021-01-13 13:28:59 +00:00
										 |  |  |         let endpoint = format!(
 | 
					
						
							|  |  |  |             "{}{}",
 | 
					
						
							|  |  |  |             API_ROOT_URL,
 | 
					
						
							|  |  |  |             Endpoint::StatsUserArtistMap(user_name)
 | 
					
						
							|  |  |  |         );
 | 
					
						
							| 
									
										
										
										
											2021-01-13 13:02:23 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         let mut request = self.agent.get(&endpoint);
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if let Some(range) = range {
 | 
					
						
							|  |  |  |             request = request.query("range", range);
 | 
					
						
							|  |  |  |         }
 | 
					
						
							|  |  |  |         if let Some(force_recalculate) = force_recalculate {
 | 
					
						
							|  |  |  |             request = request.query("force_recalculate", &force_recalculate.to_string());
 | 
					
						
							|  |  |  |         }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         request.call()?.into_json().map_err(Error::ResponseJson)
 | 
					
						
							|  |  |  |     }
 | 
					
						
							| 
									
										
										
										
											2021-01-13 10:52:58 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-14 02:03:04 +00:00
										 |  |  |     /// Endpoint: [`stats/user/{user_name}/releases`](https://listenbrainz.readthedocs.io/en/production/dev/api/#get--1-stats-user-(user_name)-releases)
 | 
					
						
							| 
									
										
										
										
											2021-01-13 13:09:34 +00:00
										 |  |  |     pub fn stats_user_releases(
 | 
					
						
							| 
									
										
										
										
											2021-01-14 22:48:27 +00:00
										 |  |  |         &self,
 | 
					
						
							| 
									
										
										
										
											2021-01-13 13:09:34 +00:00
										 |  |  |         user_name: &str,
 | 
					
						
							|  |  |  |         count: Option<u64>,
 | 
					
						
							|  |  |  |         offset: Option<u64>,
 | 
					
						
							|  |  |  |         range: Option<&str>,
 | 
					
						
							| 
									
										
										
										
											2021-01-14 01:42:33 +00:00
										 |  |  |     ) -> Result<Option<StatsUserReleasesResponse>, Error> {
 | 
					
						
							| 
									
										
										
										
											2021-01-13 16:28:12 +00:00
										 |  |  |         self.get_stats(Endpoint::StatsUserReleases(user_name), count, offset, range)
 | 
					
						
							| 
									
										
										
										
											2021-01-13 13:09:34 +00:00
										 |  |  |     }
 | 
					
						
							| 
									
										
										
										
											2021-01-13 10:52:58 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-14 02:03:04 +00:00
										 |  |  |     /// Endpoint: [`stats/user/{user_name}/artists`](https://listenbrainz.readthedocs.io/en/production/dev/api/#get--1-stats-user-(user_name)-artists)
 | 
					
						
							| 
									
										
										
										
											2021-01-13 13:15:32 +00:00
										 |  |  |     pub fn stats_user_artists(
 | 
					
						
							| 
									
										
										
										
											2021-01-14 22:48:27 +00:00
										 |  |  |         &self,
 | 
					
						
							| 
									
										
										
										
											2021-01-13 13:15:32 +00:00
										 |  |  |         user_name: &str,
 | 
					
						
							|  |  |  |         count: Option<u64>,
 | 
					
						
							|  |  |  |         offset: Option<u64>,
 | 
					
						
							|  |  |  |         range: Option<&str>,
 | 
					
						
							| 
									
										
										
										
											2021-01-14 01:42:33 +00:00
										 |  |  |     ) -> Result<Option<StatsUserArtistsResponse>, Error> {
 | 
					
						
							| 
									
										
										
										
											2021-01-13 16:28:12 +00:00
										 |  |  |         self.get_stats(Endpoint::StatsUserArtists(user_name), count, offset, range)
 | 
					
						
							| 
									
										
										
										
											2021-01-13 13:15:32 +00:00
										 |  |  |     }
 | 
					
						
							| 
									
										
										
										
											2021-01-13 10:52:58 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-14 02:03:04 +00:00
										 |  |  |     /// Endpoint: [`status/get-dump-info`](https://listenbrainz.readthedocs.io/en/production/dev/api/#get--1-status-get-dump-info)
 | 
					
						
							| 
									
										
										
										
											2021-01-13 10:52:58 +00:00
										 |  |  |     pub fn status_get_dump_info(
 | 
					
						
							| 
									
										
										
										
											2021-01-14 22:48:27 +00:00
										 |  |  |         &self,
 | 
					
						
							| 
									
										
										
										
											2021-01-13 10:52:58 +00:00
										 |  |  |         id: Option<i64>,
 | 
					
						
							|  |  |  |     ) -> Result<StatusGetDumpInfoResponse, Error> {
 | 
					
						
							|  |  |  |         let endpoint = format!("{}{}", API_ROOT_URL, Endpoint::StatusGetDumpInfo);
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         let mut request = self.agent.get(&endpoint);
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if let Some(id) = id {
 | 
					
						
							|  |  |  |             request = request.query("id", &id.to_string());
 | 
					
						
							|  |  |  |         }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         request.call()?.into_json().map_err(Error::ResponseJson)
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | }
 |