Skip to content

Commit

Permalink
Merge pull request #13 from InputUsername/fix/responses
Browse files Browse the repository at this point in the history
Update response schemas to match the latest API version
  • Loading branch information
InputUsername authored Jan 26, 2023
2 parents daad959 + 18f2a4e commit 5de1c2b
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 85 deletions.
31 changes: 30 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,38 @@
# Changelog

## v0.6.0 (unreleased)

- Updated response schemas to match the ListenBrainz API ([#12], [@cellularnetwork]):
- **These are breaking changes.**
- `user/{user_name}/playing-now`:
- Added `UserPlayingNowPayload::playing_now`;
- Added `UserPlayingNowListen::playing_now`;
- Removed `UserPlayingNowListen::{user_name, inserted_at, recording_msid}.
- `user/{user_name}/listens`:
- Changed the type of `UserListensListen::inserted_at` from `String` to `i64`.
- `stats/sitewide/artists`:
- Removed `StatsSitewideArtistsPayload::time_ranges`;
- Added `StatsSitewideArtistsPayload::artists`;
- Removed `StatsSitewideArtistsTimeRange`;
- Removed `StatsSitewideArtistsArtist::artist_msid`.
- `stats/user/{user_name}/artist-map`:
- Changed the return type of `Client::stats_user_artist_map` from `Result<StatsUserArtistMapResponse, Error>`
to `Result<Option<StatsUserArtistMapResponse>, Error>`.
- `users/{user_list}/recent-listens`:
- Removed `Client::users_recent_listens`;
- Removed `UsersRecentListensResponse`;
- Removed `UsersRecentListensPayload`;
- Removed `UsersRecentListensListen`;
- Removed `UsersRecentListensTrackMetadata`;
- Removed `examples/users_recent_listens.rs`.

[#12]: https://github.com/InputUsername/listenbrainz-rs/pull/12
[@cellularnetwork]: https://github.com/cellularnetwork

## v0.5.0 (2022-12-05)

- Made the `release` parameter of `ListenBrainz` methods optional ([#11], [@mgziminsky]).
- This is a breaking change.
- **This is a breaking change.**

[#11]: https://github.com/InputUsername/listenbrainz-rs/pull/11
[@mgziminsky]: https://github.com/mgziminsky
Expand Down
11 changes: 0 additions & 11 deletions examples/users_recent_listens.rs

This file was deleted.

17 changes: 7 additions & 10 deletions src/raw/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,14 +130,6 @@ impl Client {
self.post(Endpoint::DeleteListen, token, data)
}

/// Endpoint: [`users/{user_list}/recent-listens`](https://listenbrainz.readthedocs.io/en/production/dev/api/#get--1-users-(user_list)-recent-listens)
pub fn users_recent_listens(
&self,
user_list: &[&str],
) -> Result<UsersRecentListensResponse, Error> {
self.get(Endpoint::UsersRecentListens(user_list))
}

/// Endpoint: [`user/{user_name}/listen-count`](https://listenbrainz.readthedocs.io/en/production/dev/api/#get--1-user-(user_name)-listen-count)
pub fn user_listen_count(&self, user_name: &str) -> Result<UserListenCountResponse, Error> {
self.get(Endpoint::UserListenCount(user_name))
Expand Down Expand Up @@ -287,7 +279,7 @@ impl Client {
user_name: &str,
range: Option<&str>,
force_recalculate: Option<bool>,
) -> Result<StatsUserArtistMapResponse, Error> {
) -> Result<Option<StatsUserArtistMapResponse>, Error> {
let endpoint = format!(
"{}{}",
API_ROOT_URL,
Expand All @@ -305,7 +297,12 @@ impl Client {

let response = request.send()?;

ResponseType::from_response(response)
// API returns 204 and an empty document if there are no statistics
if response.status() == 204 {
Ok(None)
} else {
ResponseType::from_response(response).map(Some)
}
}

/// Endpoint: [`stats/user/{user_name}/releases`](https://listenbrainz.readthedocs.io/en/production/dev/api/#get--1-stats-user-(user_name)-releases)
Expand Down
11 changes: 0 additions & 11 deletions src/raw/endpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ pub enum Endpoint<'a> {
SubmitListens,
ValidateToken,
DeleteListen,
UsersRecentListens(&'a [&'a str]),
UserListenCount(&'a str),
UserPlayingNow(&'a str),
UserListens(&'a str),
Expand All @@ -25,16 +24,6 @@ impl<'a> fmt::Display for Endpoint<'a> {
Self::SubmitListens => "submit-listens",
Self::ValidateToken => "validate-token",
Self::DeleteListen => "delete-listen",
Self::UsersRecentListens(users) => {
let users = users.iter().fold(String::new(), |mut result, user| {
result.reserve(user.len() + 1);
result.push_str(&user.replace(',', "%2C"));
result.push(',');
result
});
println!("{:?}", users);
return write!(f, "users/{}/recent-listens", users);
}
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),
Expand Down
56 changes: 4 additions & 52 deletions src/raw/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,43 +141,6 @@ response_type! {
}
}

// --------- users/{user_list}/recent-listens

response_type! {
/// Response type for [`Client::users_recent_listens`](super::Client::users_recent_listens).
#[derive(Debug, Deserialize)]
pub struct UsersRecentListensResponse {
pub payload: UsersRecentListensPayload,
}
}

/// Type of the [`UsersRecentListensResponse::payload`] field.
#[derive(Debug, Deserialize)]
pub struct UsersRecentListensPayload {
pub count: u64,
pub listens: Vec<UsersRecentListensListen>,
pub user_list: String,
}

/// Type of the [`UsersRecentListensPayload::listens`] field.
#[derive(Debug, Deserialize)]
pub struct UsersRecentListensListen {
pub user_name: String,
pub inserted_at: String,
pub listened_at: i64,
pub recording_msid: String,
pub track_metadata: UsersRecentListensTrackMetadata,
}

/// Type of the [`UsersRecentListensListen::track_metadata`] field.
#[derive(Debug, Deserialize)]
pub struct UsersRecentListensTrackMetadata {
pub artist_name: String,
pub track_name: String,
pub release_name: Option<String>,
pub additional_info: HashMap<String, serde_json::Value>,
}

// --------- user/{user_name}/listen-count

response_type! {
Expand Down Expand Up @@ -210,15 +173,14 @@ pub struct UserPlayingNowPayload {
pub count: u8,
pub user_id: String,
pub listens: Vec<UserPlayingNowListen>,
pub playing_now: bool,
}

/// Type of the [`UserPlayingNowPayload::listens`] field.
#[derive(Debug, Deserialize)]
pub struct UserPlayingNowListen {
pub user_name: String,
pub inserted_at: String,
pub recording_msid: String,
pub track_metadata: UserPlayingNowTrackMetadata,
pub playing_now: bool,
}

/// Type of the [`UserPlayingNowListen::track_metadata`] field.
Expand Down Expand Up @@ -253,7 +215,7 @@ pub struct UserListensPayload {
#[derive(Debug, Deserialize)]
pub struct UserListensListen {
pub user_name: String,
pub inserted_at: String,
pub inserted_at: i64,
pub listened_at: i64,
pub recording_msid: String,
pub track_metadata: UserListensTrackMetadata,
Expand Down Expand Up @@ -302,7 +264,7 @@ response_type! {
/// Type of the [`StatsSitewideArtistsResponse::payload`] field.
#[derive(Debug, Deserialize)]
pub struct StatsSitewideArtistsPayload {
pub time_ranges: Vec<StatsSitewideArtistsTimeRange>,
pub artists: Vec<StatsSitewideArtistsArtist>,
pub offset: u64,
pub count: u64,
pub range: String,
Expand All @@ -311,20 +273,10 @@ pub struct StatsSitewideArtistsPayload {
pub to_ts: i64,
}

/// Type of the [`StatsSitewideArtistsPayload::time_ranges`] field.
#[derive(Debug, Deserialize)]
pub struct StatsSitewideArtistsTimeRange {
pub time_range: String,
pub artists: Vec<StatsSitewideArtistsArtist>,
pub from_ts: i64,
pub to_ts: i64,
}

/// Type of the [`StatsSitewideArtistsTimeRange::artists`] field.
#[derive(Debug, Deserialize)]
pub struct StatsSitewideArtistsArtist {
pub artist_mbids: Option<Vec<String>>,
pub artist_msid: String,
pub artist_name: String,
pub listen_count: u64,
}
Expand Down

0 comments on commit 5de1c2b

Please sign in to comment.