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
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
use {
    super::{client::Client, error::Result as Rs621Result},
    chrono::{offset::Utc, DateTime},
    derivative::Derivative,
    futures::{
        prelude::*,
        task::{Context, Poll},
    },
    itertools::Itertools,
    serde::{
        de::{self, Visitor},
        Deserialize, Deserializer,
    },
    std::{borrow::Borrow, pin::Pin},
};

/// Chunk size used for iterators performing requests
const ITER_CHUNK_SIZE: u64 = 320;

#[derive(Debug, PartialEq, Eq, Deserialize)]
pub enum PostFileExtension {
    #[serde(rename = "jpg")]
    Jpeg,
    #[serde(rename = "png")]
    Png,
    #[serde(rename = "gif")]
    Gif,
    #[serde(rename = "swf")]
    Swf,
    #[serde(rename = "webm")]
    WebM,
}

#[derive(Debug, PartialEq, Eq, Deserialize)]
pub struct PostFile {
    pub width: u64,
    pub height: u64,
    pub ext: PostFileExtension,
    pub size: u64,
    pub md5: String,
    pub url: Option<String>,
}

#[derive(Debug, PartialEq, Eq, Deserialize)]
pub struct PostPreview {
    pub width: u64,
    pub height: u64,
    pub url: Option<String>,
}

#[derive(Debug, PartialEq, Eq, Deserialize)]
pub struct PostSample {
    pub width: u64,
    pub height: u64,
    pub url: Option<String>,
}

#[derive(Debug, PartialEq, Eq, Deserialize)]
pub struct PostScore {
    pub up: i64,
    pub down: i64,
    pub total: i64,
}

#[derive(Debug, PartialEq, Eq, Deserialize)]
pub struct PostTags {
    pub general: Vec<String>,
    pub species: Vec<String>,
    pub character: Vec<String>,
    pub artist: Vec<String>,
    pub invalid: Vec<String>,
    pub lore: Vec<String>,
    pub meta: Vec<String>,
}

#[derive(Debug, PartialEq, Eq, Deserialize)]
pub struct PostFlags {
    #[serde(deserialize_with = "nullable_bool_from_json")]
    pub pending: bool,
    #[serde(deserialize_with = "nullable_bool_from_json")]
    pub flagged: bool,
    #[serde(deserialize_with = "nullable_bool_from_json")]
    pub note_locked: bool,
    #[serde(deserialize_with = "nullable_bool_from_json")]
    pub status_locked: bool,
    #[serde(deserialize_with = "nullable_bool_from_json")]
    pub rating_locked: bool,
    #[serde(deserialize_with = "nullable_bool_from_json")]
    pub deleted: bool,
}

#[derive(Debug, PartialEq, Eq, Deserialize)]
pub enum PostRating {
    #[serde(rename = "s")]
    Safe,
    #[serde(rename = "q")]
    Questionable,
    #[serde(rename = "e")]
    Explicit,
}

#[derive(Debug, PartialEq, Eq, Deserialize)]
pub struct PostRelationships {
    pub parent_id: Option<u64>,
    pub has_children: bool,
    pub has_active_children: bool,
    pub children: Vec<u64>,
}

/// Structure representing a post.
#[derive(Debug, PartialEq, Eq, Deserialize)]
pub struct Post {
    pub id: u64,
    pub created_at: DateTime<Utc>,
    pub updated_at: Option<DateTime<Utc>>,
    pub file: PostFile,
    pub preview: PostPreview,
    pub sample: Option<PostSample>,
    pub score: PostScore,
    pub tags: PostTags,
    pub locked_tags: Vec<String>,
    pub change_seq: u64,
    pub flags: PostFlags,
    pub rating: PostRating,
    pub fav_count: u64,
    pub sources: Vec<String>,
    pub pools: Vec<u64>,
    pub relationships: PostRelationships,
    pub approver_id: Option<u64>,
    pub uploader_id: u64,
    pub description: String,
    pub comment_count: u64,
    pub is_favorited: bool,
}

#[derive(Debug, PartialEq, Eq, Deserialize)]
struct PostListApiResponse {
    pub posts: Vec<Post>,
}

#[derive(Debug, PartialEq, Eq, Deserialize)]
struct PostShowApiResponse {
    pub post: Post,
}

fn nullable_bool_from_json<'de, D>(de: D) -> Result<bool, D::Error>
where
    D: Deserializer<'de>,
{
    struct NullableBoolVisitor;

    impl<'de> Visitor<'de> for NullableBoolVisitor {
        type Value = bool;

        fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
            formatter.write_str("null or bool")
        }

        fn visit_bool<E: de::Error>(self, v: bool) -> Result<bool, E> {
            Ok(v)
        }

        fn visit_unit<E: de::Error>(self) -> Result<bool, E> {
            Ok(false)
        }
    }

    de.deserialize_any(NullableBoolVisitor)
}

/// A search query. Contains information about the tags used and an URL encoded version of the tags.
#[derive(Debug, PartialEq, Clone)]
pub struct Query {
    url_encoded_tags: String,
    ordered: bool,
}

impl<T> From<&[T]> for Query
where
    T: AsRef<str>,
{
    fn from(q: &[T]) -> Self {
        let tags: Vec<&str> = q.iter().map(|t| t.as_ref()).collect();
        let query_str = tags.join(" ");
        let url_encoded_tags = urlencoding::encode(&query_str);
        let ordered = tags.iter().any(|t| t.starts_with("order:"));

        Query {
            url_encoded_tags,
            ordered,
        }
    }
}

#[derive(Debug, PartialEq, Eq)]
pub enum SearchPage {
    Page(u64),
    BeforePost(u64),
    AfterPost(u64),
}

/// Iterator returning posts from a search query.
#[derive(Derivative)]
#[derivative(Debug)]
pub struct PostSearchStream<'a> {
    client: &'a Client,
    query: Query,

    query_url: Option<String>,

    #[derivative(Debug = "ignore")]
    query_future: Option<Pin<Box<dyn Future<Output = Rs621Result<serde_json::Value>> + Send>>>,

    next_page: SearchPage,
    chunk: Vec<Rs621Result<Post>>,
    ended: bool,
}

impl<'a> PostSearchStream<'a> {
    fn new<T: Into<Query>>(client: &'a Client, query: T, page: SearchPage) -> Self {
        PostSearchStream {
            client: client,
            query: query.into(),

            query_url: None,
            query_future: None,

            next_page: page,
            chunk: Vec::new(),
            ended: false,
        }
    }
}

impl<'a> Stream for PostSearchStream<'a> {
    type Item = Rs621Result<Post>;

    fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Rs621Result<Post>>> {
        enum QueryPollRes {
            Pending,
            Err(crate::error::Error),
            NotFetching,
        }

        let this = self.get_mut();

        loop {
            // poll the pending query future if there's any
            let query_status = if let Some(ref mut fut) = this.query_future {
                match fut.as_mut().poll(cx) {
                    Poll::Ready(res) => {
                        // the future is finished, drop it
                        this.query_future = None;

                        match res {
                            Ok(body) => {
                                // put everything in the chunk
                                this.chunk =
                                    match serde_json::from_value::<PostListApiResponse>(body) {
                                        Ok(res) => res
                                            .posts
                                            .into_iter()
                                            .rev()
                                            .map(|post| Ok(post))
                                            .collect(),
                                        Err(e) => vec![Err(e.into())],
                                    };

                                let last_id = match this.chunk.first() {
                                    Some(Ok(post)) => post.id,
                                    _ => 0,
                                };

                                // we now know what will be the next page
                                this.next_page = if this.query.ordered {
                                    match this.next_page {
                                        SearchPage::Page(i) => SearchPage::Page(i + 1),
                                        _ => SearchPage::Page(1),
                                    }
                                } else {
                                    match this.next_page {
                                        SearchPage::Page(_) => SearchPage::BeforePost(last_id),
                                        SearchPage::BeforePost(_) => {
                                            SearchPage::BeforePost(last_id)
                                        }
                                        SearchPage::AfterPost(_) => SearchPage::AfterPost(last_id),
                                    }
                                };

                                // mark the stream as ended if there was no posts
                                this.ended = this.chunk.is_empty();
                                QueryPollRes::NotFetching
                            }

                            // if there was an error, stream it and mark the stream as ended
                            Err(e) => {
                                this.ended = true;
                                QueryPollRes::Err(e)
                            }
                        }
                    }

                    Poll::Pending => QueryPollRes::Pending,
                }
            } else {
                QueryPollRes::NotFetching
            };

            match query_status {
                QueryPollRes::Err(e) => return Poll::Ready(Some(Err(e))),
                QueryPollRes::Pending => return Poll::Pending,
                QueryPollRes::NotFetching if this.ended => {
                    // the stream ended because:
                    // 1. there was an error
                    // 2. there's simply no more elements
                    return Poll::Ready(None);
                }
                QueryPollRes::NotFetching if !this.chunk.is_empty() => {
                    // get a post
                    let post = this.chunk.pop().unwrap();

                    // stream the post
                    return Poll::Ready(Some(post));
                }
                QueryPollRes::NotFetching => {
                    // we need to load a new chunk of posts
                    let url = format!(
                        "/posts.json?limit={}&page={}&tags={}",
                        ITER_CHUNK_SIZE,
                        match this.next_page {
                            SearchPage::Page(i) => format!("{}", i),
                            SearchPage::BeforePost(i) => format!("b{}", i),
                            SearchPage::AfterPost(i) => format!("a{}", i),
                        },
                        this.query.url_encoded_tags
                    );
                    this.query_url = Some(url);

                    // get the JSON
                    this.query_future = Some(Box::pin(
                        this.client
                            .get_json_endpoint(this.query_url.as_ref().unwrap()),
                    ));
                }
            }
        }
    }
}

/// Iterator returning posts from a search query.
#[derive(Derivative)]
#[derivative(Debug)]
pub struct PostStream<'a, I, T>
where
    T: Borrow<u64> + Unpin,
    I: Iterator<Item = T> + Unpin,
{
    client: &'a Client,
    ids: I,

    query_url: Option<String>,

    #[derivative(Debug = "ignore")]
    query_future: Option<Pin<Box<dyn Future<Output = Rs621Result<serde_json::Value>> + Send>>>,

    chunk: Vec<Rs621Result<Post>>,
}

impl<'a, I, T> PostStream<'a, I, T>
where
    T: Borrow<u64> + Unpin,
    I: Iterator<Item = T> + Unpin,
{
    fn new(client: &'a Client, ids: I) -> Self {
        PostStream {
            client,
            ids,
            query_url: None,
            query_future: None,
            chunk: Vec::new(),
        }
    }
}

impl<'a, I, T> Stream for PostStream<'a, I, T>
where
    T: Borrow<u64> + Unpin,
    I: Iterator<Item = T> + Unpin,
{
    type Item = Rs621Result<Post>;

    fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Rs621Result<Post>>> {
        enum QueryPollRes {
            Pending,
            Err(crate::error::Error),
            NotFetching,
        }

        let this = self.get_mut();

        loop {
            // poll the pending query future if there's any
            let query_status = if let Some(ref mut fut) = this.query_future {
                match fut.as_mut().poll(cx) {
                    Poll::Ready(res) => {
                        // the future is finished, drop it
                        this.query_future = None;

                        match res {
                            Ok(body) => {
                                // put everything in the chunk
                                this.chunk =
                                    match serde_json::from_value::<PostListApiResponse>(body) {
                                        Ok(res) => res
                                            .posts
                                            .into_iter()
                                            .rev()
                                            .map(|post| Ok(post))
                                            .collect(),
                                        Err(e) => vec![Err(e.into())],
                                    };

                                QueryPollRes::NotFetching
                            }

                            // if there was an error, stream it
                            Err(e) => QueryPollRes::Err(e),
                        }
                    }

                    Poll::Pending => QueryPollRes::Pending,
                }
            } else {
                QueryPollRes::NotFetching
            };

            match query_status {
                QueryPollRes::Err(e) => return Poll::Ready(Some(Err(e))),
                QueryPollRes::Pending => return Poll::Pending,
                QueryPollRes::NotFetching if !this.chunk.is_empty() => {
                    // get a post
                    let post = this.chunk.pop().unwrap();

                    // stream the post
                    return Poll::Ready(Some(post));
                }
                QueryPollRes::NotFetching => {
                    // we need to load a new chunk of posts
                    let id_list = this.ids.by_ref().take(100).map(|x| *x.borrow()).join(",");

                    if id_list.is_empty() {
                        // the stream ended
                        return Poll::Ready(None);
                    }

                    let url = format!("/posts.json?tags=id%3A{}", id_list);
                    this.query_url = Some(url);

                    // get the JSON
                    this.query_future = Some(Box::pin(
                        this.client
                            .get_json_endpoint(this.query_url.as_ref().unwrap()),
                    ));
                }
            }
        }
    }
}

impl Client {
    /// Returns posts with the given IDs. Note that the order is NOT preserved!
    ///
    /// ```no_run
    /// # use rs621::client::Client;
    /// use futures::prelude::*;
    ///
    /// # #[tokio::main]
    /// # async fn main() -> rs621::error::Result<()> {
    /// let client = Client::new("https://e926.net", "MyProject/1.0 (by username on e621)")?;
    /// let mut post_stream = client.get_posts(&[8595, 535, 2105, 1470]);
    ///
    /// while let Some(post) = post_stream.next().await {
    ///     println!("Post #{}", post?.id);
    /// }
    /// # Ok(()) }
    /// ```
    pub fn get_posts<'a, I, J, T>(&'a self, ids: I) -> PostStream<'a, J, T>
    where
        T: Borrow<u64> + Unpin,
        J: Iterator<Item = T> + Unpin,
        I: IntoIterator<Item = T, IntoIter = J> + Unpin,
    {
        PostStream::new(self, ids.into_iter())
    }

    /// Returns a Stream over all the posts matching the search query.
    ///
    /// ```no_run
    /// # use rs621::{client::Client, post::PostRating};
    /// use futures::prelude::*;
    ///
    /// # #[tokio::main]
    /// # async fn main() -> rs621::error::Result<()> {
    /// let client = Client::new("https://e926.net", "MyProject/1.0 (by username on e621)")?;
    ///
    /// let mut post_stream = client.post_search(&["fluffy", "rating:s"][..]).take(3);
    ///
    /// while let Some(post) = post_stream.next().await {
    ///     assert_eq!(post?.rating, PostRating::Safe);
    /// }
    /// # Ok(()) }
    /// ```
    pub fn post_search<'a, T: Into<Query>>(&'a self, tags: T) -> PostSearchStream<'a> {
        self.post_search_from_page(tags, SearchPage::Page(1))
    }

    /// Same as [`Client::post_search`], but starting from a specific result page.
    ///
    /// ```no_run
    /// # use rs621::{client::Client, post::{PostRating, SearchPage}};
    /// use futures::prelude::*;
    ///
    /// # #[tokio::main]
    /// # async fn main() -> rs621::error::Result<()> {
    /// let client = Client::new("https://e926.net", "MyProject/1.0 (by username on e621)")?;
    ///
    /// let mut post_stream = client
    ///     .post_search_from_page(&["fluffy", "rating:s"][..], SearchPage::Page(4))
    ///     .take(5);
    ///
    /// while let Some(post) = post_stream.next().await {
    ///     let post = post?;
    ///
    ///     assert_eq!(post.rating, PostRating::Safe);
    /// }
    /// # Ok(()) }
    /// ```
    ///

    /// Returns a Stream over all the posts matching the search query, starting from the given page.
    ///
    /// ```no_run
    /// # use {
    /// #     rs621::{client::Client, post::PostRating},
    /// #     futures::prelude::*,
    /// # };
    /// use rs621::post::SearchPage;
    /// # #[tokio::main]
    /// # async fn main() -> rs621::error::Result<()> {
    /// let client = Client::new("https://e926.net", "MyProject/1.0 (by username on e621)")?;
    ///
    /// let mut post_stream = client
    ///     .post_search_from_page(&["fluffy", "rating:s"][..], SearchPage::BeforePost(123456))
    ///     .take(3);
    ///
    /// while let Some(post) = post_stream.next().await {
    ///     let post = post?;
    ///     assert!(post.id < 123456);
    ///     assert_eq!(post.rating, PostRating::Safe);
    /// }
    /// # Ok(()) }
    /// ```
    pub fn post_search_from_page<'a, T: Into<Query>>(
        &'a self,
        tags: T,
        page: SearchPage,
    ) -> PostSearchStream<'a> {
        PostSearchStream::new(self, tags, page)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use mockito::{mock, Matcher};

    #[tokio::test]
    async fn search_ordered() {
        let client = Client::new(&mockito::server_url(), b"rs621/unit_test").unwrap();

        let query = Query::from(&["fluffy", "rating:s", "order:score"][..]);

        let _m = mock(
            "GET",
            Matcher::Exact(format!(
                "/posts.json?limit={}&page=1&tags={}",
                ITER_CHUNK_SIZE, query.url_encoded_tags
            )),
        )
        .with_body(include_str!(
            "mocked/320_page-1_fluffy_rating-s_order-score.json"
        ))
        .create();

        assert_eq!(
            client
                .post_search(query)
                .take(100)
                .collect::<Vec<_>>()
                .await,
            serde_json::from_str::<PostListApiResponse>(include_str!(
                "mocked/320_page-1_fluffy_rating-s_order-score.json"
            ))
            .unwrap()
            .posts
            .into_iter()
            .take(100)
            .map(|x| Ok(x))
            .collect::<Vec<_>>()
        );
    }

    #[tokio::test]
    async fn search_above_limit_ordered() {
        let client = Client::new(&mockito::server_url(), b"rs621/unit_test").unwrap();

        let query = Query::from(&["fluffy", "rating:s", "order:score"][..]);
        const PAGES: [&str; 2] = [
            include_str!("mocked/320_page-1_fluffy_rating-s_order-score.json"),
            include_str!("mocked/320_page-2_fluffy_rating-s_order-score.json"),
        ];

        let _m = [
            mock(
                "GET",
                Matcher::Exact(format!(
                    "/posts.json?limit={}&page=1&tags={}",
                    ITER_CHUNK_SIZE, query.url_encoded_tags
                )),
            )
            .with_body(PAGES[0])
            .create(),
            mock(
                "GET",
                Matcher::Exact(format!(
                    "/posts.json?limit={}&page=2&tags={}",
                    ITER_CHUNK_SIZE, query.url_encoded_tags
                )),
            )
            .with_body(PAGES[1])
            .create(),
        ];

        assert_eq!(
            client
                .post_search(query)
                .take(400)
                .collect::<Vec<_>>()
                .await,
            serde_json::from_str::<PostListApiResponse>(PAGES[0])
                .unwrap()
                .posts
                .into_iter()
                .chain(
                    serde_json::from_str::<PostListApiResponse>(PAGES[1])
                        .unwrap()
                        .posts
                        .into_iter()
                )
                .take(400)
                .map(|x| Ok(x))
                .collect::<Vec<_>>()
        );
    }

    #[tokio::test]
    async fn search_before_id() {
        let client = Client::new(&mockito::server_url(), b"rs621/unit_test").unwrap();

        let query = Query::from(&["fluffy", "rating:s"][..]);
        let response_json = include_str!("mocked/320_fluffy_rating-s_before-2269211.json");
        let response: PostListApiResponse = serde_json::from_str(response_json).unwrap();
        let expected: Vec<_> = response.posts.into_iter().take(80).map(|x| Ok(x)).collect();

        let _m = mock(
            "GET",
            Matcher::Exact(format!(
                "/posts.json?limit={}&page=b2269211&tags={}",
                ITER_CHUNK_SIZE, query.url_encoded_tags
            )),
        )
        .with_body(response_json)
        .create();

        assert_eq!(
            client
                .post_search_from_page(query, SearchPage::BeforePost(2269211))
                .take(80)
                .collect::<Vec<_>>()
                .await,
            expected
        );
    }

    #[tokio::test]
    async fn search_above_limit() {
        let client = Client::new(&mockito::server_url(), b"rs621/unit_test").unwrap();

        let query = Query::from(&["fluffy", "rating:s"][..]);
        let responses_json: [&str; 2] = [
            include_str!("mocked/320_fluffy_rating-s.json"),
            include_str!("mocked/320_fluffy_rating-s_before-2269211.json"),
        ];
        let mut responses: [Option<PostListApiResponse>; 2] = [
            Some(serde_json::from_str(responses_json[0]).unwrap()),
            Some(serde_json::from_str(responses_json[1]).unwrap()),
        ];
        let expected: Vec<_> = responses[0]
            .take()
            .unwrap()
            .posts
            .into_iter()
            .chain(responses[1].take().unwrap().posts.into_iter())
            .take(400)
            .map(|x| Ok(x))
            .collect();

        let _m = [
            mock(
                "GET",
                Matcher::Exact(format!(
                    "/posts.json?limit={}&page=1&tags={}",
                    ITER_CHUNK_SIZE, query.url_encoded_tags
                )),
            )
            .with_body(responses_json[0])
            .create(),
            mock(
                "GET",
                Matcher::Exact(format!(
                    "/posts.json?limit={}&page=b2269211&tags={}",
                    ITER_CHUNK_SIZE, query.url_encoded_tags
                )),
            )
            .with_body(responses_json[1])
            .create(),
        ];

        assert_eq!(
            client
                .post_search(query)
                .take(400)
                .collect::<Vec<_>>()
                .await,
            expected
        );
    }

    #[tokio::test]
    async fn search_no_result() {
        let client = Client::new(&mockito::server_url(), b"rs621/unit_test").unwrap();

        let query = Query::from(&["fluffy", "rating:s"][..]);
        let response = "{\"posts\":[]}";

        let _m = mock(
            "GET",
            Matcher::Exact(format!(
                "/posts.json?limit={}&page=1&tags={}",
                ITER_CHUNK_SIZE, query.url_encoded_tags
            )),
        )
        .with_body(response)
        .create();

        assert_eq!(
            client.post_search(query).take(5).collect::<Vec<_>>().await,
            vec![]
        );
    }

    #[tokio::test]
    async fn search_simple() {
        let client = Client::new(&mockito::server_url(), b"rs621/unit_test").unwrap();

        let query = Query::from(&["fluffy", "rating:s"][..]);
        let response_json = include_str!("mocked/320_fluffy_rating-s.json");
        let response: PostListApiResponse = serde_json::from_str(response_json).unwrap();
        let expected: Vec<_> = response.posts.into_iter().take(5).map(|x| Ok(x)).collect();

        let _m = mock(
            "GET",
            Matcher::Exact(format!(
                "/posts.json?limit={}&page=1&tags={}",
                ITER_CHUNK_SIZE, query.url_encoded_tags
            )),
        )
        .with_body(response_json)
        .create();

        assert_eq!(
            client.post_search(query).take(5).collect::<Vec<_>>().await,
            expected
        );
    }

    #[tokio::test]
    async fn get_posts_by_id() {
        let client = Client::new(&mockito::server_url(), b"rs621/unit_test").unwrap();

        let response_json = include_str!("mocked/id_8595_535_2105_1470.json");
        let response: PostListApiResponse = serde_json::from_str(response_json).unwrap();
        let expected = response.posts;

        let _m = mock("GET", "/posts.json?tags=id%3A8595,535,2105,1470")
            .with_body(response_json)
            .create();

        assert_eq!(
            client
                .get_posts(&[8595, 535, 2105, 1470])
                .collect::<Vec<_>>()
                .await,
            expected.into_iter().map(|p| Ok(p)).collect::<Vec<_>>(),
        );
    }
}