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
use crate::client::NoBody;
use crate::client::scope::{Inboxes, Scoped};
use crate::{Error, Page, types::*, util::urlish};
impl<S: Inboxes> Scoped<'_, S> {
/// POST `{scope}/inboxes`, a new agent-owned email address. At [`Client::org`](crate::Client::org)
/// this creates a standalone inbox; at [`Client::pod`](crate::Client::pod) it creates the inbox
/// inside that pod. Free plans get `{username}@agentmail.to`; custom domains
/// must be verified first.
pub async fn create_inbox(&self, inbox: CreateInbox) -> Result<Inbox, Error> {
self.client
.request(
reqwest::Method::POST,
&format!("{}/inboxes", self.base()),
&[],
Some(&inbox),
)
.await
}
/// GET `{scope}/inboxes`, one page.
pub async fn list_inboxes(&self, page: Page) -> Result<InboxList, Error> {
self.client
.request(
reqwest::Method::GET,
&format!("{}/inboxes", self.base()),
&page.query(),
None::<&NoBody>,
)
.await
}
/// Every inbox in the scope, draining pagination.
pub async fn list_all_inboxes(&self) -> Result<Vec<Inbox>, Error> {
let mut out = Vec::new();
let mut token = None;
loop {
let resp = self
.list_inboxes(Page {
limit: None,
page_token: token,
})
.await?;
let next = resp.next_page_token;
out.extend(resp.inboxes);
match next {
Some(t) => token = Some(t),
None => return Ok(out),
}
}
}
/// GET `{scope}/inboxes/{inbox_id}`.
pub async fn get_inbox(&self, inbox_id: &str) -> Result<Inbox, Error> {
self.client
.request(
reqwest::Method::GET,
&format!("{}/inboxes/{}", self.base(), urlish(inbox_id)),
&[],
None::<&NoBody>,
)
.await
}
/// PATCH `{scope}/inboxes/{inbox_id}`, update display name and/or metadata.
pub async fn update_inbox(&self, inbox_id: &str, update: UpdateInbox) -> Result<Inbox, Error> {
self.client
.request(
reqwest::Method::PATCH,
&format!("{}/inboxes/{}", self.base(), urlish(inbox_id)),
&[],
Some(&update),
)
.await
}
/// DELETE `{scope}/inboxes/{inbox_id}`.
pub async fn delete_inbox(&self, inbox_id: &str) -> Result<(), Error> {
self.client
.request(
reqwest::Method::DELETE,
&format!("{}/inboxes/{}", self.base(), urlish(inbox_id)),
&[],
None::<&NoBody>,
)
.await
}
/// GET `{scope}/inboxes/search`, full-text search over the scope's
/// inboxes (display name, address, metadata).
pub async fn search_inboxes(&self, q: &str, page: Page) -> Result<InboxList, Error> {
let mut query = page.query();
query.push(("q", q.to_string()));
self.client
.request(
reqwest::Method::GET,
&format!("{}/inboxes/search", self.base()),
&query,
None::<&NoBody>,
)
.await
}
/// Every inbox matching `q`, draining pagination.
pub async fn list_all_matching_inboxes(&self, q: &str) -> Result<Vec<Inbox>, Error> {
let mut out = Vec::new();
let mut token = None;
loop {
let resp = self
.search_inboxes(
q,
Page {
limit: None,
page_token: token,
},
)
.await?;
let next = resp.next_page_token;
out.extend(resp.inboxes);
match next {
Some(t) => token = Some(t),
None => return Ok(out),
}
}
}
}
impl Scoped<'_, crate::client::scope::InboxScope<'_>> {
/// POST `/v0/inboxes/{inbox_id}/authorize`, complete the browser
/// authorization flow for this inbox: the human approves in their browser,
/// and this call exchanges the resulting `auth_token` for a scoped API
/// key ([`AuthorizeInboxResult::api_key_id`]).
pub async fn authorize(
&self,
authorize: AuthorizeInbox,
) -> Result<AuthorizeInboxResult, Error> {
self.client
.request(
reqwest::Method::POST,
&format!("/v0/inboxes/{}/authorize", urlish(self.scope.0)),
&[],
Some(&authorize),
)
.await
}
}