Extract Endpoint to endpoint module
This commit is contained in:
parent
2d23b4cccf
commit
a3265dceac
|
@ -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)
|
||||
}
|
||||
}
|
49
src/lib.rs
49
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 {
|
||||
|
|
Loading…
Reference in New Issue