From 16b7d643c9382cd93d00fa40c72422131add964f Mon Sep 17 00:00:00 2001 From: Koen Bolhuis Date: Wed, 13 Jan 2021 17:28:12 +0100 Subject: [PATCH] Extract stats code to get_stats function --- src/client.rs | 99 ++++++++++++++++++--------------------------------- 1 file changed, 34 insertions(+), 65 deletions(-) diff --git a/src/client.rs b/src/client.rs index 2ce7792..ee80bb7 100644 --- a/src/client.rs +++ b/src/client.rs @@ -36,6 +36,30 @@ impl Client { .map_err(Error::ResponseJson) } + fn get_stats( + &mut self, + endpoint: Endpoint, + count: Option, + offset: Option, + range: Option<&str>, + ) -> Result { + 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); + } + + request.call()?.into_json().map_err(Error::ResponseJson) + } + fn post(&mut self, endpoint: Endpoint, token: &str, data: D) -> Result where D: Serialize, @@ -107,7 +131,7 @@ impl Client { user_name: &str, min_ts: Option, max_ts: Option, - count: Option, + count: Option, time_range: Option, ) -> Result { let endpoint = format!("{}{}", API_ROOT_URL, Endpoint::UserListens(user_name)); @@ -158,21 +182,7 @@ impl Client { offset: Option, range: Option<&str>, ) -> Result { - let endpoint = format!("{}{}", API_ROOT_URL, Endpoint::StatsSitewideArtists); - - 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); - } - - request.call()?.into_json().map_err(Error::ResponseJson) + self.get_stats(Endpoint::StatsSitewideArtists, count, offset, range) } /// Endpoint: `stats/user/{user_name}/listening-activity` @@ -225,25 +235,12 @@ impl Client { offset: Option, range: Option<&str>, ) -> Result { - let endpoint = format!( - "{}{}", - API_ROOT_URL, - Endpoint::StatsUserRecordings(user_name) - ); - - 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); - } - - request.call()?.into_json().map_err(Error::ResponseJson) + self.get_stats( + Endpoint::StatsUserRecordings(user_name), + count, + offset, + range, + ) } /// Endpoint: `stats/user/{user_name}/artist-map` @@ -279,21 +276,7 @@ impl Client { offset: Option, range: Option<&str>, ) -> Result { - let endpoint = format!("{}{}", API_ROOT_URL, Endpoint::StatsUserReleases(user_name)); - - 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); - } - - request.call()?.into_json().map_err(Error::ResponseJson) + self.get_stats(Endpoint::StatsUserReleases(user_name), count, offset, range) } /// Endpoint: `stats/user/{user_name}/artists` @@ -304,21 +287,7 @@ impl Client { offset: Option, range: Option<&str>, ) -> Result { - let endpoint = format!("{}{}", API_ROOT_URL, Endpoint::StatsUserArtists(user_name)); - - 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); - } - - request.call()?.into_json().map_err(Error::ResponseJson) + self.get_stats(Endpoint::StatsUserArtists(user_name), count, offset, range) } /// Endpoint: `status/get-dump-info`