ytmapi_rs/query/
artist.rs

1use super::{PostMethod, PostQuery, Query};
2use crate::{
3    auth::AuthToken,
4    common::{ArtistChannelID, BrowseParams, YoutubeID},
5    parse::{ArtistParams, GetArtistAlbumsAlbum},
6};
7use serde_json::json;
8
9#[derive(Debug, Clone)]
10pub struct GetArtistQuery<'a> {
11    channel_id: ArtistChannelID<'a>,
12}
13#[derive(Debug, Clone)]
14pub struct GetArtistAlbumsQuery<'a> {
15    channel_id: ArtistChannelID<'a>,
16    params: BrowseParams<'a>,
17}
18impl<'a> GetArtistQuery<'a> {
19    pub fn new(channel_id: impl Into<ArtistChannelID<'a>>) -> GetArtistQuery<'a> {
20        GetArtistQuery {
21            channel_id: channel_id.into(),
22        }
23    }
24}
25impl<'a> GetArtistAlbumsQuery<'a> {
26    pub fn new(
27        channel_id: ArtistChannelID<'a>,
28        params: BrowseParams<'a>,
29    ) -> GetArtistAlbumsQuery<'a> {
30        GetArtistAlbumsQuery { channel_id, params }
31    }
32}
33
34impl<'a, T: Into<ArtistChannelID<'a>>> From<T> for GetArtistQuery<'a> {
35    fn from(channel_id: T) -> Self {
36        GetArtistQuery::new(channel_id.into())
37    }
38}
39
40impl<A: AuthToken> Query<A> for GetArtistQuery<'_> {
41    type Output = ArtistParams;
42    type Method = PostMethod;
43}
44impl PostQuery for GetArtistQuery<'_> {
45    fn header(&self) -> serde_json::Map<String, serde_json::Value> {
46        // XXX: could do in new to avoid process every time called
47        // or even better, could do this first time called, and store state so not
48        // required after that.
49        let value = self.channel_id.get_raw().replacen("MPLA", "", 1);
50        let serde_json::Value::Object(map) = json!({
51            "browseId" : value,
52        }) else {
53            unreachable!()
54        };
55        map
56    }
57    fn path(&self) -> &str {
58        "browse"
59    }
60    fn params(&self) -> std::vec::Vec<(&str, std::borrow::Cow<'_, str>)> {
61        vec![]
62    }
63}
64// TODO: Check if the MPLA strip is correct for both of these.
65impl<A: AuthToken> Query<A> for GetArtistAlbumsQuery<'_> {
66    type Output = Vec<GetArtistAlbumsAlbum>;
67    type Method = PostMethod;
68}
69impl PostQuery for GetArtistAlbumsQuery<'_> {
70    fn header(&self) -> serde_json::Map<String, serde_json::Value> {
71        // XXX: should do in new
72        // XXX: Think I could remove allocation here
73        let value = self.channel_id.get_raw().replacen("MPLA", "", 1);
74        FromIterator::from_iter([
75            ("browseId".to_string(), json!(value)),
76            ("params".to_string(), json!(self.params)),
77        ])
78    }
79    fn path(&self) -> &str {
80        "browse"
81    }
82    fn params(&self) -> std::vec::Vec<(&str, std::borrow::Cow<'_, str>)> {
83        vec![]
84    }
85}