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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#[warn(missing_docs)]
use reqwest::StatusCode;
use serde::de::DeserializeOwned;
use crate::models::*;
use crate::Result;
const BASE_URL: &str = "https://api.deezer.com";
#[derive(Debug, Clone)]
pub struct DeezerClient {
client: reqwest::Client,
}
impl DeezerClient {
pub fn new() -> Self {
DeezerClient {
client: reqwest::Client::new(),
}
}
pub async fn album(&self, id: u64) -> Result<Option<Album>> {
self.get_entity(id).await
}
pub async fn artist(&self, id: u64) -> Result<Option<Artist>> {
self.get_entity(id).await
}
pub async fn comment(&self, id: u64) -> Result<Option<Comment>> {
self.get_entity(id).await
}
pub async fn editorial(&self, id: u64) -> Result<Option<Editorial>> {
self.get_entity(id).await
}
pub async fn editorials(&self) -> Result<Vec<Editorial>> {
self.get_all().await
}
pub async fn genre(&self, id: u64) -> Result<Option<Genre>> {
self.get_entity(id).await
}
pub async fn genres(&self) -> Result<Vec<Genre>> {
self.get_all().await
}
pub async fn playlist(&self, id: u64) -> Result<Option<Playlist>> {
self.get_entity(id).await
}
pub async fn radio(&self, id: u64) -> Result<Option<Radio>> {
self.get_entity(id).await
}
pub async fn radios(&self) -> Result<Vec<Radio>> {
self.get_all().await
}
pub async fn track(&self, id: u64) -> Result<Option<Track>> {
self.get_entity(id).await
}
pub async fn user(&self, id: u64) -> Result<Option<User>> {
self.get_entity(id).await
}
pub async fn api_info(&self) -> Result<Infos> {
let url = format!("{}/infos", BASE_URL);
self.get(&url).await
}
pub async fn charts(&self) -> Result<Chart> {
let url = format!("{}/chart", BASE_URL);
self.get(&url).await
}
pub async fn user_options(&self) -> Result<Options> {
let url = format!("{}/options", BASE_URL);
self.get(&url).await
}
pub(crate) async fn get_entity<T>(&self, id: u64) -> Result<Option<T>>
where
T: DeezerObject,
{
let url = T::get_api_url(id);
let url = format!("{}/{}", BASE_URL, url);
let res = self.client.get(&url).send().await?;
if res.status() == StatusCode::NOT_FOUND {
return Ok(None);
}
let body = res.error_for_status()?.json().await?;
Ok(Some(body))
}
pub(crate) async fn get_all<T>(&self) -> Result<Vec<T>>
where
T: DeezerEnumerable,
{
let url = T::get_all_api_url();
let url = format!("{}/{}", BASE_URL, url);
let res: DeezerArray<T> = self.get(&url).await?;
Ok(res.data)
}
async fn get<T: DeserializeOwned>(&self, url: &str) -> Result<T> {
let res = self
.client
.get(url)
.send()
.await?
.error_for_status()?
.json()
.await?;
Ok(res)
}
}