tosho-nids 0.1.1

An asynchronous client for NI by DS
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
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
#![warn(missing_docs, clippy::empty_docs, rustdoc::broken_intra_doc_links)]
#![doc = include_str!("../README.md")]

use crate::constants::SECURE_IMAGE_HOST;
use std::collections::HashMap;

use futures_util::TryStreamExt;
use tokio::io::{self, AsyncWriteExt};
use tosho_common::{
    ToshoAuthError, ToshoClientError, ToshoResult, bail_on_error, parse_json_response_failable,
};

use crate::{constants::BASE_API, models::ErrorResponse};

pub mod constants;
pub mod filters;
pub mod models;

pub use filters::*;

/// Main client for interacting with the NI API.
///
/// # Examples
/// ```rust,no_run
/// use tosho_nids::{Filter, NIClient};
///
/// #[tokio::main]
/// async fn main() {
///     let constants = tosho_nids::constants::get_constants(1); // Web
///     let client = NIClient::new(None, constants).unwrap();
///
///     let filter = Filter::default()
///        .add_filter(tosho_nids::FilterType::Title, "Attack on Titan")
///        .with_per_page(18);
///     let issues = client.get_issues(&filter).await.unwrap();
///     println!("Issues: {:?}", issues);
/// }
/// ```
#[derive(Clone)]
pub struct NIClient {
    inner: reqwest::Client,
    constants: &'static crate::constants::Constants,
    token: Option<String>,
}

impl std::fmt::Debug for NIClient {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("NIClient")
            .field("inner", &"reqwest::Client")
            .field("constants", &self.constants)
            .field("token", &self.token.as_deref().map(|_| "****"))
            .finish()
    }
}

impl NIClient {
    /// Create a new client instance.
    ///
    /// # Parameters
    /// * `token` - JWT token for download requests, if `None` you will only be able to make non-authenticated requests.
    /// * `constants` - Constants to use for the client, see [`crate::constants::get_constants`].
    pub fn new(
        token: Option<&str>,
        constants: &'static crate::constants::Constants,
    ) -> ToshoResult<Self> {
        Self::make_client(token, constants, None)
    }

    /// Attach a proxy to the client.
    ///
    /// This will clone the client and return a new client with the proxy attached.
    ///
    /// # Arguments
    /// * `proxy` - The proxy to attach to the client
    pub fn with_proxy(&self, proxy: reqwest::Proxy) -> ToshoResult<Self> {
        Self::make_client(self.token.as_deref(), self.constants, Some(proxy))
    }

    fn make_client(
        token: Option<impl Into<String>>,
        constants: &'static crate::constants::Constants,
        proxy: Option<reqwest::Proxy>,
    ) -> ToshoResult<Self> {
        let mut headers = reqwest::header::HeaderMap::new();
        headers.insert(
            reqwest::header::USER_AGENT,
            reqwest::header::HeaderValue::from_static(constants.ua),
        );
        headers.insert(
            reqwest::header::ORIGIN,
            reqwest::header::HeaderValue::from_static(crate::constants::BASE_WEB),
        );
        headers.insert(
            reqwest::header::REFERER,
            reqwest::header::HeaderValue::from_static(crate::constants::BASE_WEB),
        );
        headers.insert(
            reqwest::header::HOST,
            reqwest::header::HeaderValue::from_static(crate::constants::API_HOST),
        );

        let client = reqwest::Client::builder()
            .http2_adaptive_window(true)
            .use_rustls_tls()
            .default_headers(headers);

        let client = match proxy {
            Some(proxy) => client
                .proxy(proxy)
                .build()
                .map_err(ToshoClientError::BuildError),
            None => client.build().map_err(ToshoClientError::BuildError),
        }?;

        Ok(Self {
            inner: client,
            constants,
            token: token.map(Into::into),
        })
    }

    /// Create an authenticated headers map.
    ///
    /// Has `prefix_bearer` to prefix the token with `Bearer ` since most endpoints does not require it.
    fn auth_headers(&self, prefix_bearer: bool) -> ToshoResult<reqwest::header::HeaderMap> {
        let token = self
            .token
            .as_deref()
            .ok_or(ToshoAuthError::UnknownSession)?;

        let header_token = if prefix_bearer {
            format!("Bearer {}", token)
        } else {
            token.to_string()
        };
        let mut headers = reqwest::header::HeaderMap::new();
        headers.insert(
            reqwest::header::AUTHORIZATION,
            reqwest::header::HeaderValue::from_str(&header_token).map_err(|_| {
                ToshoClientError::HeaderParseError(
                    "Invalid bearer token provided to client".to_string(),
                )
            })?,
        );

        Ok(headers)
    }

    /// Make an authenticated request to the API.
    ///
    /// This request will automatically add all the required headers/cookies/auth method
    /// to the request.
    ///
    /// # Arguments
    /// * `method` - The HTTP method to use
    /// * `endpoint` - The endpoint to request (e.g. `/list`) - without the `/api/v1` prefix
    /// * `data` - The data to send in the request body (as form data)
    /// * `params` - The query params to send in the request
    /// * `authenticated` - Whether to make an authenticated request or not
    async fn request<T>(
        &self,
        method: reqwest::Method,
        endpoint: &str,
        data: Option<serde_json::Value>,
        params: Option<HashMap<String, String>>,
        headers: Option<reqwest::header::HeaderMap>,
    ) -> ToshoResult<T>
    where
        T: serde::de::DeserializeOwned,
    {
        let endpoint = format!("{}/api/v1{}", BASE_API, endpoint);
        let mut extend_headers = reqwest::header::HeaderMap::new();
        // Check ir provided a custom headers
        if let Some(hdrs) = headers {
            for (key, value) in hdrs.iter() {
                extend_headers.insert(key, value.clone());
            }
        }

        let request = match (data.clone(), params.clone()) {
            (None, None) => self.inner.request(method, endpoint).headers(extend_headers),
            (Some(data), None) => {
                extend_headers.insert(
                    reqwest::header::CONTENT_TYPE,
                    reqwest::header::HeaderValue::from_static("application/json"),
                );
                self.inner
                    .request(method, endpoint)
                    .json(&data)
                    .headers(extend_headers)
            }
            (None, Some(params)) => self
                .inner
                .request(method, endpoint)
                .headers(extend_headers)
                .query(&params),
            (Some(_), Some(_)) => {
                bail_on_error!("Cannot have both data and params")
            }
        };

        parse_json_response_failable::<T, ErrorResponse>(request.send().await?).await
    }

    /// Get the list of issues
    ///
    /// # Arguments
    /// * `filter` - The filter to apply to the request
    pub async fn get_issues(
        &self,
        filters: &filters::Filter,
    ) -> ToshoResult<models::IssueListResponse> {
        let params = filters.to_params();
        self.request(reqwest::Method::GET, "/issues", None, Some(params), None)
            .await
    }

    /// Get single issue detail
    ///
    /// # Arguments
    /// * `issue_id` - The issue UUID to get the detail for
    pub async fn get_issue(&self, issue_id: u32) -> ToshoResult<models::IssueDetail> {
        let resp = self
            .request::<models::IssueDetailResponse>(
                reqwest::Method::GET,
                &format!("/issues/{}", issue_id),
                None,
                None,
                None,
            )
            .await?;

        Ok(resp.data())
    }

    /// Get the list of series runs
    ///
    /// # Arguments
    /// * `filter` - The filter to apply to the request
    pub async fn get_series_runs(
        &self,
        filters: &filters::Filter,
    ) -> ToshoResult<models::series::SeriesRunList> {
        let params = filters.to_params();
        self.request(
            reqwest::Method::GET,
            "/series_run",
            None,
            Some(params),
            None,
        )
        .await
    }

    /// Get single series run detail via ID
    ///
    /// # Arguments
    /// * `series_run_id` - The series run ID to get the detail for
    pub async fn get_series_run(
        &self,
        series_run_id: u32,
    ) -> ToshoResult<models::series::SeriesRunWithEditions> {
        let resp = self
            .request::<models::series::SeriesRunWithEditionsResponse>(
                reqwest::Method::GET,
                &format!("/series_run/{}", series_run_id),
                None,
                None,
                None,
            )
            .await?;

        Ok(resp.data())
    }

    /// Get the list of publishers
    ///
    /// # Arguments
    /// * `filter` - The filter to apply to the request
    pub async fn get_publishers(
        &self,
        filters: Option<&filters::Filter>,
    ) -> ToshoResult<models::others::PublishersList> {
        let params = match filters {
            Some(f) => f.to_params(),
            None => filters::Filter::default()
                .with_order(filters::SortBy::Name, filters::SortOrder::ASC)
                .with_per_page(25)
                .to_params(),
        };

        self.request(
            reqwest::Method::GET,
            "/publishers",
            None,
            Some(params),
            None,
        )
        .await
    }

    /// Get single publisher detail via slug
    ///
    /// # Arguments
    /// * `publisher_slug` - The publisher slug to get the detail for
    pub async fn get_publisher(
        &self,
        publisher_slug: impl Into<String>,
    ) -> ToshoResult<models::common::Publisher> {
        let resp = self
            .request::<models::others::PublisherDetailResponse>(
                reqwest::Method::GET,
                &format!("/publishers/{}", publisher_slug.into()),
                None,
                None,
                None,
            )
            .await?;

        Ok(resp.data())
    }

    /// Get a list of publisher imprints for a publisher
    ///
    /// # Arguments
    /// * `publisher_slug` - The publisher slug to get the imprints for
    pub async fn get_publisher_imprints(
        &self,
        publisher_slug: impl Into<String>,
    ) -> ToshoResult<models::others::ImprintsList> {
        let params = HashMap::from([("slug".to_string(), publisher_slug.into())]);
        self.request(
            reqwest::Method::GET,
            "/publisher_imprints",
            None,
            Some(params),
            None,
        )
        .await
    }

    /// Get the list of genres
    ///
    /// # Arguments
    /// * `filter` - The filter to apply to the request
    pub async fn get_genres(
        &self,
        filters: Option<&filters::Filter>,
    ) -> ToshoResult<models::others::GenresList> {
        let params = match filters {
            Some(f) => f.to_params(),
            None => filters::Filter::default()
                .with_order(filters::SortBy::Name, filters::SortOrder::ASC)
                .with_per_page(100)
                .to_params(),
        };

        self.request(reqwest::Method::GET, "/genres", None, Some(params), None)
            .await
    }

    /// Get the list of creators
    ///
    /// # Arguments
    /// * `filter` - The filter to apply to the request
    pub async fn get_creators(
        &self,
        filters: Option<&filters::Filter>,
    ) -> ToshoResult<models::others::CreatorsList> {
        let params = match filters {
            Some(f) => f.to_params(),
            None => filters::Filter::default()
                .with_order(filters::SortBy::DisplayName, filters::SortOrder::ASC)
                .with_per_page(25)
                .to_params(),
        };

        self.request(reqwest::Method::GET, "/creators", None, Some(params), None)
            .await
    }

    /// Get the list of books/issues sold in the marketplace
    ///
    /// # Arguments
    /// * `filter` - The filter to apply to the request
    pub async fn get_marketplace_books(
        &self,
        filters: Option<&filters::Filter>,
    ) -> ToshoResult<models::others::MarketplaceBooksList> {
        let params = match filters {
            Some(f) => f.to_params(),
            None => filters::Filter::default()
                .with_order(filters::SortBy::EditionPriceMin, filters::SortOrder::ASC)
                .with_per_page(25)
                .to_params(),
        };

        self.request(
            reqwest::Method::GET,
            "/marketplace/books",
            None,
            Some(params),
            None,
        )
        .await
    }

    /// Get the list of editions in the marketplaces
    ///
    /// # Arguments
    /// * `filters` - The filter to apply to the request
    pub async fn get_marketplace_editions(
        &self,
        filters: Option<&filters::Filter>,
    ) -> ToshoResult<models::others::MarketplaceDetailedEditionsList> {
        let params = match filters {
            Some(f) => f.to_params(),
            None => filters::Filter::default()
                .with_order(filters::SortBy::MarketplacePrice, filters::SortOrder::ASC)
                .with_per_page(25)
                .to_params(),
        };

        self.request(
            reqwest::Method::GET,
            "/marketplace/editions",
            None,
            Some(params),
            None,
        )
        .await
    }

    /// Get the list of editions sold for an issue in the marketplace
    ///
    /// # Arguments
    /// * `issue_id` - The issue UUID to get the editions for
    /// * `filter` - The filter to apply to the request
    pub async fn get_marketplace_book_editions(
        &self,
        issue_id: impl Into<String>,
        filters: Option<&filters::Filter>,
    ) -> ToshoResult<models::others::MarketplaceEditionsList> {
        let mut params = match filters {
            Some(f) => f.to_params(),
            None => filters::Filter::default()
                .clear_filters()
                .with_order(filters::SortBy::BookIndex, filters::SortOrder::ASC)
                .to_params(),
        };
        params.insert("book_id".to_string(), issue_id.into());

        self.request(
            reqwest::Method::GET,
            "/marketplace/editions",
            None,
            Some(params),
            None,
        )
        .await
    }

    /// Get the list of series run in your collections
    ///
    /// This needs authentication.
    ///
    /// # Arguments
    /// * `filter` - The filter to apply to the request
    pub async fn get_series_run_collections(
        &self,
        filters: Option<&filters::Filter>,
    ) -> ToshoResult<models::series::SeriesRunList> {
        let params = match filters {
            Some(f) => f.to_params(),
            None => filters::Filter::default()
                .with_order(filters::SortBy::Title, filters::SortOrder::ASC)
                .with_per_page(18)
                .to_params(),
        };

        let headers = self.auth_headers(false)?;
        self.request(
            reqwest::Method::GET,
            "/collection/series_run",
            None,
            Some(params),
            Some(headers),
        )
        .await
    }

    /// Get the list of issues in your collection
    ///
    /// This needs authentication.
    ///
    /// # Arguments
    /// * `filter` - The filter to apply to the request
    pub async fn get_issue_collections(
        &self,
        filters: &filters::Filter,
    ) -> ToshoResult<models::PurchasedIssuesResponse> {
        let params = filters.to_params();

        let headers = self.auth_headers(false)?;
        self.request(
            reqwest::Method::GET,
            "/collection/books",
            None,
            Some(params),
            Some(headers),
        )
        .await
    }

    /// Get the list of editions for an issue in your collection
    ///
    /// This needs authentication.
    ///
    /// # Arguments
    /// * `issue_id` - The issue UUID to get the editions for
    pub async fn get_issue_editions_collections(
        &self,
        issue_id: impl Into<String>,
    ) -> ToshoResult<models::others::CollectedEditionList> {
        let headers = self.auth_headers(false)?;
        self.request(
            reqwest::Method::GET,
            &format!("/collection/books/{}/editions", issue_id.into()),
            None,
            None,
            Some(headers),
        )
        .await
    }

    /// Get your reading history list
    ///
    /// This needs authentication.
    pub async fn get_reading_history(&self) -> ToshoResult<models::others::ReadingHistoryList> {
        let headers = self.auth_headers(false)?;
        self.request(
            reqwest::Method::GET,
            "/collection/books/bookmarked",
            None,
            None,
            Some(headers),
        )
        .await
    }

    /// Get issue reader information
    ///
    /// This needs authentication.
    ///
    /// # Arguments
    /// * `issue_id` - The issue ID to get the reader info for
    pub async fn get_issue_reader(
        &self,
        issue_id: u32,
    ) -> ToshoResult<models::reader::ReaderPagesWithMeta> {
        let headers = self.auth_headers(false)?;

        let response = self
            .request::<models::reader::ReaderPagesResponse>(
                reqwest::Method::GET,
                &format!("/frameflow/{}", issue_id),
                None,
                None,
                Some(headers),
            )
            .await?;

        // Instant deref clone
        Ok(response.data())
    }

    /// Report a page as being viewed/read
    ///
    /// This needs authentication.
    ///
    /// # Arguments
    /// * `issue_uuid` - The issue UUID to report the page for
    /// * `page_number` - The page number to report as being viewed/read, this is 1-based from the pages list
    pub async fn report_page_view(
        &self,
        issue_uuid: impl Into<String>,
        page_number: u32,
    ) -> ToshoResult<models::AckResponse> {
        let data = serde_json::json!({
            "book": {
                "page": page_number,
            }
        });

        let headers = self.auth_headers(true)?;

        self.request(
            reqwest::Method::PATCH,
            &format!("/collection/books/{}/bookmark", issue_uuid.into()),
            Some(data),
            None,
            Some(headers),
        )
        .await
    }

    /// Stream download the image from the given URL.
    ///
    /// The URL can be obtained from [`get_issue_reader`].
    ///
    /// # Parameters
    /// * `url` - The URL to download the image from.
    /// * `writer` - The writer to write the image to.
    pub async fn stream_download(
        &self,
        url: impl AsRef<str>,
        mut writer: impl io::AsyncWrite + Unpin,
    ) -> ToshoResult<()> {
        let res = self
            .inner
            .get(url.as_ref())
            .headers({
                let mut headers = reqwest::header::HeaderMap::new();
                headers.insert(
                    "Host",
                    reqwest::header::HeaderValue::from_static(SECURE_IMAGE_HOST),
                );
                headers.insert(
                    "User-Agent",
                    reqwest::header::HeaderValue::from_static(self.constants.image_ua),
                );
                headers
            })
            .send()
            .await?;

        // bail if not success
        if !res.status().is_success() {
            Err(tosho_common::ToshoError::from(res.status()))
        } else {
            let mut stream = res.bytes_stream();
            while let Some(item) = stream.try_next().await? {
                writer.write_all(&item).await?;
                writer.flush().await?;
            }

            Ok(())
        }
    }

    /// Get the customer profile
    ///
    /// This needs authentication.
    pub async fn get_profile(&self) -> ToshoResult<models::others::CustomerDetail> {
        let headers = self.auth_headers(true)?;

        let resp = self
            .request::<models::others::CustomerDetailResponse>(
                reqwest::Method::GET,
                "/profile",
                None,
                None,
                Some(headers),
            )
            .await?;

        Ok(resp.data())
    }

    /// Refresh the JWT token
    ///
    /// This needs authentication and needs refresh token
    ///
    /// # Arguments
    /// * `refresh_token` - The refresh token to use for refreshing the JWT token
    pub async fn refresh_token(
        &self,
        refresh_token: impl Into<String>,
    ) -> ToshoResult<models::common::RefreshedTokenResponse> {
        let refresh_tok: String = refresh_token.into();
        let data = serde_json::json!({
            "refresh_token": refresh_tok
        });
        let headers = self.auth_headers(true)?;

        self.request(
            reqwest::Method::POST,
            "/auth/refresh_token",
            Some(data),
            None,
            Some(headers),
        )
        .await
    }

    /// Login to NI and get the auth tokens
    ///
    /// # Arguments
    /// * `email` - The email to use for login
    /// * `password` - The password to use for login
    pub async fn login(
        email: impl Into<String>,
        password: impl Into<String>,
        proxy: Option<reqwest::Proxy>,
    ) -> ToshoResult<models::others::LoginResponse> {
        let data = serde_json::json!({
            "customer": {
                "email": email.into(),
                "password": password.into(),
            }
        });

        let client = reqwest::Client::builder()
            .http2_adaptive_window(true)
            .use_rustls_tls()
            .default_headers({
                let mut headers = reqwest::header::HeaderMap::new();
                headers.insert(
                    reqwest::header::USER_AGENT,
                    reqwest::header::HeaderValue::from_static(constants::get_constants(1).ua),
                );
                headers.insert(
                    reqwest::header::ORIGIN,
                    reqwest::header::HeaderValue::from_static(crate::constants::BASE_WEB),
                );
                headers.insert(
                    reqwest::header::REFERER,
                    reqwest::header::HeaderValue::from_static(crate::constants::BASE_WEB),
                );
                headers.insert(
                    reqwest::header::HOST,
                    reqwest::header::HeaderValue::from_static(crate::constants::API_HOST),
                );
                headers
            });

        let client = match proxy {
            Some(proxy) => client
                .proxy(proxy)
                .build()
                .map_err(ToshoClientError::BuildError)?,
            None => client.build().map_err(ToshoClientError::BuildError)?,
        };

        let request = client
            .post(format!("{}/api/v1/auth/login", BASE_API))
            .json(&data);

        parse_json_response_failable::<models::others::LoginResponse, ErrorResponse>(
            request.send().await?,
        )
        .await
    }
}

/// Format a price in USD from the API format to a human-readable format.
///
/// This follows the Stripe currency convention (i.e. 199 = $1.99).
pub fn format_price(price: u64) -> f64 {
    (price as f64) / 100.0
}