Skip to main content

ghl_sdk/services/
emails.rs

1// @generated by xtask/generate_services.py — do not edit by hand.
2//! `emails` — typed methods for all 5 API v2 operations
3//! in this module.
4//!
5//! Access via [`Ghl::emails`](crate::Ghl::emails).
6//!
7//! Request and response types come from [`ghl_models::v2::emails`](https://docs.rs/ghl-models/latest/ghl_models/v2/emails/); every endpoint is also documented in the
8//! [`emails` API reference](https://github.com/Shahroz/ghl-rs/blob/main/docs/api/emails.md).
9//!
10//! Enable with `features = ["emails"]`.
11
12#![allow(clippy::too_many_arguments)]
13
14use crate::client::Ghl;
15use crate::error::Result;
16use ghl_models::v2::emails as models;
17
18/// Typed access to the `emails` API v2 surface (5 operations). Obtained via
19/// [`Ghl::emails`](crate::Ghl::emails).
20#[derive(Debug, Clone)]
21pub struct EmailsService {
22    pub(crate) client: Ghl,
23}
24
25impl EmailsService {
26    pub(crate) fn new(client: Ghl) -> Self {
27        Self { client }
28    }
29}
30
31/// Query parameters for [`EmailsService::fetch_email_templates`].
32#[derive(Debug, Clone, Default)]
33pub struct FetchEmailTemplatesParams {
34    /// `locationId` query parameter.
35    /// Required by the API.
36    pub location_id: String,
37    /// `limit` query parameter.
38    pub limit: Option<String>,
39    /// `offset` query parameter.
40    pub offset: Option<String>,
41    /// `search` query parameter.
42    pub search: Option<String>,
43    /// `sortByDate` query parameter.
44    pub sort_by_date: Option<String>,
45    /// `archived` query parameter.
46    pub archived: Option<String>,
47    /// `builderVersion` query parameter.
48    /// Allowed values: `1`, `2`.
49    pub builder_version: Option<String>,
50    /// `name` query parameter.
51    pub name: Option<String>,
52    /// `parentId` query parameter.
53    pub parent_id: Option<String>,
54    /// `originId` query parameter.
55    pub origin_id: Option<String>,
56    /// `templatesOnly` query parameter.
57    pub templates_only: Option<String>,
58}
59
60impl FetchEmailTemplatesParams {
61    /// Start from the parameters the API requires.
62    pub fn new(location_id: impl Into<String>) -> Self {
63        Self {
64            location_id: location_id.into(),
65            ..Default::default()
66        }
67    }
68
69    /// Set the `limit` query parameter.
70    pub fn limit(mut self, v: impl Into<String>) -> Self {
71        self.limit = Some(v.into());
72        self
73    }
74
75    /// Set the `offset` query parameter.
76    pub fn offset(mut self, v: impl Into<String>) -> Self {
77        self.offset = Some(v.into());
78        self
79    }
80
81    /// Set the `search` query parameter.
82    pub fn search(mut self, v: impl Into<String>) -> Self {
83        self.search = Some(v.into());
84        self
85    }
86
87    /// Set the `sortByDate` query parameter.
88    pub fn sort_by_date(mut self, v: impl Into<String>) -> Self {
89        self.sort_by_date = Some(v.into());
90        self
91    }
92
93    /// Set the `archived` query parameter.
94    pub fn archived(mut self, v: impl Into<String>) -> Self {
95        self.archived = Some(v.into());
96        self
97    }
98
99    /// Set the `builderVersion` query parameter.
100    pub fn builder_version(mut self, v: impl Into<String>) -> Self {
101        self.builder_version = Some(v.into());
102        self
103    }
104
105    /// Set the `name` query parameter.
106    pub fn name(mut self, v: impl Into<String>) -> Self {
107        self.name = Some(v.into());
108        self
109    }
110
111    /// Set the `parentId` query parameter.
112    pub fn parent_id(mut self, v: impl Into<String>) -> Self {
113        self.parent_id = Some(v.into());
114        self
115    }
116
117    /// Set the `originId` query parameter.
118    pub fn origin_id(mut self, v: impl Into<String>) -> Self {
119        self.origin_id = Some(v.into());
120        self
121    }
122
123    /// Set the `templatesOnly` query parameter.
124    pub fn templates_only(mut self, v: impl Into<String>) -> Self {
125        self.templates_only = Some(v.into());
126        self
127    }
128
129    fn to_query(&self) -> Vec<(String, String)> {
130        let mut q: Vec<(String, String)> = vec![("locationId".into(), self.location_id.clone())];
131        if let Some(v) = &self.limit {
132            q.push(("limit".into(), v.to_string()));
133        }
134        if let Some(v) = &self.offset {
135            q.push(("offset".into(), v.to_string()));
136        }
137        if let Some(v) = &self.search {
138            q.push(("search".into(), v.to_string()));
139        }
140        if let Some(v) = &self.sort_by_date {
141            q.push(("sortByDate".into(), v.to_string()));
142        }
143        if let Some(v) = &self.archived {
144            q.push(("archived".into(), v.to_string()));
145        }
146        if let Some(v) = &self.builder_version {
147            q.push(("builderVersion".into(), v.to_string()));
148        }
149        if let Some(v) = &self.name {
150            q.push(("name".into(), v.to_string()));
151        }
152        if let Some(v) = &self.parent_id {
153            q.push(("parentId".into(), v.to_string()));
154        }
155        if let Some(v) = &self.origin_id {
156            q.push(("originId".into(), v.to_string()));
157        }
158        if let Some(v) = &self.templates_only {
159            q.push(("templatesOnly".into(), v.to_string()));
160        }
161        q
162    }
163}
164
165/// Query parameters for [`EmailsService::get_campaigns`].
166#[derive(Debug, Clone, Default)]
167pub struct GetCampaignsParams {
168    /// Location ID to fetch campaigns from
169    /// Required by the API.
170    pub location_id: String,
171    /// Maximum number of campaigns to return. Defaults to 10, maximum is 100
172    pub limit: Option<f64>,
173    /// Number of campaigns to skip for pagination
174    pub offset: Option<f64>,
175    /// Filter by schedule status
176    /// Allowed values: `active`, `pause`, `complete`, `cancelled`, `retry`, `draft`,
177    /// `resend-scheduled`.
178    pub status: Option<String>,
179    /// Filter by email delivery status
180    /// Allowed values: `all`, `not-started`, `paused`, `cancelled`, `processing`,
181    /// `resumed`, `next-drip`, `complete`, `success`, `error`.
182    pub email_status: Option<String>,
183    /// Filter campaigns by name
184    pub name: Option<String>,
185    /// Filter campaigns by parent folder ID
186    pub parent_id: Option<String>,
187    /// When true, returns only essential campaign fields like id, templateDataDownloadUrl,
188    /// updatedAt, type, templateType, templateId, downloadUrl and isPlainText. When false,
189    /// returns complete campaign data including meta information, bulkRequestStatusInfo,
190    /// ABTestInfo, resendScheduleInfo and all other campaign properties
191    pub limited_fields: Option<bool>,
192    /// Filter archived campaigns
193    pub archived: Option<bool>,
194    /// Return only campaigns, excluding folders
195    pub campaigns_only: Option<bool>,
196    /// When true, returns campaign statistics including delivered count, opened count,
197    /// clicked count and revenue if available for the campaign. When false, returns
198    /// campaign data without statistics.
199    pub show_stats: Option<bool>,
200}
201
202impl GetCampaignsParams {
203    /// Start from the parameters the API requires.
204    pub fn new(location_id: impl Into<String>) -> Self {
205        Self {
206            location_id: location_id.into(),
207            ..Default::default()
208        }
209    }
210
211    /// Maximum number of campaigns to return. Defaults to 10, maximum is 100
212    pub fn limit(mut self, v: f64) -> Self {
213        self.limit = Some(v);
214        self
215    }
216
217    /// Number of campaigns to skip for pagination
218    pub fn offset(mut self, v: f64) -> Self {
219        self.offset = Some(v);
220        self
221    }
222
223    /// Filter by schedule status
224    pub fn status(mut self, v: impl Into<String>) -> Self {
225        self.status = Some(v.into());
226        self
227    }
228
229    /// Filter by email delivery status
230    pub fn email_status(mut self, v: impl Into<String>) -> Self {
231        self.email_status = Some(v.into());
232        self
233    }
234
235    /// Filter campaigns by name
236    pub fn name(mut self, v: impl Into<String>) -> Self {
237        self.name = Some(v.into());
238        self
239    }
240
241    /// Filter campaigns by parent folder ID
242    pub fn parent_id(mut self, v: impl Into<String>) -> Self {
243        self.parent_id = Some(v.into());
244        self
245    }
246
247    /// When true, returns only essential campaign fields like id, templateDataDownloadUrl,
248    /// updatedAt, type, templateType, templateId, downloadUrl and isPlainText. When false,
249    /// returns complete campaign data including meta information, bulkRequestStatusInfo,
250    /// ABTestInfo, resendScheduleInfo and all other campaign properties
251    pub fn limited_fields(mut self, v: bool) -> Self {
252        self.limited_fields = Some(v);
253        self
254    }
255
256    /// Filter archived campaigns
257    pub fn archived(mut self, v: bool) -> Self {
258        self.archived = Some(v);
259        self
260    }
261
262    /// Return only campaigns, excluding folders
263    pub fn campaigns_only(mut self, v: bool) -> Self {
264        self.campaigns_only = Some(v);
265        self
266    }
267
268    /// When true, returns campaign statistics including delivered count, opened count,
269    /// clicked count and revenue if available for the campaign. When false, returns
270    /// campaign data without statistics.
271    pub fn show_stats(mut self, v: bool) -> Self {
272        self.show_stats = Some(v);
273        self
274    }
275
276    fn to_query(&self) -> Vec<(String, String)> {
277        let mut q: Vec<(String, String)> = vec![("locationId".into(), self.location_id.clone())];
278        if let Some(v) = &self.limit {
279            q.push(("limit".into(), v.to_string()));
280        }
281        if let Some(v) = &self.offset {
282            q.push(("offset".into(), v.to_string()));
283        }
284        if let Some(v) = &self.status {
285            q.push(("status".into(), v.to_string()));
286        }
287        if let Some(v) = &self.email_status {
288            q.push(("emailStatus".into(), v.to_string()));
289        }
290        if let Some(v) = &self.name {
291            q.push(("name".into(), v.to_string()));
292        }
293        if let Some(v) = &self.parent_id {
294            q.push(("parentId".into(), v.to_string()));
295        }
296        if let Some(v) = &self.limited_fields {
297            q.push(("limitedFields".into(), v.to_string()));
298        }
299        if let Some(v) = &self.archived {
300            q.push(("archived".into(), v.to_string()));
301        }
302        if let Some(v) = &self.campaigns_only {
303            q.push(("campaignsOnly".into(), v.to_string()));
304        }
305        if let Some(v) = &self.show_stats {
306            q.push(("showStats".into(), v.to_string()));
307        }
308        q
309    }
310}
311
312impl EmailsService {
313    /// Fetch email templates
314    ///
315    /// Fetch email templates by location id
316    ///
317    /// `GET /emails/builder`
318    ///
319    /// Requires scope: `emails/builder.readonly`.
320    pub async fn fetch_email_templates(
321        &self,
322        params: &FetchEmailTemplatesParams,
323    ) -> Result<models::FetchBuilderSuccesfulResponseDto> {
324        let query = params.to_query();
325        self.client
326            .send_versioned(
327                reqwest::Method::GET,
328                "/emails/builder",
329                &query,
330                None::<&()>,
331                Some("2021-07-28"),
332            )
333            .await
334    }
335
336    /// Create a new template
337    ///
338    /// `POST /emails/builder`
339    ///
340    /// Requires scope: `emails/builder.write`.
341    pub async fn create_a_new_template(
342        &self,
343        body: &models::CreateBuilderDto,
344    ) -> Result<models::CreateBuilderSuccesfulResponseDto> {
345        let query = Vec::new();
346        self.client
347            .send_versioned(
348                reqwest::Method::POST,
349                "/emails/builder",
350                &query,
351                Some(body),
352                Some("2021-07-28"),
353            )
354            .await
355    }
356
357    /// Update a template
358    ///
359    /// `POST /emails/builder/data`
360    ///
361    /// Requires scope: `emails/builder.write`.
362    pub async fn update_a_template(
363        &self,
364        body: &models::SaveBuilderDataDto,
365    ) -> Result<models::BuilderUpdateSuccessfulDTO> {
366        let query = Vec::new();
367        self.client
368            .send_versioned(
369                reqwest::Method::POST,
370                "/emails/builder/data",
371                &query,
372                Some(body),
373                Some("2021-07-28"),
374            )
375            .await
376    }
377
378    /// Delete a template
379    ///
380    /// `DELETE /emails/builder/{locationId}/{templateId}`
381    pub async fn delete_a_template(
382        &self,
383        location_id: &str,
384        template_id: &str,
385    ) -> Result<models::DeleteBuilderSuccesfulResponseDto> {
386        let path = format!(
387            "/emails/builder/{}/{}",
388            crate::services::encode(location_id),
389            crate::services::encode(template_id)
390        );
391        let query = Vec::new();
392        self.client
393            .send_versioned(
394                reqwest::Method::DELETE,
395                &path,
396                &query,
397                None::<&()>,
398                Some("2021-07-28"),
399            )
400            .await
401    }
402
403    /// Get Campaigns
404    ///
405    /// `GET /emails/schedule`
406    ///
407    /// Requires scope: `emails/schedule.readonly`.
408    pub async fn get_campaigns(
409        &self,
410        params: &GetCampaignsParams,
411    ) -> Result<models::ScheduleFetchSuccessfulDTO> {
412        let query = params.to_query();
413        self.client
414            .send_versioned(
415                reqwest::Method::GET,
416                "/emails/schedule",
417                &query,
418                None::<&()>,
419                Some("2021-07-28"),
420            )
421            .await
422    }
423}