animeschedule 0.1.1

A complete async/sync typed AnimeSchedule api
Documentation
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
use std::ops::Deref;

use chrono::prelude::*;
use const_format::formatcp;
use reqwest::multipart;

use crate::{
    errors::ApiError,
    objects::{Action, AutoScores, ListAnime, ListAnimePut, ListStatus, UserListAnime},
    rate_limit::RateLimit,
    utils::IsJson as _,
    AnimeScheduleClient, API_URL, RUNTIME,
};

const API_ANIMELISTS_USERID_ROUTE: &str = formatcp!("{API_URL}/animelists/{{userId}}/{{route}}");
const API_ANIMELISTS_ROUTE: &str = formatcp!("{API_URL}/animelists/oauth/{{route}}");
const API_ANIMELISTS_USERID: &str = formatcp!("{API_URL}/animelists/{{userId}}");
const API_ANIMELISTS: &str = formatcp!("{API_URL}/animelists/oauth");

pub struct AnimeListsApi {
    client: AnimeScheduleClient,
}

impl AnimeListsApi {
    pub(crate) fn new(client: AnimeScheduleClient) -> Self {
        Self { client }
    }

    /// Returns a specific List Anime object and an Etag in the response headers. Route is the anime's URL slug.
    pub fn get(&self) -> AnimeListsGet {
        AnimeListsGet {
            client: self.client.clone(),
            user_id: None,
        }
    }

    /// Import an anime list from MyAnimeList via .xml file
    pub fn put(&self) -> AnimeListsPut {
        AnimeListsPut {
            client: self.client.clone(),
            user_id: None,
            overwrite_mal_list: false,
            xml: None,
        }
    }

    /// Delete a user's specific List Anime
    pub fn delete(&self) -> AnimeListsDelete {
        AnimeListsDelete {
            client: self.client.clone(),
            route: None,
            user_id: None,
        }
    }
}

/// Returns a specific List Anime object and an Etag in the response headers. Route is the anime's URL slug.
pub struct AnimeListsGet {
    client: AnimeScheduleClient,

    /// user id to fetch from
    user_id: Option<String>,
}

impl AnimeListsGet {
    /// set the user id to get the lists from
    pub fn user_id(mut self, user_id: &str) -> Self {
        self.user_id = Some(user_id.to_owned());
        self
    }

    /// set the route to get the lists from. Route is the anime's URL slug.
    pub fn route(self, route: &str) -> AnimeListsGetRoute {
        AnimeListsGetRoute {
            client: self.client.clone(),
            user_id: self.user_id,
            route: route.to_owned(),
        }
    }

    pub async fn send(self) -> Result<(RateLimit, UserListAnime), ApiError> {
        let is_user_id = self.user_id.is_some();

        let url = if let Some(user_id) = self.user_id {
            API_ANIMELISTS_USERID.replace("{userId}", &user_id)
        } else {
            API_ANIMELISTS.to_owned()
        };

        let response = self
            .client
            .http
            .get(url)
            .bearer_auth(if is_user_id {
                // access to another's list
                self.client.auth.app_token().to_owned()
            } else {
                // access to self list
                self.client
                    .auth
                    .access_token()
                    .ok_or(ApiError::AccessToken)?
                    .secret()
                    .clone()
            })
            .send()
            .await?;

        let headers = response.headers();
        let limit = RateLimit::new(headers);

        let text = response.text().await?;

        if !text.is_json() {
            return Err(ApiError::Api(text));
        }

        let user_list: UserListAnime = serde_json::from_str(&text)?;

        Ok((limit.unwrap(), user_list))
    }

    pub fn send_blocking(self) -> Result<(RateLimit, UserListAnime), ApiError> {
        RUNTIME.block_on(self.send())
    }
}

#[derive(Debug)]
pub struct ETag(pub String);
impl Deref for ETag {
    type Target = str;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

/// Returns a specific List Anime object and an Etag in the response headers. Route is the anime's URL slug.
pub struct AnimeListsGetRoute {
    client: AnimeScheduleClient,

    /// user id to fetch from
    user_id: Option<String>,
    /// route to fetch from
    route: String,
}

impl AnimeListsGetRoute {
    /// set the user id to get the lists from
    pub fn user_id(mut self, user_id: &str) -> Self {
        self.user_id = Some(user_id.to_owned());
        self
    }

    pub async fn send(self) -> Result<(RateLimit, ETag, ListAnime), ApiError> {
        let is_user_id = self.user_id.is_some();

        let url = if let Some(user_id) = self.user_id {
            API_ANIMELISTS_USERID_ROUTE
                .replace("{userId}", &user_id)
                .replace("{route}", &self.route)
        } else {
            API_ANIMELISTS_ROUTE.replace("{route}", &self.route)
        };

        let response = self
            .client
            .http
            .get(url)
            .bearer_auth(if is_user_id {
                // access to another's list
                self.client.auth.app_token().to_owned()
            } else {
                // access to self list
                self.client
                    .auth
                    .access_token()
                    .ok_or(ApiError::AccessToken)?
                    .secret()
                    .clone()
            })
            .send()
            .await?;

        let headers = response.headers();
        let limit = RateLimit::new(headers);
        let etag = ETag(
            headers
                .get("etag")
                .and_then(|h| h.to_str().ok())
                .unwrap_or_default()
                .to_owned(),
        );

        let text = response.text().await?;

        if !text.is_json() {
            return Err(ApiError::Api(text));
        }

        let listanime: ListAnime = serde_json::from_str(&text)?;

        Ok((limit.unwrap(), etag, listanime))
    }

    pub fn send_blocking(self) -> Result<(RateLimit, ETag, ListAnime), ApiError> {
        RUNTIME.block_on(self.send())
    }
}

/// Import an anime list from MyAnimeList via .xml file
pub struct AnimeListsPut {
    client: AnimeScheduleClient,

    /// user id to put to
    user_id: Option<String>,
    /// whether to overwrite any preexisting List Anime with the ones being imported.
    overwrite_mal_list: bool,
    /// the myanimelist xml import file in the request. Up to 12mb in file size
    xml: Option<String>,
}

impl AnimeListsPut {
    pub fn route(self, route: &str) -> AnimeListsPutRoute {
        AnimeListsPutRoute {
            client: self.client,
            user_id: self.user_id,
            route: route.to_owned(),
            etag: None,
            list: ListAnimePut::default(),
        }
    }

    /// Set the user id to put to
    pub fn user_id(mut self, user_id: &str) -> Self {
        self.user_id = Some(user_id.to_owned());
        self
    }

    /// whether to overwrite any preexisting List Anime with the ones being imported.
    /// default is false
    pub fn overwrite_mal_list(mut self, overwrite: bool) -> Self {
        self.overwrite_mal_list = overwrite;
        self
    }

    /// An MyAnimeList .xml import file in the request. Up to 12MB in file size.
    pub fn xml<S: Into<String>>(mut self, data: S) -> Self {
        let data = data.into();
        self.xml = Some(data);
        self
    }

    pub async fn send(self) -> Result<RateLimit, ApiError> {
        let url = if let Some(user_id) = self.user_id {
            API_ANIMELISTS_USERID.replace("{userId}", &user_id)
        } else {
            API_ANIMELISTS.to_owned()
        };

        let Some(xml) = self.xml else {
            return Err(ApiError::Xml);
        };

        // The docs do not say how to do this part
        // so this was reverse engineered from the site's xml importer
        // the site uses a different api url for this, but I'm still using
        // the officially listed api url
        //
        // reverse engineer from here:
        // https://animeschedule.net/users/<your_username>/settings/import-export
        let part = multipart::Part::bytes(xml.into_bytes())
            .file_name("list.xml")
            .mime_str("text/xml")
            .unwrap();

        let mut form = multipart::Form::new();
        if self.overwrite_mal_list {
            form = form.text("overwrite-mal-list", "on");
        }
        form = form.part("mal-list", part);

        let response = self
            .client
            .http
            .put(url)
            .bearer_auth(
                self.client
                    .auth
                    .access_token()
                    .ok_or(ApiError::AccessToken)?
                    .secret()
                    .clone(),
            )
            .multipart(form)
            .send()
            .await?;

        let headers = response.headers();
        let limit = RateLimit::new(headers);

        let is_err = response.status().is_server_error() || response.status().is_client_error();

        let text = response.text().await?;

        if !text.is_empty() || is_err {
            return Err(ApiError::Api(text));
        }

        Ok(limit.unwrap())
    }

    pub fn send_blocking(self) -> Result<RateLimit, ApiError> {
        RUNTIME.block_on(self.send())
    }
}

/// Add/Update a specific List Anime for a user
pub struct AnimeListsPutRoute {
    client: AnimeScheduleClient,

    /// user id to put to
    user_id: Option<String>,
    /// the route's etag
    etag: Option<String>,
    /// route to put to
    route: String,
    /// the put list
    list: ListAnimePut,
}

impl AnimeListsPutRoute {
    /// Set the user id to put to
    pub fn user_id(mut self, user_id: &str) -> Self {
        self.user_id = Some(user_id.to_owned());
        self
    }

    /// An Etag header in the request headers. Mandatory and must be valid. You can get a
    /// valid Etag by doing a GET request on a specific List Anime beforehand and getting it
    /// from the response headers.
    pub fn etag(mut self, etag: &str) -> Self {
        self.etag = Some(etag.to_owned());
        self
    }

    /// The list the anime belongs to.
    pub fn list_status(mut self, status: ListStatus) -> Self {
        self.list.list_status = Some(status);
        self
    }

    /// The amount of episodes seen from the anime.
    pub fn episodes_seen(mut self, seen: u64) -> Self {
        self.list.episodes_seen = Some(seen);
        self
    }

    /// The user's manually inputted score of the anime. From 0 to a 100.
    pub fn manual_score(mut self, score: u8) -> Self {
        self.list.manual_score = Some(score.clamp(0, 100));
        self
    }

    /// Whether to use automatic score calculation with multiple scores.
    pub fn use_auto_scores(mut self, use_auto_scores: bool) -> Self {
        self.list.use_auto_scores = Some(use_auto_scores);
        self
    }

    /// set auto scores
    pub fn auto_scores(mut self, scores: AutoScores) -> Self {
        self.list.auto_scores = Some(scores);
        self
    }

    /// The date the anime was started watching.
    pub fn start_date<Tz: TimeZone>(mut self, datetime: DateTime<Tz>) -> Self {
        let datetime = datetime.with_timezone(&Utc);
        self.list.start_date = Some(datetime);
        self
    }

    /// The date the anime was finished watching.
    pub fn end_date<Tz: TimeZone>(mut self, datetime: DateTime<Tz>) -> Self {
        let datetime = datetime.with_timezone(&Utc);
        self.list.end_date = Some(datetime);
        self
    }

    /// User note. Max length is 1000.
    pub fn note(mut self, note: &str) -> Self {
        let mut note = note.to_owned();
        note.truncate(1000);

        self.list.note = Some(note);
        self
    }

    /// Indicates a non-standard operation. Used only in PUT requests. Valid values are deleteNote.
    pub fn action(mut self, action: Action) -> Self {
        self.list.action = Some(action);
        self
    }

    pub async fn send(self) -> Result<RateLimit, ApiError> {
        if self.etag.is_none() {
            return Err(ApiError::Etag);
        }

        let url = if let Some(user_id) = self.user_id {
            API_ANIMELISTS_USERID_ROUTE
                .replace("{userId}", &user_id)
                .replace("{route}", &self.route)
        } else {
            API_ANIMELISTS_ROUTE.replace("{route}", &self.route)
        };

        let response = self
            .client
            .http
            .put(url)
            .header("ETag", self.etag.unwrap())
            .bearer_auth(
                self.client
                    .auth
                    .access_token()
                    .ok_or(ApiError::AccessToken)?
                    .secret()
                    .clone(),
            )
            .json(&self.list)
            .send()
            .await?;

        let headers = response.headers();
        let limit = RateLimit::new(headers);

        let is_err = response.status().is_server_error() || response.status().is_client_error();

        let text = response.text().await?;

        if !text.is_empty() || is_err {
            return Err(ApiError::Api(text));
        }

        Ok(limit.unwrap())
    }

    pub fn send_blocking(self) -> Result<RateLimit, ApiError> {
        RUNTIME.block_on(self.send())
    }
}

/// Deletes a specific List Anime object from the user's anime list. Route is the anime's URL slug.
pub struct AnimeListsDelete {
    client: AnimeScheduleClient,

    /// anime url slug route to delete
    route: Option<String>,
    /// user id to delete from
    user_id: Option<String>,
}

impl AnimeListsDelete {
    /// set the user id to delete from
    pub fn user_id(mut self, user_id: &str) -> Self {
        self.user_id = Some(user_id.to_owned());
        self
    }

    /// set the route to delete from. this is mandatory
    pub fn route(mut self, route: &str) -> Self {
        self.route = Some(route.to_owned());
        self
    }

    pub async fn send(self) -> Result<RateLimit, ApiError> {
        let Some(route) = self.route else {
            return Err(ApiError::Route);
        };

        let url = if let Some(user_id) = self.user_id {
            API_ANIMELISTS_USERID_ROUTE
                .replace("{userId}", &user_id)
                .replace("{route}", &route)
        } else {
            API_ANIMELISTS_ROUTE.replace("{route}", &route)
        };

        let response = self
            .client
            .http
            .delete(url)
            .bearer_auth(
                self.client
                    .auth
                    .access_token()
                    .ok_or(ApiError::AccessToken)?
                    .secret()
                    .clone(),
            )
            .send()
            .await?;

        let headers = response.headers();
        let limit = RateLimit::new(headers);

        let is_err = response.status().is_server_error() || response.status().is_client_error();

        let text = response.text().await?;

        if !text.is_empty() || is_err {
            return Err(ApiError::Api(text));
        }

        Ok(limit.unwrap())
    }

    pub fn send_blocking(self) -> Result<RateLimit, ApiError> {
        RUNTIME.block_on(self.send())
    }
}