From a3265dceac063389841aff8a3f663e705fbeb99b Mon Sep 17 00:00:00 2001 From: Koen Bolhuis Date: Sun, 10 Jan 2021 19:22:50 +0100 Subject: [PATCH] Extract Endpoint to endpoint module --- src/endpoint.rs | 47 +++++++++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 49 ++----------------------------------------------- 2 files changed, 49 insertions(+), 47 deletions(-) create mode 100644 src/endpoint.rs diff --git a/src/endpoint.rs b/src/endpoint.rs new file mode 100644 index 0000000..b686547 --- /dev/null +++ b/src/endpoint.rs @@ -0,0 +1,47 @@ +use std::fmt; + +pub enum Endpoint<'a> { + SubmitListens, + ValidateToken, + DeleteListen, + UsersRecentListens(&'a [&'a str]), + UserListenCount(&'a str), + UserPlayingNow(&'a str), + UserListens(&'a str), + LatestImport, + // StatsSitewideArtists, + // StatsUserListeningActivity(&'a str), + // StatsUserDailyActivity(&'a str), + // StatsUserRecordings(&'a str), + // StatsUserArtistMap(&'a str), + // StatsUserReleases(&'a str), + // StatsUserArtists(&'a str), + StatusGetDumpInfo, +} + +impl<'a> fmt::Display for Endpoint<'a> { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + let s = match self { + Self::SubmitListens => "submit-listens", + Self::ValidateToken => "validate-token", + Self::DeleteListen => "delete-listen", + Self::UsersRecentListens(users) => { + // TODO: url-encode usernames with commas + return write!(f, "users/{}/recent-listens", users.join(",")); + } + Self::UserListenCount(user) => return write!(f, "user/{}/listen-count", user), + 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::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), + // Self::StatsUserArtistMap(user) => return write!(f, "stats/user/{}/artist-map", user), + // Self::StatsUserReleases(user) => return write!(f, "stats/user/{}/releases", user), + // Self::StatsUserArtists(user) => return write!(f, "stats/user/{}/artists", user), + Self::StatusGetDumpInfo => "status/get-dump-info", + }; + write!(f, "{}", s) + } +} diff --git a/src/lib.rs b/src/lib.rs index 91584f5..883902b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,6 +1,5 @@ //! API bindings for ListenBrainz. -use std::fmt; use std::io; use serde::de::DeserializeOwned; @@ -8,59 +7,15 @@ use serde::{Deserialize, Serialize}; use ureq::Agent; +mod endpoint; pub mod models; +use endpoint::Endpoint; use models::request::*; use models::response::*; const API_ROOT_URL: &str = "https://api.listenbrainz.org/1/"; -enum Endpoint<'a> { - SubmitListens, - ValidateToken, - DeleteListen, - UsersRecentListens(&'a [&'a str]), - UserListenCount(&'a str), - UserPlayingNow(&'a str), - UserListens(&'a str), - LatestImport, - // StatsSitewideArtists, - // StatsUserListeningActivity(&'a str), - // StatsUserDailyActivity(&'a str), - // StatsUserRecordings(&'a str), - // StatsUserArtistMap(&'a str), - // StatsUserReleases(&'a str), - // StatsUserArtists(&'a str), - StatusGetDumpInfo, -} - -impl<'a> fmt::Display for Endpoint<'a> { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - let s = match self { - Self::SubmitListens => "submit-listens", - Self::ValidateToken => "validate-token", - Self::DeleteListen => "delete-listen", - Self::UsersRecentListens(users) => { - // TODO: url-encode usernames with commas - return write!(f, "users/{}/recent-listens", users.join(",")); - } - Self::UserListenCount(user) => return write!(f, "user/{}/listen-count", user), - 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::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), - // Self::StatsUserArtistMap(user) => return write!(f, "stats/user/{}/artist-map", user), - // Self::StatsUserReleases(user) => return write!(f, "stats/user/{}/releases", user), - // Self::StatsUserArtists(user) => return write!(f, "stats/user/{}/artists", user), - Self::StatusGetDumpInfo => "status/get-dump-info", - }; - write!(f, "{}", s) - } -} - /// Represents errors that can occor while interacting with the API. #[derive(Debug, thiserror::Error)] pub enum Error {