1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
use serde::Serialize;

use crate::{
    auth::{AuthFlow, Verifier},
    error::Result,
    model::{
        album::{AlbumGroup, SimplifiedAlbum},
        artist::{Artist, Artists},
        track::{Track, Tracks},
        Page,
    },
    query_list,
};

use super::{Builder, Endpoint, Limit};

impl Endpoint for ArtistAlbumsEndpoint {}
impl Endpoint for ArtistTopTracksEndpoint {}
impl Endpoint for ArtistEndpoint {}

#[derive(Clone, Debug, Default, Serialize)]
pub struct ArtistEndpoint {
    pub(crate) id: String,
}

impl<'a, F: AuthFlow, V: Verifier> Builder<'a, F, V, ArtistEndpoint> {
    pub fn albums(self) -> Builder<'a, F, V, ArtistAlbumsEndpoint> {
        Builder {
            spotify: self.spotify,
            endpoint: ArtistAlbumsEndpoint {
                id: self.endpoint.id,
                ..Default::default()
            },
        }
    }

    pub fn top_tracks(self) -> Builder<'a, F, V, ArtistTopTracksEndpoint> {
        Builder {
            spotify: self.spotify,
            endpoint: ArtistTopTracksEndpoint {
                id: self.endpoint.id,
                market: None,
            },
        }
    }

    #[doc = include_str!("../docs/send.md")]
    pub async fn get(self) -> Result<Artist> {
        self.spotify
            .get::<(), _>(format!("/artists/{}", self.endpoint.id), None)
            .await
    }

    #[doc = include_str!("../docs/send.md")]
    pub async fn get_related_artists(self) -> Result<Vec<Artist>> {
        self.spotify
            .get::<(), _>(
                format!("/artists/{}/related-artists", self.endpoint.id),
                None,
            )
            .await
            .map(|a: Artists| a.artists)
    }
}

#[derive(Clone, Debug, Default, Serialize)]
pub struct ArtistAlbumsEndpoint {
    #[serde(skip)]
    pub(crate) id: String,
    pub(crate) include_groups: Option<String>,
    pub(crate) market: Option<String>,
    pub(crate) limit: Option<Limit>,
    pub(crate) offset: Option<u32>,
}

impl<F: AuthFlow, V: Verifier> Builder<'_, F, V, ArtistAlbumsEndpoint> {
    /// Sets the album types to be returned. If not supplied all album types will be returned.
    pub fn include_groups(mut self, include_groups: &[AlbumGroup]) -> Self {
        self.endpoint.include_groups = Some(query_list(include_groups));
        self
    }

    #[doc = include_str!("../docs/market.md")]
    pub fn market(mut self, market: impl Into<String>) -> Self {
        self.endpoint.market = Some(market.into());
        self
    }

    #[doc = include_str!("../docs/limit.md")]
    pub fn limit(mut self, limit: u32) -> Self {
        self.endpoint.limit = Some(Limit::new(limit));
        self
    }

    #[doc = include_str!("../docs/offset.md")]
    pub fn offset(mut self, offset: u32) -> Self {
        self.endpoint.offset = Some(offset);
        self
    }

    #[doc = include_str!("../docs/send.md")]
    pub async fn get(self) -> Result<Page<SimplifiedAlbum>> {
        self.spotify
            .get(
                format!("/artists/{}/albums", self.endpoint.id),
                self.endpoint,
            )
            .await
    }
}

#[derive(Clone, Debug, Default, Serialize)]
pub struct ArtistTopTracksEndpoint {
    #[serde(skip)]
    pub(crate) id: String,
    pub(crate) market: Option<String>,
}

impl<F: AuthFlow, V: Verifier> Builder<'_, F, V, ArtistTopTracksEndpoint> {
    #[doc = include_str!("../docs/market.md")]
    pub fn market(mut self, market: impl Into<String>) -> Self {
        self.endpoint.market = Some(market.into());
        self
    }

    #[doc = include_str!("../docs/send.md")]
    pub async fn get(self) -> Result<Vec<Track>> {
        self.spotify
            .get(
                format!("/artists/{}/top-tracks", self.endpoint.id),
                self.endpoint,
            )
            .await
            .map(|t: Tracks| t.tracks)
    }
}