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
147
148
149
150
151
152
153
154
use std::sync::Arc;
use serde::Serialize;
use serde_json::{Map, Value};
use crate::client::Inner;
use crate::error::Result;
use crate::params;
/// The `contacts` resource. Reached through
/// [`Mailtea::contacts`](crate::Mailtea::contacts).
///
/// Audience resources are scoped to a publication — pass `publication_id`.
#[derive(Clone, Debug)]
pub struct Contacts {
inner: Arc<Inner>,
}
impl Contacts {
pub(crate) fn new(inner: Arc<Inner>) -> Self {
Self { inner }
}
/// `POST /v1/contacts` — create a contact, or update it if the email
/// already exists in the publication (the endpoint upserts).
/// [`upsert`](Self::upsert) is the same call under the name of what it does.
pub async fn create(&self, params: impl Serialize) -> Result<Value> {
self.inner
.call("POST", "/v1/contacts", crate::params::to_body(params)?)
.await
}
/// Create the contact or update it in place — an alias of
/// [`create`](Self::create), named for what `POST /v1/contacts` does.
pub async fn upsert(&self, params: impl Serialize) -> Result<Value> {
self.create(params).await
}
/// `GET /v1/contacts` — list contacts, cursor-paginated. Filters:
/// `publication_id` (required), `status`
/// (`active`/`unsubscribed`/`suppressed`), `search` (matches the email
/// address), `limit`, `after` (cursor from a previous `next_cursor`).
pub async fn list(&self, params: impl Serialize) -> Result<Value> {
self.inner
.call(
"GET",
&format!("/v1/contacts{}", params::query(params)?),
None,
)
.await
}
/// `GET /v1/contacts/:id_or_email` — one contact, by id or email address.
pub async fn get(&self, id_or_email: &str, params: impl Serialize) -> Result<Value> {
self.inner
.call(
"GET",
&format!(
"/v1/contacts/{}{}",
crate::params::encode(id_or_email),
crate::params::query(params)?
),
None,
)
.await
}
/// `PATCH /v1/contacts/:id_or_email` — update a contact. `publication_id`
/// rides in both the query and the body, which is what the endpoint expects.
pub async fn update(&self, id_or_email: &str, params: impl Serialize) -> Result<Value> {
let payload = crate::params::to_value(params)?;
self.inner
.call(
"PATCH",
&format!(
"/v1/contacts/{}{}",
crate::params::encode(id_or_email),
crate::params::query_pick(&payload, &["publication_id"])
),
crate::params::body_from_value(&payload)?,
)
.await
}
/// `DELETE /v1/contacts/:id_or_email` — remove a contact. Requires
/// `publication_id`.
pub async fn delete(&self, id_or_email: &str, params: impl Serialize) -> Result<Value> {
self.inner
.call(
"DELETE",
&format!(
"/v1/contacts/{}{}",
crate::params::encode(id_or_email),
crate::params::query(params)?
),
None,
)
.await
}
}
/// The payload for [`Contacts::create`] / [`Contacts::upsert`].
#[derive(Clone, Debug, Default, Serialize)]
pub struct CreateContact {
pub publication_id: String,
pub email: String,
/// `active`, `unsubscribed` or `suppressed`. Omitted means `active`.
#[serde(skip_serializing_if = "Option::is_none")]
pub status: Option<String>,
/// Anything this struct does not name yet.
#[serde(flatten)]
pub extra: Map<String, Value>,
}
impl CreateContact {
pub fn new(publication_id: impl Into<String>, email: impl Into<String>) -> Self {
Self {
publication_id: publication_id.into(),
email: email.into(),
status: None,
extra: Map::new(),
}
}
pub fn status(mut self, status: impl Into<String>) -> Self {
self.status = Some(status.into());
self
}
}
/// The payload for [`Contacts::update`].
#[derive(Clone, Debug, Default, Serialize)]
pub struct UpdateContact {
pub publication_id: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub status: Option<String>,
#[serde(flatten)]
pub extra: Map<String, Value>,
}
impl UpdateContact {
pub fn new(publication_id: impl Into<String>) -> Self {
Self {
publication_id: publication_id.into(),
status: None,
extra: Map::new(),
}
}
pub fn status(mut self, status: impl Into<String>) -> Self {
self.status = Some(status.into());
self
}
}