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
// Copyright 2019 Dmitry Tantsur <divius.inside@gmail.com>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! A stream of resources.

use std::fmt::Debug;

use async_stream::try_stream;
use futures::pin_mut;
use futures::stream::{Stream, TryStreamExt};
use reqwest::RequestBuilder;
use serde::de::DeserializeOwned;
use serde::Serialize;

use super::request;
use super::Error;

/// A single resource.
pub trait Resource {
    /// Type of an ID.
    type Id: Debug + Serialize;

    /// Root type of the listing.
    type Root: DeserializeOwned;

    /// Retrieve a copy of the ID.
    fn resource_id(&self) -> Self::Id;
}

#[derive(Serialize)]
struct Query<T: Serialize> {
    #[serde(skip_serializing_if = "Option::is_none")]
    limit: Option<usize>,
    #[serde(skip_serializing_if = "Option::is_none")]
    marker: Option<T>,
}

fn chunks<T>(
    builder: RequestBuilder,
    limit: Option<usize>,
    starting_with: Option<T::Id>,
) -> impl Stream<Item = Result<Vec<T>, Error>>
where
    T: Resource + Unpin,
    T::Root: Into<Vec<T>> + Send,
{
    let mut marker = starting_with;

    try_stream! {
        loop {
            let prepared = builder
                .try_clone()
                .expect("Builder with a streaming body cannot be used")
                .query(&Query{ limit: limit, marker: marker.take() });
            let result: T::Root = request::fetch_json(prepared).await?;
            let items = result.into();
            if let Some(new_m) = items.last() {
                marker = Some(new_m.resource_id());
                yield items;
            } else {
                break
            }
        }
    }
}

/// Creates a paginated resource stream.
///
/// # Panics
///
/// Will panic during iteration if the request builder has a streaming body.
pub fn paginated<T>(
    builder: RequestBuilder,
    limit: Option<usize>,
    starting_with: Option<T::Id>,
) -> impl Stream<Item = Result<T, Error>>
where
    T: Resource + Unpin,
    T::Root: Into<Vec<T>> + Send,
{
    try_stream! {
        let iter = chunks(builder, limit, starting_with);
        pin_mut!(iter);
        while let Some(chunk) = iter.try_next().await? {
            for item in chunk {
                yield item;
            }
        }
    }
}