use crate::api::prelude::*;
#[derive(Debug, Clone)]
pub struct FollowPlaylist {
pub id: String,
pub public: bool,
}
impl FollowPlaylist {
pub fn new(id: impl Into<String>, public: bool) -> Self {
Self {
id: id.into(),
public,
}
}
}
impl<T: Into<String>> From<T> for FollowPlaylist {
fn from(id: T) -> Self {
Self {
id: id.into(),
public: true,
}
}
}
impl Endpoint for FollowPlaylist {
fn method(&self) -> Method {
Method::PUT
}
fn endpoint(&self) -> Cow<'static, str> {
format!("playlists/{}/followers", self.id).into()
}
fn body(&self) -> Result<Option<(&'static str, Vec<u8>)>, BodyError> {
JsonParams::into_body(&serde_json::json!({
"public": self.public,
}))
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{
api::{self, Query as _},
test::client::{ExpectedUrl, SingleTestClient},
};
#[test]
fn test_follow_playlist_endpoint() {
let endpoint = ExpectedUrl::builder()
.endpoint("playlists/3cEYpjA9oz9GiPac4AsH4n/followers")
.method(Method::PUT)
.content_type("application/json")
.body_str(r#"{"public":true}"#)
.build();
let client = SingleTestClient::new_raw(endpoint, "");
api::ignore(FollowPlaylist::from("3cEYpjA9oz9GiPac4AsH4n"))
.query(&client)
.unwrap();
}
#[test]
fn test_follow_playlist_endpoint_with_public_false() {
let endpoint = ExpectedUrl::builder()
.endpoint("playlists/3cEYpjA9oz9GiPac4AsH4n/followers")
.method(Method::PUT)
.content_type("application/json")
.body_str(r#"{"public":false}"#)
.build();
let client = SingleTestClient::new_raw(endpoint, "");
api::ignore(FollowPlaylist::new("3cEYpjA9oz9GiPac4AsH4n", false))
.query(&client)
.unwrap();
}
}