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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/// see API docs at https://audible.readthedocs.io/en/latest/misc/external_api.html
use json_value_merge::Merge;
use serde_json::{json, Value};
use super::Client;
use crate::Result;
impl Client {
/// GET /1.0/library
///
/// The audible library of current user
///
/// Query Parameters:
/// - `num_results` (integer) – (max: 1000)
/// - `page` (integer) – page
/// - `purchased_after` (string) – [RFC3339](https://tools.ietf.org/html/rfc3339) (e.g. 2000-01-01T00:00:00Z)
/// - `title` (string) – a title
/// - `author` (string) – an author
/// - `response_groups` (string) – [contributors, customer_rights, media, price, product_attrs, product_desc,
/// product_details, product_extended_attrs, product_plan_details, product_plans, rating, sample, sku, series,
/// reviews, ws4v, origin, relationships, review_attrs, categories, badge_types, category_ladders, claim_code_url,
/// in_wishlist, is_archived, is_downloaded, is_finished, is_playable, is_removable, is_returnable, is_visible,
/// listening_status, order_details, origin_asin, pdf_url, percent_complete, periodicals, provided_review]
/// - `image_sizes` (string) – 1215,408,360,882,315,570,252,558,900,500
/// - `sort_by` (string) – [-Author, -Length, -Narrator, -PurchaseDate, -Title, Author, Length, Narrator, PurchaseDate, Title]
/// - `status` (string) – [Active, Revoked] (‘Active’ is the default, ‘Revoked’ returns audiobooks the user has returned for a refund.)
/// - `parent_asin` (string) – asin
/// - `include_pending` (string) – [true, false]
/// - `marketplace` (string) – [e.g. AN7V1F1VY261K]
/// - `state_token` (string)
pub async fn get_library(&self, params: Option<Value>) -> Result<Value> {
let url = format!("{}/1.0/library", self.base_url);
let mut query = json! {{
"num_results": 1000,
"response_groups": "product_desc,product_attrs",
"sort_by": "-PurchaseDate"
}};
if let Some(params) = params {
query.merge(¶ms);
}
let req = self.client.get(url).query(&query).build()?;
let res = self.send_request(req).await?;
let json: Value = res.json().await?;
Ok(json)
}
/// GET /1.0/library/(string:asin)
///
/// Parameters:
/// - `asin` (string) – The ASIN of the book
///
/// Query Parameters:
/// - `response_groups` (string) – [contributors, media, price, product_attrs, product_desc, product_details,
/// product_extended_attrs, product_plan_details, product_plans, rating, sample, sku, series, reviews, ws4v,
/// origin, relationships, review_attrs, categories, badge_types, category_ladders, claim_code_url,
/// is_downloaded, is_finished, is_returnable, origin_asin, pdf_url, percent_complete, periodicals, provided_review]
pub async fn get_library_item_by_asin(
&self,
asin: &str,
params: Option<Value>,
) -> Result<Value> {
let url = format!("{}/1.0/library/{}", self.base_url, asin);
let mut req = self.client.get(url);
if let Some(params) = params {
req = req.query(¶ms);
}
let req = req.build()?;
let res = self.send_request(req).await?;
let json: Value = res.json().await?;
Ok(json)
}
/// POST /1.0/library/item
///
/// Request JSON Object:
/// - `asin` (string) – The ASIN of the book
pub async fn post_library_item_by_asin(
&self,
asin: &str,
params: Option<Value>,
) -> Result<Value> {
let url = format!("{}/1.0/library/item", self.base_url);
let mut json = json! {{
"asin": asin,
}};
if let Some(params) = params {
json.merge(¶ms);
}
let req = self.client.post(url).json(&json).build()?;
let res = self.send_request(req).await?;
let json: Value = res.json().await?;
Ok(json)
}
/// PUT /1.0/library/item
/// Add an (AYCL) item to the library
///
/// Request JSON Object:
/// - `asin` (string) – The ASIN of the book
pub async fn put_library_item_by_asin(
&self,
asin: &str,
params: Option<Value>,
) -> Result<Value> {
let url = format!("{}/1.0/library/item", self.base_url);
let mut json = json! {{
"asin": asin,
}};
if let Some(params) = params {
json.merge(¶ms);
}
let req = self.client.put(url).query(&json).build()?;
let res = self.send_request(req).await?;
let json: Value = res.json().await?;
Ok(json)
}
/// POST /1.0/library/item/(param1)/(param2)
///
/// Parameters:
/// - `param1` (string)
/// - `param2` (string)
///
/// Request JSON Object:
/// - `unknown` (object) – The structure of the JSON object is not specified
pub async fn post_library_item_with_two_params(
&self,
param1: &str,
param2: &str,
params: Option<Value>,
) -> Result<Value> {
let url = format!("{}/1.0/library/item/{}/{}", self.base_url, param1, param2);
let mut req = self.client.post(url);
if let Some(params) = params {
req = req.json(¶ms);
}
let req = req.build()?;
let res = self.send_request(req).await?;
let json: Value = res.json().await?;
Ok(json)
}
/// POST /1.0/library/collections/(param1)/channels/(param2)
///
/// Parameters:
/// - `param1` (string)
/// - `param2` (string)
///
/// Request JSON Object:
/// - `customer_id` (string)
/// - `marketplace` (string)
pub async fn post_library_collection_channels(
&self,
param1: &str,
param2: &str,
params: Option<Value>,
) -> Result<Value> {
let url = format!(
"{}/1.0/library/collections/{}/channels/{}",
self.base_url, param1, param2
);
let mut req = self.client.post(url);
if let Some(params) = params {
req = req.json(¶ms);
}
let req = req.build()?;
let res = self.send_request(req).await?;
let json: Value = res.json().await?;
Ok(json)
}
/// POST /1.0/library/collections/(param1)/products/(param2)
///
/// Parameters:
/// - `param1` (string)
/// - `param2` (string)
///
/// Request JSON Object:
/// - `channel_id` (string)
pub async fn post_library_collection_products(
&self,
param1: &str,
param2: &str,
params: Option<Value>,
) -> Result<Value> {
let url = format!(
"{}/1.0/library/collections/{}/products/{}",
self.base_url, param1, param2
);
let mut req = self.client.post(url);
if let Some(params) = params {
req = req.json(¶ms);
}
let req = req.build()?;
let res = self.send_request(req).await?;
let json: Value = res.json().await?;
Ok(json)
}
/// GET /1.0/library/collections
///
/// Query Parameters:
/// - `customer_id` (string)
/// - `marketplace` (string)
pub async fn get_library_collections(&self, params: Option<Value>) -> Result<Value> {
let url = format!("{}/1.0/library/collections", self.base_url);
let mut req = self.client.get(url);
if let Some(params) = params {
req = req.json(¶ms);
}
let req = req.build()?;
let res = self.send_request(req).await?;
let json: Value = res.json().await?;
Ok(json)
}
/// POST /1.0/library/collections
///
/// Request JSON Object:
/// - `collection_type` (string)
pub async fn post_library_collections(&self, params: Option<Value>) -> Result<Value> {
let url = format!("{}/1.0/library/collections", self.base_url);
let mut req = self.client.post(url);
if let Some(params) = params {
req = req.json(¶ms);
}
let req = req.build()?;
let res = self.send_request(req).await?;
let json: Value = res.json().await?;
Ok(json)
}
/// GET /1.0/library/collections/(param1)
///
/// Parameters:
/// - `param1` (string)
///
/// Query Parameters:
/// - `customer_id` (string)
/// - `marketplace` (string)
/// - `page_size` (integer)
/// - `continuation_token` (string)
pub async fn get_library_collections_with_param(
&self,
param1: &str,
params: Option<Value>,
) -> Result<Value> {
let url = format!("{}/1.0/library/collections/{}", self.base_url, param1);
let mut req = self.client.get(url);
if let Some(params) = params {
req = req.query(¶ms);
}
let req = req.build()?;
let res = self.send_request(req).await?;
let json: Value = res.json().await?;
Ok(json)
}
/// GET /1.0/library/collections/(param1)/products
///
/// Parameters:
/// - `param1` (string)
///
/// Query Parameters:
/// - `customer_id` (string)
/// - `marketplace` (string)
/// - `page_size` (integer)
/// - `continuation_token` (string)
/// - `image_sizes` (string)
pub async fn get_library_collections_with_param_products(
&self,
param1: &str,
params: Option<Value>,
) -> Result<Value> {
let url = format!(
"{}/1.0/library/collections/{}/products",
self.base_url, param1
);
let mut req = self.client.get(url);
if let Some(params) = params {
req = req.query(¶ms);
}
let req = req.build()?;
let res = self.send_request(req).await?;
let json: Value = res.json().await?;
Ok(json)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::auth::Auth;
#[tokio::test]
async fn test_get_library() {
let auth = Auth::default("us").await.unwrap();
let client = Client::new(auth).unwrap();
client.get_library(None).await.unwrap();
}
}