Skip to main content

archive_it_client/
public.rs

1use futures_core::Stream;
2use serde::Serialize;
3
4use crate::http::{Transport, paginated};
5use crate::models::public::{PublicAccount, PublicCollection};
6use crate::{Config, Error, PageOpts};
7
8pub struct PublicClient {
9    transport: Transport,
10}
11
12impl PublicClient {
13    pub fn new() -> Result<Self, Error> {
14        Self::with_config(Config::api())
15    }
16
17    pub fn with_config(cfg: Config) -> Result<Self, Error> {
18        Ok(Self {
19            transport: Transport::new(cfg, None)?,
20        })
21    }
22
23    pub async fn list_accounts(&self, opts: PageOpts) -> Result<Vec<PublicAccount>, Error> {
24        self.transport.get_json("account", &opts).await
25    }
26
27    pub async fn get_account(&self, id: u64) -> Result<PublicAccount, Error> {
28        self.transport.get_json(&format!("account/{id}"), &()).await
29    }
30
31    pub async fn list_collections(
32        &self,
33        account_id: Option<u64>,
34        opts: PageOpts,
35    ) -> Result<Vec<PublicCollection>, Error> {
36        self.transport
37            .get_json("collection", &CollectionsQuery::new(account_id, opts))
38            .await
39    }
40
41    pub async fn get_collection(&self, id: u64) -> Result<PublicCollection, Error> {
42        self.transport
43            .get_json(&format!("collection/{id}"), &())
44            .await
45    }
46
47    pub fn accounts(&self) -> impl Stream<Item = Result<PublicAccount, Error>> + Send + '_ {
48        paginated(move |opts| self.list_accounts(opts))
49    }
50
51    pub fn collections(
52        &self,
53        account_id: Option<u64>,
54    ) -> impl Stream<Item = Result<PublicCollection, Error>> + Send + '_ {
55        paginated(move |opts| self.list_collections(account_id, opts))
56    }
57}
58
59#[derive(Serialize)]
60pub(crate) struct CollectionsQuery {
61    #[serde(skip_serializing_if = "Option::is_none")]
62    pub(crate) account: Option<u64>,
63    #[serde(flatten)]
64    pub(crate) page: PageOpts,
65}
66
67impl CollectionsQuery {
68    pub(crate) fn new(account: Option<u64>, opts: PageOpts) -> Self {
69        Self {
70            account,
71            page: opts,
72        }
73    }
74}