resend-rs 0.31.0

Resend's Official Rust SDK.
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
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
use std::sync::Arc;

use reqwest::Method;
use types::{ListRecipientsOptions, UpdateBroadcastOptions, UpdateBroadcastResponse};

use crate::{Config, Result, list_opts::ListResponse};
use crate::{
    list_opts::ListOptions,
    types::{
        Broadcast, BroadcastClickedLink, BroadcastRecipient, CancelBroadcastResponse,
        CreateBroadcastOptions, CreateBroadcastResponse, RemoveBroadcastResponse,
        SendBroadcastOptions, SendBroadcastResponse,
    },
};

/// `Resend` APIs for `/broadcasts` endpoints.
#[derive(Clone, Debug)]
pub struct BroadcastsSvc(pub(crate) Arc<Config>);

impl BroadcastsSvc {
    /// Create a new broadcast to send to your audience.
    ///
    /// <https://resend.com/docs/api-reference/broadcasts/create-broadcast>
    #[maybe_async::maybe_async]
    pub async fn create(
        &self,
        broadcast: CreateBroadcastOptions,
    ) -> Result<CreateBroadcastResponse> {
        let request = self.0.build(Method::POST, "/broadcasts");
        let response = self.0.send(request.json(&broadcast)).await?;
        let content = response.json::<CreateBroadcastResponse>().await?;

        Ok(content)
    }

    /// Start sending broadcasts to your audience through the Resend API.
    ///
    /// <https://resend.com/docs/api-reference/broadcasts/send-broadcast>
    #[maybe_async::maybe_async]
    pub async fn send(&self, broadcast: SendBroadcastOptions) -> Result<SendBroadcastResponse> {
        let path = format!("/broadcasts/{}/send", broadcast.broadcast_id);

        let request = self.0.build(Method::POST, &path);
        let response = self.0.send(request.json(&broadcast)).await?;
        let content = response.json::<SendBroadcastResponse>().await?;

        Ok(content)
    }

    /// Retrieve a list of broadcasts.
    ///
    /// - Default limit: no limit (return everything)
    ///
    /// <https://resend.com/docs/api-reference/broadcasts/list-broadcasts>
    #[maybe_async::maybe_async]
    pub async fn list<T>(&self, list_opts: ListOptions<T>) -> Result<ListResponse<Broadcast>> {
        let request = self.0.build(Method::GET, "/broadcasts").query(&list_opts);
        let response = self.0.send(request).await?;
        let content = response.json::<ListResponse<Broadcast>>().await?;

        Ok(content)
    }

    /// Retrieve a single broadcast.
    ///
    /// <https://resend.com/docs/api-reference/broadcasts/get-broadcast>
    #[maybe_async::maybe_async]
    pub async fn get(&self, broadcast_id: &str) -> Result<Broadcast> {
        let path = format!("/broadcasts/{broadcast_id}");

        let request = self.0.build(Method::GET, &path);
        let response = self.0.send(request).await?;
        let content = response.json::<Broadcast>().await?;

        Ok(content)
    }

    #[maybe_async::maybe_async]
    pub async fn cancel(&self, broadcast_id: &str) -> Result<CancelBroadcastResponse> {
        let path = format!("/broadcasts/{broadcast_id}/cancel");

        let request = self.0.build(Method::POST, &path);
        let response = self.0.send(request).await?;
        let content = response.json::<CancelBroadcastResponse>().await?;

        Ok(content)
    }

    /// Remove an existing broadcast.
    ///
    /// <https://resend.com/docs/api-reference/broadcasts/delete-broadcast>
    #[maybe_async::maybe_async]
    pub async fn delete(&self, broadcast_id: &str) -> Result<bool> {
        let path = format!("/broadcasts/{broadcast_id}");

        let request = self.0.build(Method::DELETE, &path);
        let response = self.0.send(request).await?;
        let content = response.json::<RemoveBroadcastResponse>().await?;

        Ok(content.deleted)
    }

    /// Retrieve the links clicked in a broadcast, ranked by total clicks.
    ///
    /// <https://resend.com/docs/api-reference/broadcasts/list-broadcast-clicked-links>
    #[maybe_async::maybe_async]
    pub async fn clicked_links<T>(
        &self,
        broadcast_id: &str,
        list_opts: ListOptions<T>,
    ) -> Result<ListResponse<BroadcastClickedLink>> {
        let path = format!("/broadcasts/{broadcast_id}/clicked-links");

        let request = self.0.build(Method::GET, &path).query(&list_opts);
        let response = self.0.send(request).await?;
        let content = response
            .json::<ListResponse<BroadcastClickedLink>>()
            .await?;

        Ok(content)
    }

    /// Update a broadcast to send to your audience.
    #[maybe_async::maybe_async]
    pub async fn update(
        &self,
        broadcast_id: &str,
        update: UpdateBroadcastOptions,
    ) -> Result<UpdateBroadcastResponse> {
        let path = format!("/broadcasts/{broadcast_id}");

        let request = self.0.build(Method::PATCH, &path);
        let response = self.0.send(request.json(&update)).await?;
        let content = response.json::<UpdateBroadcastResponse>().await?;

        Ok(content)
    }

    /// Retrieve the recipients of a broadcast for a given event type, such as who opened,
    /// clicked, or bounced.
    ///
    /// - Default limit: 20
    ///
    /// <https://resend.com/docs/api-reference/broadcasts/list-broadcast-recipients>
    #[maybe_async::maybe_async]
    pub async fn recipients<T>(
        &self,
        broadcast_id: &str,
        list_opts: ListRecipientsOptions<T>,
    ) -> Result<ListResponse<BroadcastRecipient>> {
        let path = format!("/broadcasts/{broadcast_id}/recipients");

        let request = self.0.build(Method::GET, &path).query(&list_opts);
        let response = self.0.send(request).await?;
        let content = response.json::<ListResponse<BroadcastRecipient>>().await?;

        Ok(content)
    }
}

#[allow(unreachable_pub)]
pub mod types {
    use ecow::EcoString;
    use serde::{Deserialize, Serialize};

    use crate::{
        list_opts::{ListAfter, ListBefore, ListOptions, TimeNotSpecified},
        types::{ContactId, SegmentId},
    };

    /// Details of a new `Broadcast`.
    #[must_use]
    #[derive(Debug, Clone, Serialize)]
    pub struct CreateBroadcastOptions {
        audience_id: String,
        from: String,
        subject: String,
        #[serde(skip_serializing_if = "Option::is_none")]
        reply_to: Option<Vec<String>>,
        #[serde(skip_serializing_if = "Option::is_none")]
        html: Option<String>,
        #[serde(skip_serializing_if = "Option::is_none")]
        text: Option<String>,
        #[serde(skip_serializing_if = "Option::is_none")]
        name: Option<String>,
        #[serde(skip_serializing_if = "Option::is_none")]
        send: Option<bool>,
        #[serde(skip_serializing_if = "Option::is_none")]
        scheduled_at: Option<String>,
    }

    impl CreateBroadcastOptions {
        /// Creates a new [`CreateBroadcastOptions`].
        ///
        /// - `audience_id`: The ID of the audience you want to send to.
        /// - `from`: To include a friendly name, use the format `"Your Name <sender@domain.com>"`.
        /// - `subject`: Email subject.
        pub fn new(audience_id: &str, from: &str, subject: &str) -> Self {
            Self {
                audience_id: audience_id.to_string(),
                from: from.to_string(),
                subject: subject.to_string(),
                reply_to: None,
                html: None,
                text: None,
                name: None,
                send: None,
                scheduled_at: None,
            }
        }

        /// Appends `reply_to` address to the broadcast.
        #[inline]
        pub fn with_reply(mut self, to: &str) -> Self {
            let reply_to = self.reply_to.get_or_insert_with(Vec::new);
            reply_to.push(to.to_owned());
            self
        }

        /// Appends multiple `reply_to` addresses to the broadcast.
        #[inline]
        pub fn with_reply_multiple(mut self, to: &[String]) -> Self {
            let reply_to = self.reply_to.get_or_insert_with(Vec::new);
            reply_to.extend_from_slice(to);
            self
        }

        /// Adds or overwrites the HTML version of the message.
        #[inline]
        pub fn with_html(mut self, html: &str) -> Self {
            self.html = Some(html.to_owned());
            self
        }

        /// Adds or overwrites the plain text version of the message.
        #[inline]
        pub fn with_text(mut self, text: &str) -> Self {
            self.text = Some(text.to_owned());
            self
        }

        /// Sets the broadast name.
        #[inline]
        pub fn with_name(mut self, name: &str) -> Self {
            self.name = Some(name.to_owned());
            self
        }

        /// When set to `true`, the broadcast will be sent or scheduled (if `scheduled_at` is
        /// provided) without requiring a separate call to the
        /// [`crate::broadcasts::BroadcastsSvc::send`] endpoint.
        #[inline]
        pub fn with_send(mut self, send: bool) -> Self {
            self.send = Some(send);
            self
        }

        /// Schedule email to be sent later. The date should be in language natural (e.g.: in 1 min)
        /// or ISO 8601 format (e.g: 2024-08-05T11:52:01.858Z).
        #[inline]
        pub fn with_scheduled_at(mut self, scheduled_at: &str) -> Self {
            self.scheduled_at = Some(scheduled_at.to_owned());
            self
        }
    }

    #[must_use]
    #[derive(Debug, Clone, Serialize, Default)]
    pub struct UpdateBroadcastOptions {
        #[serde(skip_serializing_if = "Option::is_none")]
        from: Option<String>,
        #[serde(skip_serializing_if = "Option::is_none")]
        subject: Option<String>,
        #[serde(skip_serializing_if = "Option::is_none")]
        reply_to: Option<Vec<String>>,
        #[serde(skip_serializing_if = "Option::is_none")]
        html: Option<String>,
        #[serde(skip_serializing_if = "Option::is_none")]
        text: Option<String>,
        #[serde(skip_serializing_if = "Option::is_none")]
        name: Option<String>,
    }

    impl UpdateBroadcastOptions {
        /// Creates a new [`UpdateBroadcastOptions`].
        pub fn new() -> Self {
            Self::default()
        }

        /// Adds or overwrites the sender email address.
        #[inline]
        pub fn with_from(mut self, from: &str) -> Self {
            self.from = Some(from.to_owned());
            self
        }

        /// Adds or overwrites the subject.
        #[inline]
        pub fn with_subject(mut self, subject: &str) -> Self {
            self.subject = Some(subject.to_owned());
            self
        }

        /// Appends `reply_to` address to the broadcast.
        pub fn with_reply(mut self, to: &str) -> Self {
            let reply_to = self.reply_to.get_or_insert_with(Vec::new);
            reply_to.push(to.to_owned());
            self
        }

        /// Appends multiple `reply_to` addresses to the broadcast.
        #[inline]
        pub fn with_reply_multiple(mut self, to: &[String]) -> Self {
            let reply_to = self.reply_to.get_or_insert_with(Vec::new);
            reply_to.extend_from_slice(to);
            self
        }

        /// Adds or overwrites the HTML version of the message.
        #[inline]
        pub fn with_html(mut self, html: &str) -> Self {
            self.html = Some(html.to_owned());
            self
        }

        /// Adds or overwrites the plain text version of the message.
        #[inline]
        pub fn with_text(mut self, text: &str) -> Self {
            self.text = Some(text.to_owned());
            self
        }

        /// Sets the broadast name.
        #[inline]
        pub fn with_name(mut self, name: &str) -> Self {
            self.name = Some(name.to_owned());
            self
        }
    }

    #[derive(Debug, Clone, Serialize, Deserialize)]
    pub struct UpdateBroadcastResponse {
        /// Unique identifier for the updated broadcast.
        pub id: BroadcastId,
    }

    crate::define_id_type!(BroadcastId);

    #[derive(Debug, Clone, Serialize, Deserialize)]
    pub struct CreateBroadcastResponse {
        /// The ID of the created broadcast.
        pub id: BroadcastId,
    }

    #[must_use]
    #[derive(Debug, Clone, Serialize)]
    pub struct SendBroadcastOptions {
        pub(crate) broadcast_id: BroadcastId,

        #[serde(skip_serializing_if = "Option::is_none")]
        scheduled_at: Option<String>,
    }

    impl SendBroadcastOptions {
        pub fn new(broadcast_id: &str) -> Self {
            let broadcast_id = BroadcastId(EcoString::from(broadcast_id.to_owned()));

            Self {
                broadcast_id,
                scheduled_at: None,
            }
        }

        /// Schedule email to be sent later. The date should be in language natural (e.g.: in 1 min)
        /// or ISO 8601 format (e.g: 2024-08-05T11:52:01.858Z).
        #[inline]
        pub fn with_scheduled_at(mut self, scheduled_at: &str) -> Self {
            self.scheduled_at = Some(scheduled_at.to_owned());
            self
        }
    }

    #[derive(Debug, Clone, Serialize, Deserialize)]
    pub struct SendBroadcastResponse {
        /// The ID of the sent broadcast.
        pub id: BroadcastId,
    }

    #[must_use]
    #[derive(Debug, Clone, Serialize, Deserialize)]
    pub struct Broadcast {
        pub id: BroadcastId,
        pub name: String,
        pub audience_id: SegmentId,
        pub status: String,
        pub created_at: String,
        pub scheduled_at: Option<String>,
        pub sent_at: Option<String>,
        pub from: Option<String>,
        pub subject: Option<String>,
        pub reply_to: Option<Vec<String>>,
        pub preview_text: Option<String>,
        pub text: Option<String>,
        pub html: Option<String>,
    }

    #[derive(Debug, Clone, Serialize, Deserialize)]
    pub struct CancelBroadcastResponse {
        pub id: BroadcastId,
    }

    #[derive(Debug, Clone, Serialize, Deserialize)]
    pub struct RemoveBroadcastResponse {
        /// The ID of the broadcast.
        #[allow(dead_code)]
        pub id: BroadcastId,
        /// The deleted attribute indicates that the corresponding broadcast has been deleted.
        pub deleted: bool,
    }

    /// The recipient event type to filter by when listing [`BroadcastRecipient`]s.
    #[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone, Copy)]
    #[must_use]
    #[serde(rename_all = "snake_case")]
    pub enum BroadcastRecipientEventType {
        Sent,
        Delivered,
        Opened,
        Clicked,
        Bounced,
        Complained,
        Unsubscribed,
        Suppressed,
    }

    /// The classification of a bounce for a [`BroadcastRecipient`].
    #[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone, Copy)]
    #[must_use]
    #[serde(rename_all = "snake_case")]
    pub enum BroadcastRecipientBounceType {
        Permanent,
        Transient,
        Undetermined,
    }

    /// Query parameters for [`crate::broadcasts::BroadcastsSvc::recipients`].
    ///
    /// `before` and `after` are mutually exclusive; use [`ListRecipientsOptions::list_before`] or
    /// [`ListRecipientsOptions::list_after`] to pick one, same as [`ListOptions`].
    ///
    /// <https://resend.com/docs/api-reference/broadcasts/list-broadcast-recipients>
    ///
    /// ## Example
    ///
    /// ```
    /// # use resend_rs::types::{BroadcastRecipientEventType, ListRecipientsOptions};
    /// let list_opts =
    ///   ListRecipientsOptions::new(BroadcastRecipientEventType::Clicked).with_limit(10);
    /// ```
    #[must_use]
    #[derive(Debug, Clone, Serialize)]
    // The `List` parameter only ever reaches the wire through `pagination`'s own (bound-free)
    // `Serialize` impl, so it never needs to implement `Serialize` itself; the phantom marker
    // types (e.g. `TimeNotSpecified`) intentionally don't.
    #[serde(bound(serialize = ""))]
    pub struct ListRecipientsOptions<List = TimeNotSpecified> {
        #[serde(rename = "type")]
        r#type: BroadcastRecipientEventType,

        #[serde(skip_serializing_if = "Option::is_none")]
        email: Option<String>,

        #[serde(skip_serializing_if = "Option::is_none")]
        bounce_type: Option<BroadcastRecipientBounceType>,

        #[serde(flatten)]
        pagination: ListOptions<List>,
    }

    impl ListRecipientsOptions<TimeNotSpecified> {
        /// Creates a new [`ListRecipientsOptions`], filtering recipients by the given event
        /// `type`.
        pub fn new(event_type: BroadcastRecipientEventType) -> Self {
            Self {
                r#type: event_type,
                email: None,
                bounce_type: None,
                pagination: ListOptions::default(),
            }
        }

        /// The id before which we'll retrieve the items. This id will *not* be included in the
        /// list.
        #[inline]
        pub fn list_before(self, id: &str) -> ListRecipientsOptions<ListBefore> {
            ListRecipientsOptions {
                r#type: self.r#type,
                email: self.email,
                bounce_type: self.bounce_type,
                pagination: self.pagination.list_before(id),
            }
        }

        /// The id after which we'll retrieve the items. This id will *not* be included in the
        /// list.
        #[inline]
        pub fn list_after(self, id: &str) -> ListRecipientsOptions<ListAfter> {
            ListRecipientsOptions {
                r#type: self.r#type,
                email: self.email,
                bounce_type: self.bounce_type,
                pagination: self.pagination.list_after(id),
            }
        }
    }

    impl<T> ListRecipientsOptions<T> {
        /// Number of recipients to retrieve.
        ///
        /// - min: 1
        /// - max: 100
        /// - default: 20
        #[inline]
        pub fn with_limit(mut self, limit: u8) -> Self {
            self.pagination = self.pagination.with_limit(limit);
            self
        }

        /// Filters recipients whose email contains this value.
        #[inline]
        pub fn with_email(mut self, email: &str) -> Self {
            self.email = Some(email.to_owned());
            self
        }

        /// Filters bounced recipients by bounce type.
        ///
        /// Only meaningful when `type` is [`BroadcastRecipientEventType::Bounced`].
        #[inline]
        pub fn with_bounce_type(mut self, bounce_type: BroadcastRecipientBounceType) -> Self {
            self.bounce_type = Some(bounce_type);
            self
        }
    }

    /// A link clicked by a [`BroadcastRecipient`]. Only present when `type` is `clicked`.
    #[must_use]
    #[derive(Debug, Clone, Serialize, Deserialize)]
    pub struct BroadcastRecipientClickedLink {
        pub url: String,
        pub clicks: u32,
    }

    /// A single recipient of a broadcast, matching the requested event `type`.
    ///
    /// <https://resend.com/docs/api-reference/broadcasts/list-broadcast-recipients>
    #[must_use]
    #[derive(Debug, Clone, Serialize, Deserialize)]
    pub struct BroadcastRecipient {
        /// Opaque cursor identifying this row, used only for pagination. This does not identify
        /// any entity in Resend; use [`BroadcastRecipient::contact_id`] to reference the contact.
        pub id: String,
        /// The ID of the contact associated with this recipient. `None` if the recipient's email
        /// no longer maps to a contact.
        pub contact_id: Option<ContactId>,
        pub email: String,
        /// The number of times this recipient triggered the event. Only present when the
        /// requested `type` is `opened` or `clicked`.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        pub count: Option<u32>,
        /// The type of bounce. Only present when the requested `type` is `bounced`.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        pub bounce_type: Option<BroadcastRecipientBounceType>,
        /// The links this recipient clicked. Only present when the requested `type` is `clicked`.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        pub clicked_links: Option<Vec<BroadcastRecipientClickedLink>>,
    }

    #[derive(Debug, Clone, Serialize, Deserialize)]
    pub struct BroadcastClickedLink {
        /// An opaque cursor for this row, used only for pagination. It does not identify any
        /// entity in Resend.
        pub id: String,
        /// The URL that was clicked.
        pub url: String,
        /// Total number of clicks on this URL.
        pub clicks: u64,
        /// Number of unique clicks on this URL.
        pub unique_clicks: u64,
    }
}

#[cfg(test)]
#[allow(clippy::needless_return, clippy::indexing_slicing)]
mod test {
    #[cfg(not(feature = "blocking"))]
    use crate::{
        list_opts::ListOptions,
        test::{CLIENT, DebugResult},
        types::{
            CreateBroadcastOptions, CreateContactOptions, SendBroadcastOptions,
            UpdateBroadcastOptions,
        },
    };

    use super::types::{
        Broadcast, BroadcastClickedLink, BroadcastRecipient, BroadcastRecipientEventType,
        CancelBroadcastResponse, ListRecipientsOptions,
    };

    #[tokio_shared_rt::test(shared = true)]
    #[serial_test::serial]
    #[cfg(not(feature = "blocking"))]
    #[ignore = "Can no longer send broadcasts from the resend.dev domain"]
    async fn create_send_broadcast() -> DebugResult<()> {
        let resend = &*CLIENT;
        std::thread::sleep(std::time::Duration::from_secs(1));

        let audience_id = resend.segments.create("audience").await?.id;

        let contact = CreateContactOptions::new("steve.wozniak@gmail.com")
            .with_first_name("Steve")
            .with_last_name("Wozniak")
            .with_unsubscribed(false)
            .with_audience_id(&audience_id);

        let _contact_id = resend.contacts.create(contact).await?;

        let from = "Acme <onboarding@resend.dev>";
        let subject = "hello world";
        let html =
            "Hi {{{FIRST_NAME|there}}}, you can unsubscribe here: {{{RESEND_UNSUBSCRIBE_URL}}}";

        std::thread::sleep(std::time::Duration::from_secs(2));

        // Create
        let broadcast = CreateBroadcastOptions::new(&audience_id, from, subject).with_html(html);
        let res = resend.broadcasts.create(broadcast).await?;

        std::thread::sleep(std::time::Duration::from_secs(4));

        // Send
        let opts = SendBroadcastOptions::new(&res.id);
        let _res = resend.broadcasts.send(opts).await?;

        // Cleanup
        std::thread::sleep(std::time::Duration::from_secs(2));

        let deleted = resend.segments.delete(&audience_id).await?;
        std::thread::sleep(std::time::Duration::from_secs(1));

        assert!(deleted);

        Ok(())
    }

    #[tokio_shared_rt::test(shared = true)]
    #[serial_test::serial]
    #[cfg(not(feature = "blocking"))]
    #[ignore = "Can no longer send broadcasts from the resend.dev domain"]
    async fn list_get_broadcast() -> DebugResult<()> {
        let resend = &*CLIENT;
        std::thread::sleep(std::time::Duration::from_secs(1));

        let broadcasts = resend.broadcasts.list(ListOptions::default()).await?;
        assert!(!broadcasts.data.is_empty(), "No broadcasts found");
        let broadcast = broadcasts[0].clone();

        let _res = resend.broadcasts.get(&broadcast.id.clone()).await?;
        let _deleted = resend.broadcasts.delete(&broadcast.id).await;
        // TODO: This does not seem to be the case anymore?
        // Already used broadcasts cant be deleted
        // assert!(deleted.is_err());

        // Create fresh broadcast and delete that instead
        let audience_id = resend.segments.create("audience").await?.id;
        let from = "Acme <onboarding@resend.dev>";
        let subject = "hello world";
        let text = "text";

        let broadcast = CreateBroadcastOptions::new(&audience_id, from, subject).with_text(text);
        let res = resend.broadcasts.create(broadcast).await?;
        std::thread::sleep(std::time::Duration::from_secs(2));
        let deleted_broadcast = resend.broadcasts.delete(&res.id).await;
        let deleted_audience = resend.segments.delete(&audience_id).await;
        std::thread::sleep(std::time::Duration::from_secs(1));

        assert!(deleted_broadcast.is_ok());
        assert!(deleted_audience.is_ok());

        Ok(())
    }

    #[tokio_shared_rt::test(shared = true)]
    #[serial_test::serial]
    #[cfg(not(feature = "blocking"))]
    #[ignore = "Can no longer send broadcasts from the resend.dev domain"]
    async fn update_broadcast() -> DebugResult<()> {
        let resend = &*CLIENT;
        std::thread::sleep(std::time::Duration::from_secs(1));

        // Create audience & broadcast
        let audience_id = resend.segments.create("audience").await?.id;
        let from = "Acme <onboarding@resend.dev>";
        let subject = "hello world";

        let create_broadcast =
            CreateBroadcastOptions::new(&audience_id, from, subject).with_text("text");
        let broadcast_id = resend.broadcasts.create(create_broadcast).await?.id;
        std::thread::sleep(std::time::Duration::from_secs(2));

        // Assert subject == initial subject
        let broadcast = resend.broadcasts.get(&broadcast_id).await?;
        assert_eq!(Some(subject.to_string()), broadcast.subject);

        std::thread::sleep(std::time::Duration::from_secs(2));

        // Update subject
        let subject = "updated";
        let opts = UpdateBroadcastOptions::new().with_subject(subject);
        let _unused = resend.broadcasts.update(&broadcast_id, opts).await?;

        // Assert subject == updated subject
        let broadcast = resend.broadcasts.get(&broadcast_id).await?;
        assert_eq!(Some(subject.to_string()), broadcast.subject);

        // Delete
        let deleted = resend.broadcasts.delete(&broadcast_id).await?;
        assert!(deleted);

        Ok(())
    }

    #[test]
    fn parse_broadcast_test() {
        let data = r#"{
    "object": "broadcast",
    "id": "498ee8e4-7aa2-4eb5-9f04-4194848049d1",
    "name": "Untitled",
    "audience_id": "fd644f07-a05a-467e-9bae-23bb7c35766a",
    "from": "Acme <onboarding@resend.dev>",
    "subject": "Hello!",
    "reply_to": [],
    "preview_text": null,
    "status": "scheduled",
    "created_at": "2024-12-18 18:05:09.905933+00",
    "scheduled_at": null,
    "sent_at": null
}"#;

        let _parsed = serde_json::from_str::<Broadcast>(data).expect("Parsing failed");
    }

    #[test]
    fn parse_cancel_broadcast_response_test() {
        let data = r#"{
    "object": "broadcast",
    "id": "498ee8e4-7aa2-4eb5-9f04-4194848049d1"
}"#;

        let _parsed =
            serde_json::from_str::<CancelBroadcastResponse>(data).expect("Parsing failed");
    }

    #[test]
    fn parse_recipients_response_sent_test() {
        let data = r#"{
    "object": "list",
    "has_more": false,
    "data": [
        {
            "id": "b2Zmc2V0OjA",
            "contact_id": "e169aa45-1ecf-4183-9955-b1499d5701d3",
            "email": "steve.wozniak@gmail.com"
        },
        {
            "id": "b2Zmc2V0OjE",
            "contact_id": null,
            "email": "dana@example.com"
        }
    ]
}"#;

        let parsed =
            serde_json::from_str::<crate::list_opts::ListResponse<BroadcastRecipient>>(data)
                .expect("Parsing failed");

        assert!(!parsed.has_more);
        assert_eq!(parsed.len(), 2);
        assert_eq!(
            parsed[0].contact_id.as_deref(),
            Some("e169aa45-1ecf-4183-9955-b1499d5701d3")
        );
        assert!(parsed[1].contact_id.is_none());
        assert!(parsed[0].count.is_none());
        assert!(parsed[0].bounce_type.is_none());
        assert!(parsed[0].clicked_links.is_none());
    }

    #[test]
    fn parse_recipients_response_opened_test() {
        let data = r#"{
    "id": "b2Zmc2V0OjA",
    "contact_id": "e169aa45-1ecf-4183-9955-b1499d5701d3",
    "email": "steve.wozniak@gmail.com",
    "count": 3
}"#;

        let parsed = serde_json::from_str::<BroadcastRecipient>(data).expect("Parsing failed");

        assert_eq!(parsed.count, Some(3));
        assert!(parsed.bounce_type.is_none());
        assert!(parsed.clicked_links.is_none());
    }

    #[test]
    fn parse_recipients_response_clicked_test() {
        let data = r#"{
    "id": "b2Zmc2V0OjA",
    "contact_id": "e169aa45-1ecf-4183-9955-b1499d5701d3",
    "email": "carter@example.com",
    "count": 3,
    "clicked_links": [
        { "url": "https://resend.com/pricing", "clicks": 2 },
        { "url": "https://resend.com/docs", "clicks": 1 }
    ]
}"#;

        let parsed = serde_json::from_str::<BroadcastRecipient>(data).expect("Parsing failed");

        assert_eq!(parsed.count, Some(3));
        let clicked_links = parsed.clicked_links.expect("clicked_links should be set");
        assert_eq!(clicked_links.len(), 2);
        assert_eq!(clicked_links[0].url, "https://resend.com/pricing");
        assert_eq!(clicked_links[0].clicks, 2);
    }

    #[test]
    fn parse_recipients_response_bounced_test() {
        let data = r#"{
    "id": "b2Zmc2V0OjA",
    "contact_id": null,
    "email": "bounced@example.com",
    "bounce_type": "permanent"
}"#;

        let parsed = serde_json::from_str::<BroadcastRecipient>(data).expect("Parsing failed");

        assert!(parsed.contact_id.is_none());
        assert!(parsed.count.is_none());
        assert!(parsed.clicked_links.is_none());
        assert_eq!(
            parsed.bounce_type,
            Some(super::types::BroadcastRecipientBounceType::Permanent)
        );
    }

    #[test]
    fn serialize_list_recipients_options_test() {
        use super::types::BroadcastRecipientBounceType;

        let opts = ListRecipientsOptions::new(BroadcastRecipientEventType::Bounced)
            .with_email("steve")
            .with_bounce_type(BroadcastRecipientBounceType::Permanent)
            .with_limit(10)
            .list_after("cursor-123");

        let json = serde_json::to_value(&opts).expect("Failed to serialize");

        assert_eq!(json["type"], "bounced");
        assert_eq!(json["email"], "steve");
        assert_eq!(json["bounce_type"], "permanent");
        assert_eq!(json["limit"], 10);
        assert_eq!(json["after"], "cursor-123");
        assert!(json.get("before").is_none() || json["before"].is_null());
    }

    #[tokio_shared_rt::test(shared = true)]
    #[serial_test::serial]
    #[cfg(not(feature = "blocking"))]
    #[ignore = "requires RESEND_API_KEY and network access"]
    async fn recipients_not_found() -> DebugResult<()> {
        let resend = &*CLIENT;
        std::thread::sleep(std::time::Duration::from_secs(1));

        let list_opts = ListRecipientsOptions::new(BroadcastRecipientEventType::Sent);
        let result = resend
            .broadcasts
            .recipients("00000000-0000-0000-0000-000000000000", list_opts)
            .await;

        assert!(result.is_err());

        Ok(())
    }

    #[test]
    fn parse_broadcast_clicked_links_test() {
        let data = r#"{
          "object": "list",
          "has_more": true,
          "data": [
            {
              "id": "b2Zmc2V0OjA",
              "url": "https://resend.com/pricing",
              "clicks": 42,
              "unique_clicks": 30
            },
            {
              "id": "b2Zmc2V0OjE",
              "url": "https://resend.com/docs",
              "clicks": 17,
              "unique_clicks": 15
            }
          ]
        }"#;

        let _parsed =
            serde_json::from_str::<crate::list_opts::ListResponse<BroadcastClickedLink>>(data)
                .expect("Parsing failed");
    }
}