ferrtable 0.0.2

Ferris the crab's favorite Airtable library
Documentation
use std::{collections::VecDeque, pin::Pin};

use derive_builder::Builder;
use futures::prelude::*;
use serde::{Deserialize, Serialize};

use crate::{
    client::Client,
    errors::ExecutionError,
    pagination::{PaginatedQuery, PaginatedResponse, execute_paginated},
};

#[derive(Builder, Clone, Debug, Serialize)]
#[builder(pattern = "owned", setter(prefix = "with"))]
pub struct ListBasesQuery {
    #[serde(skip)]
    #[builder(vis = "pub(crate)")]
    client: Client,

    /// To fetch the next page of records, include offset from the previous
    /// request in the next request's parameters.
    #[builder(default, private)]
    offset: Option<String>,
}

impl PaginatedQuery<Base, ListBasesResponse> for ListBasesQuery {
    fn get_offset(&self) -> Option<String> {
        self.offset.clone()
    }

    fn set_offset(&mut self, value: Option<String>) {
        self.offset = value
    }

    fn get_req_builder(&self) -> reqwest::RequestBuilder {
        self.client
            .get_path("v0/meta/bases")
            .query(&[("offset", self.offset.clone())])
    }
}

impl ListBasesQuery {
    pub fn stream_items(self) -> Pin<Box<impl Stream<Item = Result<Base, ExecutionError>>>> {
        execute_paginated::<Base, ListBasesResponse>(self)
    }
}

#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
pub struct Base {
    /// Base ID, a unique identifier for a base.
    pub id: String,

    pub name: String,

    #[serde(rename = "permissionLevel")]
    pub permission_level: String,
}

#[derive(Clone, Deserialize)]
struct ListBasesResponse {
    /// If there are more records, the response will contain an offset. Pass
    /// this offset into the next request to fetch the next page of records.
    offset: Option<String>,

    bases: VecDeque<Base>,
}

impl PaginatedResponse<Base> for ListBasesResponse {
    fn get_offset(&self) -> Option<String> {
        self.offset.clone()
    }

    fn get_items(&self) -> VecDeque<Base> {
        self.bases.clone()
    }
}