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
155
156
157
158
159
160
161
162
163
164
use std::sync::Arc;
use serde::Serialize;
use crate::error::Result;
use crate::http::HttpClient;
use crate::resources::{enc, to_value};
use crate::response::{Page, Response};
use crate::transport::Method;
use crate::types::params::ListParams;
/// Operations on the `/templates` endpoints, including the draft/publish flow.
pub struct Templates {
http: Arc<HttpClient>,
}
impl Templates {
pub(crate) fn new(http: Arc<HttpClient>) -> Self {
Self { http }
}
/// List the team's templates, newest-first.
pub async fn list(&self, params: ListParams) -> Result<Page> {
self.http
.list(
"/templates",
vec![
("limit", params.limit.map(|n| n.to_string())),
("after", params.after),
],
)
.await
}
/// List every template, walking all pages.
pub async fn list_all(&self, params: ListParams) -> Result<Vec<Response>> {
self.http
.list_all(
"/templates",
vec![("limit", params.limit.map(|n| n.to_string()))],
)
.await
}
/// Create a template. It starts unpublished — publish it before sending.
pub async fn create(&self, body: impl Serialize) -> Result<Response> {
self.http
.request_object(
Method::Post,
"/templates",
Some(to_value(body)?),
false,
None,
)
.await
}
/// Retrieve a template, including its published content.
pub async fn get(&self, id: &str) -> Result<Response> {
self.http
.request_object(
Method::Get,
&format!("/templates/{}", enc(id)),
None,
false,
None,
)
.await
}
/// Update a template's `name`. Body content lives on the draft.
pub async fn update(&self, id: &str, body: impl Serialize) -> Result<Response> {
self.http
.request_object(
Method::Patch,
&format!("/templates/{}", enc(id)),
Some(to_value(body)?),
false,
None,
)
.await
}
/// Permanently delete a template.
pub async fn delete(&self, id: &str) -> Result<()> {
self.http
.request_empty(Method::Delete, &format!("/templates/{}", enc(id)))
.await
}
/// Copy a template. The copy starts unpublished with a draft seeded from the
/// source's current editable content.
pub async fn duplicate(&self, id: &str) -> Result<Response> {
self.http
.request_object(
Method::Post,
&format!("/templates/{}/duplicate", enc(id)),
None,
false,
None,
)
.await
}
/// Copy a template, overriding fields (e.g. `name`) on the copy.
pub async fn duplicate_with(&self, id: &str, body: impl Serialize) -> Result<Response> {
self.http
.request_object(
Method::Post,
&format!("/templates/{}/duplicate", enc(id)),
Some(to_value(body)?),
false,
None,
)
.await
}
/// Retrieve the template's unpublished draft. Raises `not_found` if none exists.
pub async fn get_draft(&self, id: &str) -> Result<Response> {
self.http
.request_object(
Method::Get,
&format!("/templates/{}/draft", enc(id)),
None,
false,
None,
)
.await
}
/// Create or update the template's draft. Idempotent upsert; published
/// content untouched.
pub async fn update_draft(&self, id: &str, body: impl Serialize) -> Result<Response> {
self.http
.request_object(
Method::Patch,
&format!("/templates/{}/draft", enc(id)),
Some(to_value(body)?),
false,
None,
)
.await
}
/// Discard the template's draft without touching published content.
pub async fn delete_draft(&self, id: &str) -> Result<()> {
self.http
.request_empty(Method::Delete, &format!("/templates/{}/draft", enc(id)))
.await
}
/// Promote the draft into the published slot, consuming the draft.
pub async fn publish(&self, id: &str) -> Result<Response> {
self.http
.request_object(
Method::Post,
&format!("/templates/{}/publish", enc(id)),
None,
false,
None,
)
.await
}
}