From 22b04846b2047510f0c97792956394960553da49 Mon Sep 17 00:00:00 2001 From: Koen Bolhuis Date: Sun, 10 Jan 2021 21:42:40 +0100 Subject: [PATCH] Implement StatsSitewideArtists endpoint --- src/endpoint.rs | 4 ++-- src/lib.rs | 43 ++++++++++++++++++++++++++++++++++-------- src/models/response.rs | 38 +++++++++++++++++++++++++++++++++++++ 3 files changed, 75 insertions(+), 10 deletions(-) diff --git a/src/endpoint.rs b/src/endpoint.rs index b686547..8d825c4 100644 --- a/src/endpoint.rs +++ b/src/endpoint.rs @@ -9,7 +9,7 @@ pub enum Endpoint<'a> { UserPlayingNow(&'a str), UserListens(&'a str), LatestImport, - // StatsSitewideArtists, + StatsSitewideArtists, // StatsUserListeningActivity(&'a str), // StatsUserDailyActivity(&'a str), // StatsUserRecordings(&'a str), @@ -33,7 +33,7 @@ impl<'a> fmt::Display for Endpoint<'a> { Self::UserPlayingNow(user) => return write!(f, "user/{}/playing-now", user), Self::UserListens(user) => return write!(f, "user/{}/listens", user), Self::LatestImport => "latest-import", - // Self::StatsSitewideArtists => "stats/sitewide/artists", + Self::StatsSitewideArtists => "stats/sitewide/artists", // Self::StatsUserListeningActivity(user) => return write!(f, "stats/user/{}/listening-activity", user), // Self::StatsUserDailyActivity(user) => return write!(f, "stats/user/{}/daily-activity", user), // Self::StatsUserRecordings(user) => return write!(f, "stats/user/{}/recordings", user), diff --git a/src/lib.rs b/src/lib.rs index bc758dc..6122f8f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -102,7 +102,6 @@ impl Client { self.get(Endpoint::UserListenCount(user_name)) } - // UserPlayingNow(&'a str), /// Endpoint: `user/{user_name}/playing-now` pub fn user_playing_now(&mut self, user_name: &str) -> Result { self.get(Endpoint::UserPlayingNow(user_name)) @@ -157,13 +156,41 @@ impl Client { self.post(Endpoint::LatestImport, token, data) } - // StatsSitewideArtists, - // StatsUserListeningActivity(&'a str), - // StatsUserDailyActivity(&'a str), - // StatsUserRecordings(&'a str), - // StatsUserArtistMap(&'a str), - // StatsUserReleases(&'a str), - // StatsUserArtists(&'a str), + /// Endpoint: `stats/sitewide/artists` + pub fn stats_sitewide_artists( + &mut self, + count: Option, + 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) + } + + // /// Endpoint: `stats/user/{user_name}/listening-activity` + + // /// Endpoint: `stats/user/{user_name}/daily-activity` + + // /// Endpoint: `stats/user/{user_name}/recordings` + + // /// Endpoint: `stats/user/{user_name}/artist-map` + + // /// Endpoint: `stats/user/{user_name}/releases` + + // /// Endpoint: `stats/user/{user_name}/artists` /// Endpoint: `status/get-dump-info` pub fn status_get_dump_info( diff --git a/src/models/response.rs b/src/models/response.rs index b515e68..b4ec79d 100644 --- a/src/models/response.rs +++ b/src/models/response.rs @@ -176,6 +176,44 @@ pub struct UpdateLatestImportResponse { pub status: String, } +// --------- stats/sitewide/artists + +/// Response type for [`Client::stats_sitewide_artists`](crate::Client::stats_sitewide_artists). +#[derive(Debug, Deserialize)] +pub struct StatsSitewideArtistsResponse { + pub payload: StatsSitewideArtistsPayload, +} + +/// Type of the [`StatsSitewideArtistsResponse::payload`] field. +#[derive(Debug, Deserialize)] +pub struct StatsSitewideArtistsPayload { + pub time_ranges: Vec, + pub offset: u64, + pub count: u64, + pub range: String, + pub last_updated: i64, + pub from_ts: i64, + pub to_ts: i64, +} + +/// Type of the [`StatsSitewideArtistsPayload::time_ranges`] field. +#[derive(Debug, Deserialize)] +pub struct StatsSitewideArtistsTimeRange { + pub time_range: String, + pub artists: Vec, + pub from_ts: i64, + pub to_ts: i64, +} + +/// Type of the [`StatsSitewideArtistsTimeRange::artists`] field. +#[derive(Debug, Deserialize)] +pub struct StatsSitewideArtistsArtist { + pub artist_mbids: Option>, + pub artist_msid: String, + pub artist_name: String, + pub listen_count: u64, +} + // --------- status/get-dump-info /// Response type for [`Client::status_get_dump_info`](crate::Client::status_get_dump_info).