use serde_json::json;
use super::{PostMethod, PostQuery, Query};
use crate::{
auth::AuthToken,
common::{ArtistChannelID, BrowseParams, YoutubeID},
parse::{ArtistParams, GetArtistAlbumsAlbum},
};
use std::borrow::Cow;
#[derive(Debug, Clone)]
pub struct GetArtistQuery<'a> {
channel_id: ArtistChannelID<'a>,
}
#[derive(Debug, Clone)]
pub struct GetArtistAlbumsQuery<'a> {
channel_id: ArtistChannelID<'a>,
params: BrowseParams<'a>,
}
impl<'a> GetArtistQuery<'a> {
pub fn new(channel_id: impl Into<ArtistChannelID<'a>>) -> GetArtistQuery<'a> {
GetArtistQuery {
channel_id: channel_id.into(),
}
}
}
impl<'a> GetArtistAlbumsQuery<'a> {
pub fn new(
channel_id: ArtistChannelID<'a>,
params: BrowseParams<'a>,
) -> GetArtistAlbumsQuery<'a> {
GetArtistAlbumsQuery { channel_id, params }
}
}
impl<'a, T: Into<ArtistChannelID<'a>>> From<T> for GetArtistQuery<'a> {
fn from(channel_id: T) -> Self {
GetArtistQuery::new(channel_id.into())
}
}
impl<'a, A: AuthToken> Query<A> for GetArtistQuery<'a> {
type Output = ArtistParams;
type Method = PostMethod;
}
impl<'a> PostQuery for GetArtistQuery<'a> {
fn header(&self) -> serde_json::Map<String, serde_json::Value> {
let value = self.channel_id.get_raw().replacen("MPLA", "", 1);
let serde_json::Value::Object(map) = json!({
"browseId" : value,
}) else {
unreachable!()
};
map
}
fn path(&self) -> &str {
"browse"
}
fn params(&self) -> Option<Cow<str>> {
None
}
}
impl<'a, A: AuthToken> Query<A> for GetArtistAlbumsQuery<'a> {
type Output = Vec<GetArtistAlbumsAlbum>;
type Method = PostMethod;
}
impl<'a> PostQuery for GetArtistAlbumsQuery<'a> {
fn header(&self) -> serde_json::Map<String, serde_json::Value> {
let value = self.channel_id.get_raw().replacen("MPLA", "", 1);
let serde_json::Value::Object(map) = json!({
"browseId" : value,
}) else {
unreachable!()
};
map
}
fn path(&self) -> &str {
"browse"
}
fn params(&self) -> Option<Cow<str>> {
Some(self.params.get_raw().into())
}
}