ghl-sdk 0.5.1

Unofficial async Rust SDK for the GoHighLevel (HighLevel) API 2.0 — OAuth 2.0, Private Integration Tokens, rate-limit-aware retries, paginated streams
Documentation
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
// @generated by xtask/generate_services.py — do not edit by hand.
//! `emails` — typed methods for all 5 API v2 operations
//! in this module.
//!
//! Access via [`Ghl::emails`](crate::Ghl::emails).
//!
//! 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
//! [`emails` API reference](https://github.com/Shahroz/ghl-rs/blob/main/docs/api/emails.md).
//!
//! Enable with `features = ["emails"]`.

#![allow(clippy::too_many_arguments)]

use crate::client::Ghl;
use crate::error::Result;
use ghl_models::v2::emails as models;

/// Typed access to the `emails` API v2 surface (5 operations). Obtained via
/// [`Ghl::emails`](crate::Ghl::emails).
#[derive(Debug, Clone)]
pub struct EmailsService {
    pub(crate) client: Ghl,
}

impl EmailsService {
    pub(crate) fn new(client: Ghl) -> Self {
        Self { client }
    }
}

/// Query parameters for [`EmailsService::fetch_email_templates`].
#[derive(Debug, Clone, Default)]
pub struct FetchEmailTemplatesParams {
    /// `locationId` query parameter.
    /// Required by the API.
    pub location_id: String,
    /// `limit` query parameter.
    pub limit: Option<String>,
    /// `offset` query parameter.
    pub offset: Option<String>,
    /// `search` query parameter.
    pub search: Option<String>,
    /// `sortByDate` query parameter.
    pub sort_by_date: Option<String>,
    /// `archived` query parameter.
    pub archived: Option<String>,
    /// `builderVersion` query parameter.
    /// Allowed values: `1`, `2`.
    pub builder_version: Option<String>,
    /// `name` query parameter.
    pub name: Option<String>,
    /// `parentId` query parameter.
    pub parent_id: Option<String>,
    /// `originId` query parameter.
    pub origin_id: Option<String>,
    /// `templatesOnly` query parameter.
    pub templates_only: Option<String>,
}

impl FetchEmailTemplatesParams {
    /// Start from the parameters the API requires.
    pub fn new(location_id: impl Into<String>) -> Self {
        Self {
            location_id: location_id.into(),
            ..Default::default()
        }
    }

    /// Set the `limit` query parameter.
    pub fn limit(mut self, v: impl Into<String>) -> Self {
        self.limit = Some(v.into());
        self
    }

    /// Set the `offset` query parameter.
    pub fn offset(mut self, v: impl Into<String>) -> Self {
        self.offset = Some(v.into());
        self
    }

    /// Set the `search` query parameter.
    pub fn search(mut self, v: impl Into<String>) -> Self {
        self.search = Some(v.into());
        self
    }

    /// Set the `sortByDate` query parameter.
    pub fn sort_by_date(mut self, v: impl Into<String>) -> Self {
        self.sort_by_date = Some(v.into());
        self
    }

    /// Set the `archived` query parameter.
    pub fn archived(mut self, v: impl Into<String>) -> Self {
        self.archived = Some(v.into());
        self
    }

    /// Set the `builderVersion` query parameter.
    pub fn builder_version(mut self, v: impl Into<String>) -> Self {
        self.builder_version = Some(v.into());
        self
    }

    /// Set the `name` query parameter.
    pub fn name(mut self, v: impl Into<String>) -> Self {
        self.name = Some(v.into());
        self
    }

    /// Set the `parentId` query parameter.
    pub fn parent_id(mut self, v: impl Into<String>) -> Self {
        self.parent_id = Some(v.into());
        self
    }

    /// Set the `originId` query parameter.
    pub fn origin_id(mut self, v: impl Into<String>) -> Self {
        self.origin_id = Some(v.into());
        self
    }

    /// Set the `templatesOnly` query parameter.
    pub fn templates_only(mut self, v: impl Into<String>) -> Self {
        self.templates_only = Some(v.into());
        self
    }

    fn to_query(&self) -> Vec<(String, String)> {
        let mut q: Vec<(String, String)> = vec![("locationId".into(), self.location_id.clone())];
        if let Some(v) = &self.limit {
            q.push(("limit".into(), v.to_string()));
        }
        if let Some(v) = &self.offset {
            q.push(("offset".into(), v.to_string()));
        }
        if let Some(v) = &self.search {
            q.push(("search".into(), v.to_string()));
        }
        if let Some(v) = &self.sort_by_date {
            q.push(("sortByDate".into(), v.to_string()));
        }
        if let Some(v) = &self.archived {
            q.push(("archived".into(), v.to_string()));
        }
        if let Some(v) = &self.builder_version {
            q.push(("builderVersion".into(), v.to_string()));
        }
        if let Some(v) = &self.name {
            q.push(("name".into(), v.to_string()));
        }
        if let Some(v) = &self.parent_id {
            q.push(("parentId".into(), v.to_string()));
        }
        if let Some(v) = &self.origin_id {
            q.push(("originId".into(), v.to_string()));
        }
        if let Some(v) = &self.templates_only {
            q.push(("templatesOnly".into(), v.to_string()));
        }
        q
    }
}

/// Query parameters for [`EmailsService::get_campaigns`].
#[derive(Debug, Clone, Default)]
pub struct GetCampaignsParams {
    /// Location ID to fetch campaigns from
    /// Required by the API.
    pub location_id: String,
    /// Maximum number of campaigns to return. Defaults to 10, maximum is 100
    pub limit: Option<f64>,
    /// Number of campaigns to skip for pagination
    pub offset: Option<f64>,
    /// Filter by schedule status
    /// Allowed values: `active`, `pause`, `complete`, `cancelled`, `retry`, `draft`,
    /// `resend-scheduled`.
    pub status: Option<String>,
    /// Filter by email delivery status
    /// Allowed values: `all`, `not-started`, `paused`, `cancelled`, `processing`,
    /// `resumed`, `next-drip`, `complete`, `success`, `error`.
    pub email_status: Option<String>,
    /// Filter campaigns by name
    pub name: Option<String>,
    /// Filter campaigns by parent folder ID
    pub parent_id: Option<String>,
    /// When true, returns only essential campaign fields like id, templateDataDownloadUrl,
    /// updatedAt, type, templateType, templateId, downloadUrl and isPlainText. When false,
    /// returns complete campaign data including meta information, bulkRequestStatusInfo,
    /// ABTestInfo, resendScheduleInfo and all other campaign properties
    pub limited_fields: Option<bool>,
    /// Filter archived campaigns
    pub archived: Option<bool>,
    /// Return only campaigns, excluding folders
    pub campaigns_only: Option<bool>,
    /// When true, returns campaign statistics including delivered count, opened count,
    /// clicked count and revenue if available for the campaign. When false, returns
    /// campaign data without statistics.
    pub show_stats: Option<bool>,
}

impl GetCampaignsParams {
    /// Start from the parameters the API requires.
    pub fn new(location_id: impl Into<String>) -> Self {
        Self {
            location_id: location_id.into(),
            ..Default::default()
        }
    }

    /// Maximum number of campaigns to return. Defaults to 10, maximum is 100
    pub fn limit(mut self, v: f64) -> Self {
        self.limit = Some(v);
        self
    }

    /// Number of campaigns to skip for pagination
    pub fn offset(mut self, v: f64) -> Self {
        self.offset = Some(v);
        self
    }

    /// Filter by schedule status
    pub fn status(mut self, v: impl Into<String>) -> Self {
        self.status = Some(v.into());
        self
    }

    /// Filter by email delivery status
    pub fn email_status(mut self, v: impl Into<String>) -> Self {
        self.email_status = Some(v.into());
        self
    }

    /// Filter campaigns by name
    pub fn name(mut self, v: impl Into<String>) -> Self {
        self.name = Some(v.into());
        self
    }

    /// Filter campaigns by parent folder ID
    pub fn parent_id(mut self, v: impl Into<String>) -> Self {
        self.parent_id = Some(v.into());
        self
    }

    /// When true, returns only essential campaign fields like id, templateDataDownloadUrl,
    /// updatedAt, type, templateType, templateId, downloadUrl and isPlainText. When false,
    /// returns complete campaign data including meta information, bulkRequestStatusInfo,
    /// ABTestInfo, resendScheduleInfo and all other campaign properties
    pub fn limited_fields(mut self, v: bool) -> Self {
        self.limited_fields = Some(v);
        self
    }

    /// Filter archived campaigns
    pub fn archived(mut self, v: bool) -> Self {
        self.archived = Some(v);
        self
    }

    /// Return only campaigns, excluding folders
    pub fn campaigns_only(mut self, v: bool) -> Self {
        self.campaigns_only = Some(v);
        self
    }

    /// When true, returns campaign statistics including delivered count, opened count,
    /// clicked count and revenue if available for the campaign. When false, returns
    /// campaign data without statistics.
    pub fn show_stats(mut self, v: bool) -> Self {
        self.show_stats = Some(v);
        self
    }

    fn to_query(&self) -> Vec<(String, String)> {
        let mut q: Vec<(String, String)> = vec![("locationId".into(), self.location_id.clone())];
        if let Some(v) = &self.limit {
            q.push(("limit".into(), v.to_string()));
        }
        if let Some(v) = &self.offset {
            q.push(("offset".into(), v.to_string()));
        }
        if let Some(v) = &self.status {
            q.push(("status".into(), v.to_string()));
        }
        if let Some(v) = &self.email_status {
            q.push(("emailStatus".into(), v.to_string()));
        }
        if let Some(v) = &self.name {
            q.push(("name".into(), v.to_string()));
        }
        if let Some(v) = &self.parent_id {
            q.push(("parentId".into(), v.to_string()));
        }
        if let Some(v) = &self.limited_fields {
            q.push(("limitedFields".into(), v.to_string()));
        }
        if let Some(v) = &self.archived {
            q.push(("archived".into(), v.to_string()));
        }
        if let Some(v) = &self.campaigns_only {
            q.push(("campaignsOnly".into(), v.to_string()));
        }
        if let Some(v) = &self.show_stats {
            q.push(("showStats".into(), v.to_string()));
        }
        q
    }
}

impl EmailsService {
    /// Fetch email templates
    ///
    /// Fetch email templates by location id
    ///
    /// `GET /emails/builder`
    ///
    /// Requires scope: `emails/builder.readonly`.
    pub async fn fetch_email_templates(
        &self,
        params: &FetchEmailTemplatesParams,
    ) -> Result<models::FetchBuilderSuccesfulResponseDto> {
        let query = params.to_query();
        self.client
            .send_versioned(
                reqwest::Method::GET,
                "/emails/builder",
                &query,
                None::<&()>,
                Some("2021-07-28"),
            )
            .await
    }

    /// Create a new template
    ///
    /// `POST /emails/builder`
    ///
    /// Requires scope: `emails/builder.write`.
    pub async fn create_a_new_template(
        &self,
        body: &models::CreateBuilderDto,
    ) -> Result<models::CreateBuilderSuccesfulResponseDto> {
        let query = Vec::new();
        self.client
            .send_versioned(
                reqwest::Method::POST,
                "/emails/builder",
                &query,
                Some(body),
                Some("2021-07-28"),
            )
            .await
    }

    /// Update a template
    ///
    /// `POST /emails/builder/data`
    ///
    /// Requires scope: `emails/builder.write`.
    pub async fn update_a_template(
        &self,
        body: &models::SaveBuilderDataDto,
    ) -> Result<models::BuilderUpdateSuccessfulDTO> {
        let query = Vec::new();
        self.client
            .send_versioned(
                reqwest::Method::POST,
                "/emails/builder/data",
                &query,
                Some(body),
                Some("2021-07-28"),
            )
            .await
    }

    /// Delete a template
    ///
    /// `DELETE /emails/builder/{locationId}/{templateId}`
    pub async fn delete_a_template(
        &self,
        location_id: &str,
        template_id: &str,
    ) -> Result<models::DeleteBuilderSuccesfulResponseDto> {
        let path = format!(
            "/emails/builder/{}/{}",
            crate::services::encode(location_id),
            crate::services::encode(template_id)
        );
        let query = Vec::new();
        self.client
            .send_versioned(
                reqwest::Method::DELETE,
                &path,
                &query,
                None::<&()>,
                Some("2021-07-28"),
            )
            .await
    }

    /// Get Campaigns
    ///
    /// `GET /emails/schedule`
    ///
    /// Requires scope: `emails/schedule.readonly`.
    pub async fn get_campaigns(
        &self,
        params: &GetCampaignsParams,
    ) -> Result<models::ScheduleFetchSuccessfulDTO> {
        let query = params.to_query();
        self.client
            .send_versioned(
                reqwest::Method::GET,
                "/emails/schedule",
                &query,
                None::<&()>,
                Some("2021-07-28"),
            )
            .await
    }
}