Skip to main content

usesend_api/services/
emails.rs

1use crate::config::SharedConfig;
2use crate::error::ApiResult;
3use crate::types::email::*;
4
5#[derive(Debug, Clone)]
6pub struct EmailsSvc(pub(crate) SharedConfig);
7
8impl EmailsSvc {
9    pub async fn send(&self, body: &SendEmailRequest) -> ApiResult<SendEmailResponse> {
10        let req = self
11            .0
12            .auth(self.0.client.post(self.0.url("/v1/emails")))
13            .json(body);
14        self.0.send_and_parse(req).await
15    }
16
17    pub async fn send_with_idempotency_key(
18        &self,
19        body: &SendEmailRequest,
20        idempotency_key: &str,
21    ) -> ApiResult<SendEmailResponse> {
22        let req = self
23            .0
24            .auth(self.0.client.post(self.0.url("/v1/emails")))
25            .header("Idempotency-Key", idempotency_key)
26            .json(body);
27        self.0.send_and_parse(req).await
28    }
29
30    pub async fn batch_send(&self, bodies: &[SendEmailRequest]) -> ApiResult<BatchSendResponse> {
31        let req = self
32            .0
33            .auth(self.0.client.post(self.0.url("/v1/emails/batch")))
34            .json(bodies);
35        self.0.send_and_parse(req).await
36    }
37
38    pub async fn batch_send_with_idempotency_key(
39        &self,
40        bodies: &[SendEmailRequest],
41        idempotency_key: &str,
42    ) -> ApiResult<BatchSendResponse> {
43        let req = self
44            .0
45            .auth(self.0.client.post(self.0.url("/v1/emails/batch")))
46            .header("Idempotency-Key", idempotency_key)
47            .json(bodies);
48        self.0.send_and_parse(req).await
49    }
50
51    pub async fn list(&self, params: &ListEmailsParams) -> ApiResult<ListEmailsResponse> {
52        let mut req = self.0.auth(self.0.client.get(self.0.url("/v1/emails")));
53        if let Some(page) = &params.page {
54            req = req.query(&[("page", page)]);
55        }
56        if let Some(limit) = &params.limit {
57            req = req.query(&[("limit", limit)]);
58        }
59        if let Some(start_date) = &params.start_date {
60            req = req.query(&[("startDate", start_date)]);
61        }
62        if let Some(end_date) = &params.end_date {
63            req = req.query(&[("endDate", end_date)]);
64        }
65        self.0.send_and_parse(req).await
66    }
67
68    pub async fn get(&self, email_id: &str) -> ApiResult<EmailDetail> {
69        let req = self.0.auth(
70            self.0
71                .client
72                .get(self.0.url(&format!("/v1/emails/{email_id}"))),
73        );
74        self.0.send_and_parse(req).await
75    }
76
77    pub async fn reschedule(
78        &self,
79        email_id: &str,
80        body: &RescheduleEmailRequest,
81    ) -> ApiResult<SendEmailResponse> {
82        let req = self
83            .0
84            .auth(
85                self.0
86                    .client
87                    .patch(self.0.url(&format!("/v1/emails/{email_id}"))),
88            )
89            .json(body);
90        self.0.send_and_parse(req).await
91    }
92
93    pub async fn cancel(&self, email_id: &str) -> ApiResult<SendEmailResponse> {
94        let req = self.0.auth(
95            self.0
96                .client
97                .post(self.0.url(&format!("/v1/emails/{email_id}/cancel"))),
98        );
99        self.0.send_and_parse(req).await
100    }
101}