use crate::api::prelude::*;
use serde_json::json;
#[derive(Debug, Clone)]
pub struct CreatePlaylist {
pub id: String,
pub name: String,
pub public: Option<bool>,
pub collaborative: Option<bool>,
pub description: Option<String>,
}
impl Endpoint for CreatePlaylist {
fn method(&self) -> Method {
Method::POST
}
fn endpoint(&self) -> Cow<'static, str> {
format!("users/{}/playlists", self.id).into()
}
fn body(&self) -> Result<Option<(&'static str, Vec<u8>)>, BodyError> {
let mut body = json!({
"name": self.name,
});
if let Some(public) = self.public {
body["public"] = json!(public);
}
if let Some(collaborative) = self.collaborative {
body["collaborative"] = json!(collaborative);
}
if let Some(description) = &self.description {
body["description"] = json!(description);
}
JsonParams::into_body(&body)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{
api::{self, Query as _},
test::client::{ExpectedUrl, SingleTestClient},
};
#[test]
fn test_create_playlist_endpoint() {
let endpoint = ExpectedUrl::builder()
.method(Method::POST)
.content_type("application/json")
.endpoint("users/smedjan/playlists")
.body_str(r#"{"description":"New playlist description","name":"New Playlist","public":false}"#)
.build();
let client = SingleTestClient::new_raw(endpoint, "");
let endpoint = CreatePlaylist {
id: "smedjan".to_owned(),
name: "New Playlist".to_owned(),
description: Some("New playlist description".to_owned()),
public: Some(false),
collaborative: None,
};
api::ignore(endpoint).query(&client).unwrap();
}
}