Skip to main content

agentmail/client/
apps.rs

1use crate::client::NoBody;
2use crate::{Client, Error, Page, types::*, util::urlish};
3
4impl Client {
5    /// GET /v0/apps, one page of the app directory.
6    pub async fn list_apps(&self, filters: AppsListFilters) -> Result<AppList, Error> {
7        self.request(
8            reqwest::Method::GET,
9            "/v0/apps",
10            &filters.query(),
11            None::<&NoBody>,
12        )
13        .await
14    }
15
16    /// Every app, draining pagination.
17    pub async fn list_all_apps(&self) -> Result<Vec<App>, Error> {
18        let mut out = Vec::new();
19        let mut token = None;
20        loop {
21            let resp = self
22                .list_apps(AppsListFilters {
23                    limit: None,
24                    page_token: token,
25                    category: None,
26                })
27                .await?;
28            let next = resp.next_page_token;
29            out.extend(resp.apps);
30            match next {
31                Some(t) => token = Some(t),
32                None => return Ok(out),
33            }
34        }
35    }
36
37    /// GET /v0/apps/search, full-text search of the app directory (no
38    /// pagination cursor).
39    pub async fn search_apps(&self, q: &str, limit: Option<u32>) -> Result<AppSearchList, Error> {
40        let mut query = Vec::new();
41        query.push(("q", q.to_string()));
42        if let Some(limit) = limit {
43            query.push(("limit", limit.to_string()));
44        }
45        self.request(
46            reqwest::Method::GET,
47            "/v0/apps/search",
48            &query,
49            None::<&NoBody>,
50        )
51        .await
52    }
53
54    /// GET /v0/apps/{app_id}.
55    pub async fn get_app(&self, app_id: &str) -> Result<App, Error> {
56        self.request(
57            reqwest::Method::GET,
58            &format!("/v0/apps/{}", urlish(app_id)),
59            &[],
60            None::<&NoBody>,
61        )
62        .await
63    }
64
65    /// GET /v0/apps/{app_id}/accounts, one page of the humans connected to
66    /// this app.
67    pub async fn list_app_accounts(
68        &self,
69        app_id: &str,
70        page: Page,
71    ) -> Result<AppAccountList, Error> {
72        self.request(
73            reqwest::Method::GET,
74            &format!("/v0/apps/{}/accounts", urlish(app_id)),
75            &page.query(),
76            None::<&NoBody>,
77        )
78        .await
79    }
80
81    /// POST /v0/apps/{app_id}/connect, start a human's connection to an app.
82    /// The human opens [`ConnectAppResult::magic_url`] to approve before it
83    /// expires.
84    pub async fn connect_app(
85        &self,
86        app_id: &str,
87        connect: ConnectApp,
88    ) -> Result<ConnectAppResult, Error> {
89        self.request(
90            reqwest::Method::POST,
91            &format!("/v0/apps/{}/connect", urlish(app_id)),
92            &[],
93            Some(&connect),
94        )
95        .await
96    }
97}