Extract Endpoint to endpoint module

This commit is contained in:
Koen Bolhuis 2021-01-10 19:22:50 +01:00
parent 2d23b4cccf
commit a3265dceac
2 changed files with 49 additions and 47 deletions

47
src/endpoint.rs Normal file
View File

@ -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)
}
}

View File

@ -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 {