sendry 0.2.0

Official Rust crate for the Sendry email API
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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
//! Automations — workflows that run steps for contacts.

use std::collections::HashMap;

use reqwest::Method;
use serde::{Deserialize, Serialize};
use serde_json::Value;

use crate::{client::Sendry, error::Error, DeleteResponse, Page};

/// Automations resource handle.
#[derive(Debug, Clone)]
pub struct Automations {
    client: Sendry,
}

impl Automations {
    pub(crate) fn new(client: Sendry) -> Self {
        Self { client }
    }

    /// List automations.
    pub async fn list(&self, params: ListAutomations) -> Result<Page<Automation>, Error> {
        let q = params.to_query();
        self.client
            .request(
                self.client
                    .build::<()>(Method::GET, "/v1/automations", &q, None),
            )
            .await
    }

    /// Get an automation by id.
    pub async fn get(&self, id: &str) -> Result<Automation, Error> {
        self.client
            .request(self.client.build::<()>(
                Method::GET,
                &format!("/v1/automations/{id}"),
                &[],
                None,
            ))
            .await
    }

    /// Create an automation.
    pub async fn create(&self, params: CreateAutomation) -> Result<Automation, Error> {
        self.client
            .request(
                self.client
                    .build(Method::POST, "/v1/automations", &[], Some(&params)),
            )
            .await
    }

    /// Update an automation (partial).
    pub async fn update(
        &self,
        id: &str,
        params: UpdateAutomation,
    ) -> Result<Automation, Error> {
        self.client
            .request(self.client.build(
                Method::PATCH,
                &format!("/v1/automations/{id}"),
                &[],
                Some(&params),
            ))
            .await
    }

    /// Delete an automation.
    pub async fn delete(&self, id: &str) -> Result<DeleteResponse, Error> {
        self.client
            .request(self.client.build::<()>(
                Method::DELETE,
                &format!("/v1/automations/{id}"),
                &[],
                None,
            ))
            .await
    }

    /// Activate an automation.
    pub async fn activate(&self, id: &str) -> Result<Automation, Error> {
        self.client
            .request(self.client.build::<()>(
                Method::POST,
                &format!("/v1/automations/{id}/activate"),
                &[],
                None,
            ))
            .await
    }

    /// Pause an automation.
    pub async fn pause(&self, id: &str) -> Result<Automation, Error> {
        self.client
            .request(self.client.build::<()>(
                Method::POST,
                &format!("/v1/automations/{id}/pause"),
                &[],
                None,
            ))
            .await
    }

    /// Archive an automation.
    pub async fn archive(&self, id: &str) -> Result<Automation, Error> {
        self.client
            .request(self.client.build::<()>(
                Method::POST,
                &format!("/v1/automations/{id}/archive"),
                &[],
                None,
            ))
            .await
    }

    /// Steps sub-resource.
    #[must_use]
    pub fn steps(&self) -> AutomationSteps {
        AutomationSteps { client: self.client.clone() }
    }

    /// Runs sub-resource.
    #[must_use]
    pub fn runs(&self) -> AutomationRuns {
        AutomationRuns { client: self.client.clone() }
    }
}

/// Automation steps sub-resource handle.
#[derive(Debug, Clone)]
pub struct AutomationSteps {
    client: Sendry,
}

impl AutomationSteps {
    /// List steps for an automation.
    pub async fn list(&self, automation_id: &str) -> Result<AutomationStepList, Error> {
        self.client
            .request(self.client.build::<()>(
                Method::GET,
                &format!("/v1/automations/{automation_id}/steps"),
                &[],
                None,
            ))
            .await
    }

    /// Add a new step.
    pub async fn add(
        &self,
        automation_id: &str,
        params: AddAutomationStep,
    ) -> Result<AutomationStep, Error> {
        self.client
            .request(self.client.build(
                Method::POST,
                &format!("/v1/automations/{automation_id}/steps"),
                &[],
                Some(&params),
            ))
            .await
    }

    /// Update a step.
    pub async fn update(
        &self,
        automation_id: &str,
        step_id: &str,
        params: UpdateAutomationStep,
    ) -> Result<AutomationStep, Error> {
        self.client
            .request(self.client.build(
                Method::PATCH,
                &format!("/v1/automations/{automation_id}/steps/{step_id}"),
                &[],
                Some(&params),
            ))
            .await
    }

    /// Delete a step.
    pub async fn delete(
        &self,
        automation_id: &str,
        step_id: &str,
    ) -> Result<DeleteResponse, Error> {
        self.client
            .request(self.client.build::<()>(
                Method::DELETE,
                &format!("/v1/automations/{automation_id}/steps/{step_id}"),
                &[],
                None,
            ))
            .await
    }
}

/// Automation runs sub-resource handle.
#[derive(Debug, Clone)]
pub struct AutomationRuns {
    client: Sendry,
}

impl AutomationRuns {
    /// List runs for an automation.
    pub async fn list(
        &self,
        automation_id: &str,
        params: ListAutomationRuns,
    ) -> Result<Page<AutomationRun>, Error> {
        let q = params.to_query();
        self.client
            .request(self.client.build::<()>(
                Method::GET,
                &format!("/v1/automations/{automation_id}/runs"),
                &q,
                None,
            ))
            .await
    }

    /// Get a single run.
    pub async fn get(
        &self,
        automation_id: &str,
        run_id: &str,
    ) -> Result<AutomationRun, Error> {
        self.client
            .request(self.client.build::<()>(
                Method::GET,
                &format!("/v1/automations/{automation_id}/runs/{run_id}"),
                &[],
                None,
            ))
            .await
    }

    /// List a run's executed steps.
    pub async fn list_steps(
        &self,
        automation_id: &str,
        run_id: &str,
    ) -> Result<AutomationRunStepList, Error> {
        self.client
            .request(self.client.build::<()>(
                Method::GET,
                &format!("/v1/automations/{automation_id}/runs/{run_id}/steps"),
                &[],
                None,
            ))
            .await
    }

    /// Cancel an in-progress run.
    pub async fn cancel(
        &self,
        automation_id: &str,
        run_id: &str,
    ) -> Result<AutomationRun, Error> {
        self.client
            .request(self.client.build::<()>(
                Method::POST,
                &format!("/v1/automations/{automation_id}/runs/{run_id}/cancel"),
                &[],
                None,
            ))
            .await
    }

    /// Create a manual run for a contact.
    pub async fn create(
        &self,
        automation_id: &str,
        params: CreateAutomationRun,
    ) -> Result<AutomationRun, Error> {
        self.client
            .request(self.client.build(
                Method::POST,
                &format!("/v1/automations/{automation_id}/runs"),
                &[],
                Some(&params),
            ))
            .await
    }
}

// ---------------------------------------------------------------------------
// Types
// ---------------------------------------------------------------------------

/// Automation record.
#[derive(Debug, Clone, Deserialize)]
pub struct Automation {
    /// Automation id.
    pub id: String,
    /// Display name.
    pub name: String,
    /// Description.
    pub description: Option<String>,
    /// `draft`, `active`, `paused`, or `archived`.
    pub status: String,
    /// Trigger type.
    pub trigger_type: String,
    /// Trigger config blob.
    pub trigger_config: Value,
    /// Optional entry segment.
    pub entry_segment_id: Option<String>,
    /// Re-entry policy.
    pub reentry_policy: String,
    /// Cooldown in seconds (when policy = cooldown).
    pub reentry_cooldown_seconds: Option<u64>,
    /// Total runs ever.
    pub total_runs: u64,
    /// Currently active runs.
    pub active_runs: u64,
    /// Completed runs.
    pub completed_runs: u64,
    /// Failed runs.
    pub failed_runs: u64,
    /// Created.
    pub created_at: String,
    /// Updated.
    pub updated_at: String,
}

/// Filters for [`Automations::list`].
#[derive(Debug, Clone, Default)]
pub struct ListAutomations {
    /// Page size.
    pub limit: Option<u32>,
    /// Cursor.
    pub cursor: Option<String>,
    /// Filter by status.
    pub status: Option<String>,
}

impl ListAutomations {
    fn to_query(&self) -> Vec<(&'static str, String)> {
        let mut q = Vec::new();
        if let Some(v) = self.limit {
            q.push(("limit", v.to_string()));
        }
        if let Some(v) = &self.cursor {
            q.push(("cursor", v.clone()));
        }
        if let Some(v) = &self.status {
            q.push(("status", v.clone()));
        }
        q
    }
}

/// Parameters for [`Automations::create`].
#[derive(Debug, Clone, Serialize)]
pub struct CreateAutomation {
    /// Display name.
    pub name: String,
    /// Description.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    /// Trigger type: `event`, `contact_added_to_segment`, `schedule`, `manual`.
    pub trigger_type: String,
    /// Trigger config.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub trigger_config: Option<Value>,
    /// Entry segment.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub entry_segment_id: Option<String>,
    /// Re-entry policy: `once_per_contact`, `cooldown`, `always`.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub reentry_policy: Option<String>,
    /// Cooldown in seconds.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub reentry_cooldown_seconds: Option<u64>,
}

/// Parameters for [`Automations::update`]. All fields optional.
#[derive(Debug, Clone, Default, Serialize)]
pub struct UpdateAutomation {
    /// Display name.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    /// Description.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    /// Trigger config.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub trigger_config: Option<Value>,
    /// Entry segment.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub entry_segment_id: Option<String>,
    /// Re-entry policy.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub reentry_policy: Option<String>,
    /// Cooldown in seconds.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub reentry_cooldown_seconds: Option<u64>,
}

/// A/B split definition.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AbSplit {
    /// Weight for branch A.
    pub a: u32,
    /// Weight for branch B.
    pub b: u32,
}

/// Branch condition.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BranchCondition {
    /// Operator (e.g. `eq`, `gt`, `contains`, `in_segment`, `did_event`).
    pub op: String,
    /// Property path.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub property: Option<String>,
    /// Comparison value.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub value: Option<Value>,
    /// Segment id (for `in_segment`).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub segment_id: Option<String>,
    /// Event name (for `did_event`).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub event_name: Option<String>,
    /// Within window in seconds.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub within_seconds: Option<u64>,
}

/// One step config — discriminated by `type` field.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum AutomationStepConfig {
    /// Send an email.
    SendEmail {
        /// Template id.
        #[serde(skip_serializing_if = "Option::is_none")]
        template_id: Option<String>,
        /// From address.
        from: String,
        /// Reply-to address.
        #[serde(skip_serializing_if = "Option::is_none")]
        reply_to: Option<String>,
        /// Subject.
        #[serde(skip_serializing_if = "Option::is_none")]
        subject: Option<String>,
        /// HTML body.
        #[serde(skip_serializing_if = "Option::is_none")]
        html: Option<String>,
        /// Plain-text body.
        #[serde(skip_serializing_if = "Option::is_none")]
        text: Option<String>,
        /// Message type — `transactional` or `marketing`.
        #[serde(skip_serializing_if = "Option::is_none")]
        message_type: Option<String>,
        /// Topic id.
        #[serde(skip_serializing_if = "Option::is_none")]
        topic_id: Option<String>,
        /// Template variables.
        #[serde(skip_serializing_if = "Option::is_none")]
        variables: Option<HashMap<String, String>>,
    },
    /// Wait for the given number of seconds.
    Wait {
        /// Wait duration.
        duration_seconds: u64,
    },
    /// Branch the flow based on a condition.
    Branch {
        /// Condition.
        condition: BranchCondition,
    },
    /// Split traffic A/B.
    AbSplit {
        /// Split definition.
        split: AbSplit,
        /// Optional deterministic seed.
        #[serde(skip_serializing_if = "Option::is_none")]
        seed: Option<String>,
    },
}

/// One step record.
#[derive(Debug, Clone, Deserialize)]
pub struct AutomationStep {
    /// Step id.
    pub id: String,
    /// Owning automation id.
    pub automation_id: String,
    /// Parent step id (for branches).
    pub parent_step_id: Option<String>,
    /// Branch label.
    pub branch_label: Option<String>,
    /// Position within parent.
    pub position: u32,
    /// Step type.
    #[serde(rename = "type")]
    pub step_type: String,
    /// Step config (raw — also includes `type`).
    pub config: Value,
    /// Created.
    pub created_at: String,
    /// Updated.
    pub updated_at: String,
}

/// Steps list response.
#[derive(Debug, Clone, Deserialize)]
pub struct AutomationStepList {
    /// Rows.
    pub data: Vec<AutomationStep>,
}

/// Parameters for [`AutomationSteps::add`].
#[derive(Debug, Clone, Serialize)]
pub struct AddAutomationStep {
    /// Parent step id (for branching).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub parent_step_id: Option<String>,
    /// Branch label.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub branch_label: Option<String>,
    /// Position.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub position: Option<u32>,
    /// Config — see [`AutomationStepConfig`].
    pub config: AutomationStepConfig,
}

/// Parameters for [`AutomationSteps::update`].
#[derive(Debug, Clone, Default, Serialize)]
pub struct UpdateAutomationStep {
    /// Parent step id.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub parent_step_id: Option<String>,
    /// Branch label.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub branch_label: Option<String>,
    /// Position.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub position: Option<u32>,
    /// New config.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub config: Option<AutomationStepConfig>,
}

/// One automation run.
#[derive(Debug, Clone, Deserialize)]
pub struct AutomationRun {
    /// Run id.
    pub id: String,
    /// Automation id.
    pub automation_id: String,
    /// Contact id.
    pub contact_id: Option<String>,
    /// Contact email.
    pub contact_email: String,
    /// Trigger event id.
    pub trigger_event_id: Option<String>,
    /// Status.
    pub status: String,
    /// Current step.
    pub current_step_id: Option<String>,
    /// Per-run context.
    pub context: Value,
    /// Started.
    pub started_at: String,
    /// Completed.
    pub completed_at: Option<String>,
    /// Failed.
    pub failed_at: Option<String>,
    /// Failure reason.
    pub failure_reason: Option<String>,
    /// Created.
    pub created_at: String,
    /// Updated.
    pub updated_at: String,
}

/// One step execution within a run.
#[derive(Debug, Clone, Deserialize)]
pub struct AutomationRunStep {
    /// Row id.
    pub id: String,
    /// Owning run id.
    pub run_id: String,
    /// Step id.
    pub step_id: String,
    /// Status.
    pub status: String,
    /// Email id (for send steps).
    pub email_id: Option<String>,
    /// Branch taken (for branches).
    pub branch_taken: Option<String>,
    /// Scheduled for.
    pub scheduled_for: Option<String>,
    /// Started.
    pub started_at: Option<String>,
    /// Completed.
    pub completed_at: Option<String>,
    /// Error.
    pub error: Option<String>,
    /// Created.
    pub created_at: String,
}

/// Run steps list response.
#[derive(Debug, Clone, Deserialize)]
pub struct AutomationRunStepList {
    /// Rows.
    pub data: Vec<AutomationRunStep>,
}

/// Filters for [`AutomationRuns::list`].
#[derive(Debug, Clone, Default)]
pub struct ListAutomationRuns {
    /// Page size.
    pub limit: Option<u32>,
    /// Cursor.
    pub cursor: Option<String>,
    /// Filter by status.
    pub status: Option<String>,
}

impl ListAutomationRuns {
    fn to_query(&self) -> Vec<(&'static str, String)> {
        let mut q = Vec::new();
        if let Some(v) = self.limit {
            q.push(("limit", v.to_string()));
        }
        if let Some(v) = &self.cursor {
            q.push(("cursor", v.clone()));
        }
        if let Some(v) = &self.status {
            q.push(("status", v.clone()));
        }
        q
    }
}

/// Parameters for [`AutomationRuns::create`].
#[derive(Debug, Clone, Default, Serialize)]
pub struct CreateAutomationRun {
    /// Contact id.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub contact_id: Option<String>,
    /// Contact email.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub contact_email: Option<String>,
    /// Run context.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub context: Option<Value>,
}