mal-api 2.0.3

An asynchronous MyAnimeList (MAL) API library for Rust
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
use super::{error::MangaApiError, requests::GetUserMangaList, responses::MangaListStatus};
use async_trait::async_trait;
use oauth2::{AccessToken, ClientId};
use serde::{de::DeserializeOwned, Serialize};
use std::marker::PhantomData;

use crate::{
    common::{struct_to_form_data, PagingIter},
    manga::requests::{DeleteMyMangaListItem, UpdateMyMangaListStatus},
    oauth::{Authenticated, MalClientId, OauthClient},
    MANGA_URL, USER_URL,
};

use super::{
    requests::{GetMangaDetails, GetMangaList, GetMangaRanking},
    responses::{MangaDetails, MangaList, MangaRanking},
};
use reqwest;

#[doc(hidden)]
#[derive(Debug)]
pub struct Client {}

#[doc(hidden)]
#[derive(Debug)]
pub struct Oauth {}

#[doc(hidden)]
#[derive(Debug)]
pub struct None {}

/// The MangaApiClient provides functions for interacting with the various
/// `manga` and `user mangalist` MAL API endpoints. The accessible endpoints
/// vary depending on if the MangaApiClient was constructed from a
/// [MalClientId] or an [OauthClient].
///
/// Keep in mind that constructing a MangaApiClient from an [OauthClient] provides
/// more access to the MAL API than from a [MalClientId]. Check the MAL API documentation
/// to view which endpoints require an [OauthClient] versus a [MalClientId] to see which
/// one is most appropriate for your use case.
///
/// # Example
///
/// ```rust,ignore
/// use dotenvy;
/// use mal_api::oauth::MalClientId;
/// use mal_api::prelude::*;
///
/// #[tokio::main]
/// async fn main() {
///     dotenvy::dotenv().ok();
///
///     let client_id = MalClientId::from_env().unwrap();
///     let api_client = MangaApiClient::from(&client_id);
///     let common_fields = mal_api::manga::all_common_fields();
///     let detail_fields = mal_api::manga::all_detail_fields();
///
///     let query = GetMangaList::builder("one")
///         .fields(&common_fields)
///         .limit(3)
///         .build()
///         .unwrap();
///     let response = api_client.get_manga_list(&query).await;
///     if let Ok(response) = response {
///         println!("Response: {}\n", response);
///     }
///
///     let query = GetMangaDetails::builder(44347)
///         .fields(&detail_fields)
///         .build()
///         .unwrap();
///     let response = api_client.get_manga_details(&query).await;
///     if let Ok(response) = response {
///         println!("Response: {}\n", response);
///     }
///
///     let query = GetMangaRanking::builder(MangaRankingType::All)
///         .enable_nsfw()
///         .fields(&common_fields)
///         .limit(10)
///         .build()
///         .unwrap();
///     let response = api_client.get_manga_ranking(&query).await;
///     if let Ok(response) = response {
///         println!("Response: {}\n", response);
///     }
/// }
/// ```

#[derive(Debug, Clone)]
pub struct MangaApiClient<State = None> {
    client: reqwest::Client,
    client_id: Option<String>,
    access_token: Option<String>,
    state: PhantomData<State>,
}

impl From<&AccessToken> for MangaApiClient<Oauth> {
    fn from(value: &AccessToken) -> Self {
        MangaApiClient::<Oauth> {
            client: reqwest::Client::new(),
            client_id: None,
            access_token: Some(value.secret().clone()),
            state: PhantomData::<Oauth>,
        }
    }
}

impl From<&ClientId> for MangaApiClient<Client> {
    fn from(value: &ClientId) -> Self {
        MangaApiClient::<Client> {
            client: reqwest::Client::new(),
            client_id: Some(value.clone().to_string()),
            access_token: None,
            state: PhantomData::<Client>,
        }
    }
}

impl From<&MalClientId> for MangaApiClient<Client> {
    fn from(value: &MalClientId) -> Self {
        MangaApiClient::<Client> {
            client: reqwest::Client::new(),
            client_id: Some(value.0.to_string()),
            access_token: None,
            state: PhantomData::<Client>,
        }
    }
}

impl From<&OauthClient<Authenticated>> for MangaApiClient<Oauth> {
    fn from(value: &OauthClient<Authenticated>) -> Self {
        MangaApiClient {
            client: reqwest::Client::new(),
            client_id: None,
            access_token: Some(value.get_access_token().secret().clone()),
            state: PhantomData::<Oauth>,
        }
    }
}

/// This trait defines the common request methods available to both
/// Client and Oauth MangaApiClients
#[async_trait]
pub trait Request {
    async fn get<T>(&self, query: &T) -> Result<String, MangaApiError>
    where
        T: Serialize + Send + Sync;

    async fn get_details(&self, query: &GetMangaDetails) -> Result<String, MangaApiError>;

    async fn get_ranking(&self, query: &GetMangaRanking) -> Result<String, MangaApiError>;

    async fn get_user(&self, query: &GetUserMangaList) -> Result<String, MangaApiError>;

    async fn get_next_or_prev(&self, query: Option<&String>) -> Result<String, MangaApiError>;
}

#[async_trait]
impl Request for MangaApiClient<Client> {
    async fn get<T>(&self, query: &T) -> Result<String, MangaApiError>
    where
        T: Serialize + Send + Sync,
    {
        let response = self
            .client
            .get(MANGA_URL)
            .header("X-MAL-CLIENT-ID", self.client_id.as_ref().unwrap())
            .query(&query)
            .send()
            .await
            .map_err(|err| MangaApiError::new(format!("Failed get request: {}", err)))?;

        handle_response(response).await
    }

    async fn get_details(&self, query: &GetMangaDetails) -> Result<String, MangaApiError> {
        let response = self
            .client
            .get(format!("{}/{}", MANGA_URL, query.manga_id))
            .header("X-MAL-CLIENT-ID", self.client_id.as_ref().unwrap())
            .query(&query)
            .send()
            .await
            .map_err(|err| MangaApiError::new(format!("Failed get request: {}", err)))?;

        handle_response(response).await
    }

    async fn get_ranking(&self, query: &GetMangaRanking) -> Result<String, MangaApiError> {
        let response = self
            .client
            .get(format!("{}/ranking", MANGA_URL))
            .header("X-MAL-CLIENT-ID", self.client_id.as_ref().unwrap())
            .query(&query)
            .send()
            .await
            .map_err(|err| MangaApiError::new(format!("Failed get request: {}", err)))?;

        handle_response(response).await
    }

    async fn get_user(&self, query: &GetUserMangaList) -> Result<String, MangaApiError> {
        let response = self
            .client
            .get(format!("{}/{}/mangalist", USER_URL, query.user_name))
            .header("X-MAL-CLIENT-ID", self.client_id.as_ref().unwrap())
            .query(&query)
            .send()
            .await
            .map_err(|err| MangaApiError::new(format!("Failed get request: {}", err)))?;

        handle_response(response).await
    }

    async fn get_next_or_prev(&self, query: Option<&String>) -> Result<String, MangaApiError> {
        if let Some(itr) = query {
            let response = self
                .client
                .get(itr)
                .header("X-MAL-CLIENT-ID", self.client_id.as_ref().unwrap())
                .send()
                .await
                .map_err(|err| MangaApiError::new(format!("Failed get request: {}", err)))?;

            handle_response(response).await
        } else {
            Err(MangaApiError::new("Page does not exist".to_string()))
        }
    }
}

#[async_trait]
impl Request for MangaApiClient<Oauth> {
    async fn get<T>(&self, query: &T) -> Result<String, MangaApiError>
    where
        T: Serialize + std::marker::Send + std::marker::Sync,
    {
        let response = self
            .client
            .get(MANGA_URL)
            .bearer_auth(self.access_token.as_ref().unwrap())
            .query(&query)
            .send()
            .await
            .map_err(|err| MangaApiError::new(format!("Failed get request: {}", err)))?;

        handle_response(response).await
    }

    async fn get_details(&self, query: &GetMangaDetails) -> Result<String, MangaApiError> {
        let response = self
            .client
            .get(format!("{}/{}", MANGA_URL, query.manga_id))
            .bearer_auth(self.access_token.as_ref().unwrap())
            .query(&query)
            .send()
            .await
            .map_err(|err| MangaApiError::new(format!("Failed get request: {}", err)))?;

        handle_response(response).await
    }

    async fn get_ranking(&self, query: &GetMangaRanking) -> Result<String, MangaApiError> {
        let response = self
            .client
            .get(format!("{}/ranking", MANGA_URL))
            .bearer_auth(self.access_token.as_ref().unwrap())
            .query(&query)
            .send()
            .await
            .map_err(|err| MangaApiError::new(format!("Failed get request: {}", err)))?;

        handle_response(response).await
    }

    async fn get_user(&self, query: &GetUserMangaList) -> Result<String, MangaApiError> {
        let response = self
            .client
            .get(format!("{}/{}/mangalist", USER_URL, query.user_name))
            .bearer_auth(self.access_token.as_ref().unwrap())
            .query(&query)
            .send()
            .await
            .map_err(|err| MangaApiError::new(format!("Failed get request: {}", err)))?;

        handle_response(response).await
    }

    async fn get_next_or_prev(&self, query: Option<&String>) -> Result<String, MangaApiError> {
        if let Some(itr) = query {
            let response = self
                .client
                .get(itr)
                .bearer_auth(self.access_token.as_ref().unwrap())
                .send()
                .await
                .map_err(|err| MangaApiError::new(format!("Failed get request: {}", err)))?;

            handle_response(response).await
        } else {
            Err(MangaApiError::new("Page does not exist".to_string()))
        }
    }
}

/// This trait defines the shared endpoints for Client and Oauth
/// MangaApiClients. It provides default implementations such that
/// the Oauth MangaApiClient can override them if needed.
#[async_trait]
pub trait MangaApi {
    type State: Request + Send + Sync;

    /// Get a list of manga that are similar to the given query
    ///
    /// Corresponds to the [Get manga list](https://myanimelist.net/apiconfig/references/api/v2#operation/manga_get) endpoint
    async fn get_manga_list(&self, query: &GetMangaList) -> Result<MangaList, MangaApiError> {
        let response = self.get_self().get(query).await?;
        let result: MangaList = serde_json::from_str(response.as_str()).map_err(|err| {
            MangaApiError::new(format!("Failed to parse MangaList result: {}", err))
        })?;
        Ok(result)
    }

    /// Get the details of a manga that matches the given query
    ///
    /// Corresponds to the [Get manga details](https://myanimelist.net/apiconfig/references/api/v2#operation/manga_manga_id_get) endpoint
    async fn get_manga_details(
        &self,
        query: &GetMangaDetails,
    ) -> Result<MangaDetails, MangaApiError> {
        let response = self.get_self().get_details(query).await?;
        let result: MangaDetails = serde_json::from_str(response.as_str()).map_err(|err| {
            MangaApiError::new(format!("Failed to parse MangaList result: {}", err))
        })?;
        Ok(result)
    }

    /// Get the ranking of manga
    ///
    /// Corresponds to the [Get manga ranking](https://myanimelist.net/apiconfig/references/api/v2#operation/manga_ranking_get) endpoint
    async fn get_manga_ranking(
        &self,
        query: &GetMangaRanking,
    ) -> Result<MangaRanking, MangaApiError> {
        let response = self.get_self().get_ranking(query).await?;
        let result: MangaRanking = serde_json::from_str(response.as_str()).map_err(|err| {
            MangaApiError::new(format!("Failed to parse MangaList result: {}", err))
        })?;
        Ok(result)
    }

    /// Get a users manga list
    ///
    /// You **cannot** get the manga list of `@me` with a [ClientId] MangaApiClient
    ///
    /// Corresponds to the [Get user mangalist](https://myanimelist.net/apiconfig/references/api/v2#operation/users_user_id_mangalist_get) endpoint
    async fn get_user_manga_list(
        &self,
        query: &GetUserMangaList,
    ) -> Result<MangaList, MangaApiError> {
        if query.user_name == "@me".to_string() {
            return Err(MangaApiError::new(
                "You can only get your list via an Oauth client".to_string(),
            ));
        }
        let response = self.get_self().get_user(query).await?;
        let result: MangaList = serde_json::from_str(response.as_str()).map_err(|err| {
            MangaApiError::new(format!("Failed to parse Anime List result: {}", err))
        })?;
        Ok(result)
    }

    /// Return the results of the next page, if possible
    async fn next<T>(&self, response: &T) -> Result<T, MangaApiError>
    where
        T: DeserializeOwned + PagingIter + Sync + Send,
    {
        let response = self
            .get_self()
            .get_next_or_prev(response.next_page())
            .await?;
        let result: T = serde_json::from_str(response.as_str())
            .map_err(|err| MangaApiError::new(format!("Failed to fetch next page: {}", err)))?;
        Ok(result)
    }

    /// Return the results of the previous page, if possible
    async fn prev<T>(&self, response: &T) -> Result<T, MangaApiError>
    where
        T: DeserializeOwned + PagingIter + Sync + Send,
    {
        let response = self
            .get_self()
            .get_next_or_prev(response.prev_page())
            .await?;
        let result: T = serde_json::from_str(response.as_str())
            .map_err(|err| MangaApiError::new(format!("Failed to fetch next page: {}", err)))?;
        Ok(result)
    }

    /// Utility method for API trait to use the appropriate request method
    fn get_self(&self) -> &Self::State;
}

#[async_trait]
impl MangaApi for MangaApiClient<Client> {
    type State = MangaApiClient<Client>;

    fn get_self(&self) -> &Self::State {
        self
    }
}

#[async_trait]
impl MangaApi for MangaApiClient<Oauth> {
    type State = MangaApiClient<Oauth>;

    fn get_self(&self) -> &Self::State {
        self
    }

    /// Get a users manga list
    ///
    /// You **can** get the manga list of `@me` with an [OauthClient] MangaApiClient
    ///
    /// Corresponds to the [Get user mangalist](https://myanimelist.net/apiconfig/references/api/v2#operation/users_user_id_mangalist_get) endpoint
    async fn get_user_manga_list(
        &self,
        query: &GetUserMangaList,
    ) -> Result<MangaList, MangaApiError> {
        let response = self.get_self().get_user(query).await?;
        let result: MangaList = serde_json::from_str(response.as_str()).map_err(|err| {
            MangaApiError::new(format!("Failed to parse Anime List result: {}", err))
        })?;
        Ok(result)
    }
}

impl MangaApiClient<Oauth> {
    /// Update the status of a manga for the OAuth user's manga list
    ///
    /// Correspoonds to the [Update my manga list status](https://myanimelist.net/apiconfig/references/api/v2#operation/manga_manga_id_my_list_status_put) endpoint
    pub async fn update_manga_list_status(
        &self,
        query: &UpdateMyMangaListStatus,
    ) -> Result<MangaListStatus, MangaApiError> {
        let form_data = struct_to_form_data(&query).map_err(|err| {
            MangaApiError::new(format!("Failed to turn request into form data: {}", err))
        })?;
        let response = self
            .client
            .put(format!("{}/{}/my_list_status", MANGA_URL, query.manga_id))
            .bearer_auth(&self.access_token.as_ref().unwrap())
            .form(&form_data)
            .send()
            .await
            .map_err(|err| MangaApiError::new(format!("Failed put request: {}", err)))?;

        let response = handle_response(response).await?;
        let result: MangaListStatus = serde_json::from_str(response.as_str()).map_err(|err| {
            MangaApiError::new(format!("Failed to parse Anime List result: {}", err))
        })?;
        Ok(result)
    }

    /// Delete a manga entry from the OAuth user's manga list
    ///
    /// Corresponds to the [Delete my manga list item](https://myanimelist.net/apiconfig/references/api/v2#operation/manga_manga_id_my_list_status_delete) endpoint
    pub async fn delete_manga_list_item(
        &self,
        query: &DeleteMyMangaListItem,
    ) -> Result<(), MangaApiError> {
        let response = self
            .client
            .delete(format!("{}/{}/my_list_status", MANGA_URL, query.manga_id))
            .bearer_auth(&self.access_token.as_ref().unwrap())
            .send()
            .await
            .map_err(|err| MangaApiError::new(format!("Failed delete request: {}", err)))?;

        match response.status() {
            reqwest::StatusCode::OK => Ok(()),
            reqwest::StatusCode::NOT_FOUND => Err(MangaApiError::new(
                "Manga does not exist in user's manga list".to_string(),
            )),
            _ => Err(MangaApiError::new(format!(
                "Did not recieve expected response: {}",
                response.status()
            ))),
        }
    }
}

async fn handle_response(response: reqwest::Response) -> Result<String, MangaApiError> {
    match response.status() {
        reqwest::StatusCode::OK => {
            let content = response.text().await.map_err(|err| {
                MangaApiError::new(format!("Failed to get content from response: {}", err))
            })?;
            Ok(content)
        }
        _ => Err(MangaApiError::new(format!(
            "Did not recieve OK response: {}",
            response.status()
        ))),
    }
}