From 67d9bf96327346454d21f9606e943234c2c261dc Mon Sep 17 00:00:00 2001 From: Koen Bolhuis Date: Thu, 14 Jan 2021 02:42:33 +0100 Subject: [PATCH] Fix handling of HTTP 204 on statistics endpoints The ListenBrainz API returns 204 and an empty document when there are no statistics, which can't be parsed as JSON. Therefore the return types of statistics endpoints now includes an Option to reflect this. --- src/client.rs | 41 +++++++++++++++++++++++++++++++---------- 1 file changed, 31 insertions(+), 10 deletions(-) diff --git a/src/client.rs b/src/client.rs index ee80bb7..02bb49c 100644 --- a/src/client.rs +++ b/src/client.rs @@ -42,7 +42,7 @@ impl Client { count: Option, offset: Option, range: Option<&str>, - ) -> Result { + ) -> Result, Error> { let endpoint = format!("{}{}", API_ROOT_URL, endpoint); let mut request = self.agent.get(&endpoint); @@ -57,7 +57,14 @@ impl Client { request = request.query("range", range); } - request.call()?.into_json().map_err(Error::ResponseJson) + 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) + } } fn post(&mut self, endpoint: Endpoint, token: &str, data: D) -> Result @@ -181,7 +188,7 @@ impl Client { count: Option, offset: Option, range: Option<&str>, - ) -> Result { + ) -> Result, Error> { self.get_stats(Endpoint::StatsSitewideArtists, count, offset, range) } @@ -190,7 +197,7 @@ impl Client { &mut self, user_name: &str, range: Option<&str>, - ) -> Result { + ) -> Result, Error> { let endpoint = format!( "{}{}", API_ROOT_URL, @@ -203,7 +210,14 @@ impl Client { request = request.query("range", range); } - request.call()?.into_json().map_err(Error::ResponseJson) + 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) + } } /// Endpoint: `stats/user/{user_name}/daily-activity` @@ -211,7 +225,7 @@ impl Client { &mut self, user_name: &str, range: Option<&str>, - ) -> Result { + ) -> Result, Error> { let endpoint = format!( "{}{}", API_ROOT_URL, @@ -224,7 +238,14 @@ impl Client { request = request.query("range", range); } - request.call()?.into_json().map_err(Error::ResponseJson) + 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) + } } /// Endpoint: `stats/user/{user_name}/recordings` @@ -234,7 +255,7 @@ impl Client { count: Option, offset: Option, range: Option<&str>, - ) -> Result { + ) -> Result, Error> { self.get_stats( Endpoint::StatsUserRecordings(user_name), count, @@ -275,7 +296,7 @@ impl Client { count: Option, offset: Option, range: Option<&str>, - ) -> Result { + ) -> Result, Error> { self.get_stats(Endpoint::StatsUserReleases(user_name), count, offset, range) } @@ -286,7 +307,7 @@ impl Client { count: Option, offset: Option, range: Option<&str>, - ) -> Result { + ) -> Result, Error> { self.get_stats(Endpoint::StatsUserArtists(user_name), count, offset, range) }