flaremail-rs 0.1.0

Rust SDK for Cloudflare Email Service
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
use reqwest::header::{AUTHORIZATION, CONTENT_TYPE};
use serde::{Deserialize, Serialize, de::DeserializeOwned};
use serde_json::Value;
use std::collections::HashMap;

const DEFAULT_BASE_URL: &str = "https://api.cloudflare.com/client/v4";

#[derive(Clone, Debug)]
pub struct Email {
    api_key: String,
    account_id: Option<String>,
    base_url: String,
    client: reqwest::Client,
}

impl Email {
    pub fn new(api_key: impl Into<String>) -> Self {
        Self {
            api_key: api_key.into(),
            account_id: std::env::var("CLOUDFLARE_ACCOUNT_ID")
                .or_else(|_| std::env::var("CF_ACCOUNT_ID"))
                .ok(),
            base_url: DEFAULT_BASE_URL.to_string(),
            client: reqwest::Client::new(),
        }
    }

    pub fn from_env() -> Result<Self, Error> {
        let api_key = std::env::var("CLOUDFLARE_API_TOKEN")
            .or_else(|_| std::env::var("CF_API_TOKEN"))
            .map_err(|_| Error::MissingApiToken)?;

        Ok(Self::new(api_key))
    }

    pub fn with_account_id(mut self, account_id: impl Into<String>) -> Self {
        self.account_id = Some(account_id.into());
        self
    }

    pub fn with_base_url(mut self, base_url: impl Into<String>) -> Self {
        self.base_url = base_url.into().trim_end_matches('/').to_string();
        self
    }

    pub fn with_client(mut self, client: reqwest::Client) -> Self {
        self.client = client;
        self
    }

    pub fn emails(&self) -> Emails<'_> {
        Emails { email: self }
    }

    async fn account_id(&self) -> Result<String, Error> {
        if let Some(account_id) = &self.account_id {
            return Ok(account_id.clone());
        }

        let accounts: Vec<Account> = self
            .request(reqwest::Method::GET, "/accounts", None)
            .await?;

        match accounts.as_slice() {
            [] => Err(Error::NoAccounts),
            [account] => Ok(account.id.clone()),
            _ => Err(Error::MultipleAccounts),
        }
    }

    async fn request<T: DeserializeOwned>(
        &self,
        method: reqwest::Method,
        path: &str,
        body: Option<Value>,
    ) -> Result<T, Error> {
        if self.api_key.is_empty() {
            return Err(Error::MissingApiToken);
        }

        let url = format!("{}{}", self.base_url, path);
        let mut request = self
            .client
            .request(method, url)
            .header(AUTHORIZATION, format!("Bearer {}", self.api_key));

        if let Some(body) = body {
            request = request.header(CONTENT_TYPE, "application/json").json(&body);
        }

        let response = request.send().await?;
        let status = response.status();
        let payload = response.json::<CloudflareResponse<T>>().await?;

        if !status.is_success() || !payload.success {
            let api_error = payload.errors.first();

            return Err(Error::Api {
                status: status.as_u16(),
                code: api_error.map(ApiError::code),
                message: api_error
                    .map(|error| error.message.clone())
                    .unwrap_or_else(|| status.to_string()),
                details: Value::Array(payload.errors.into_iter().map(Value::from).collect()),
            });
        }

        payload.result.ok_or(Error::MissingResult)
    }
}

pub struct Emails<'a> {
    email: &'a Email,
}

impl Emails<'_> {
    pub async fn send(&self, message: SendEmail) -> Result<SendEmailResponse, Error> {
        self.send_with_options(message, SendOptions::default())
            .await
    }

    pub async fn create(&self, message: SendEmail) -> Result<SendEmailResponse, Error> {
        self.send(message).await
    }

    pub async fn send_with_options(
        &self,
        message: SendEmail,
        options: SendOptions,
    ) -> Result<SendEmailResponse, Error> {
        message.validate()?;

        let account_id = self.email.account_id().await?;
        let path = format!("/accounts/{account_id}/email/sending/send");
        let mut request = self
            .email
            .client
            .post(format!("{}{}", self.email.base_url, path))
            .header(AUTHORIZATION, format!("Bearer {}", self.email.api_key))
            .header(CONTENT_TYPE, "application/json");

        if let Some(idempotency_key) = options.idempotency_key {
            request = request.header("Idempotency-Key", idempotency_key);
        }

        let response = request.json(&message).send().await?;
        let status = response.status();
        let payload = response
            .json::<CloudflareResponse<CloudflareSendResult>>()
            .await?;

        if !status.is_success() || !payload.success {
            let api_error = payload.errors.first();

            return Err(Error::Api {
                status: status.as_u16(),
                code: api_error.map(ApiError::code),
                message: api_error
                    .map(|error| error.message.clone())
                    .unwrap_or_else(|| status.to_string()),
                details: Value::Array(payload.errors.into_iter().map(Value::from).collect()),
            });
        }

        let result = payload.result.ok_or(Error::MissingResult)?;

        Ok(SendEmailResponse {
            id: result.id(),
            delivered: result.delivered,
            queued: result.queued,
            permanent_bounces: result.permanent_bounces,
        })
    }
}

#[derive(Clone, Debug, Default)]
pub struct SendOptions {
    pub idempotency_key: Option<String>,
}

impl SendOptions {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn idempotency_key(mut self, key: impl Into<String>) -> Self {
        self.idempotency_key = Some(key.into());
        self
    }
}

#[derive(Clone, Debug, Serialize)]
pub struct SendEmail {
    pub from: Address,
    pub to: Recipients,
    pub subject: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub html: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub text: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cc: Option<Recipients>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub bcc: Option<Recipients>,
    #[serde(rename = "reply_to", skip_serializing_if = "Option::is_none")]
    pub reply_to: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub headers: Option<HashMap<String, String>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub attachments: Option<Vec<Attachment>>,
}

impl SendEmail {
    pub fn new(
        from: impl Into<Address>,
        to: impl Into<Recipients>,
        subject: impl Into<String>,
    ) -> Self {
        Self {
            from: from.into(),
            to: to.into(),
            subject: subject.into(),
            html: None,
            text: None,
            cc: None,
            bcc: None,
            reply_to: None,
            headers: None,
            attachments: None,
        }
    }

    pub fn html(mut self, html: impl Into<String>) -> Self {
        self.html = Some(html.into());
        self
    }

    pub fn text(mut self, text: impl Into<String>) -> Self {
        self.text = Some(text.into());
        self
    }

    pub fn cc(mut self, cc: impl Into<Recipients>) -> Self {
        self.cc = Some(cc.into());
        self
    }

    pub fn bcc(mut self, bcc: impl Into<Recipients>) -> Self {
        self.bcc = Some(bcc.into());
        self
    }

    pub fn reply_to(mut self, reply_to: impl Into<String>) -> Self {
        self.reply_to = Some(reply_to.into());
        self
    }

    pub fn header(mut self, name: impl Into<String>, value: impl Into<String>) -> Self {
        self.headers
            .get_or_insert_with(HashMap::new)
            .insert(name.into(), value.into());
        self
    }

    pub fn attachment(mut self, attachment: Attachment) -> Self {
        self.attachments
            .get_or_insert_with(Vec::new)
            .push(attachment);
        self
    }

    fn validate(&self) -> Result<(), Error> {
        if self.subject.is_empty() {
            return Err(Error::Validation("subject is required".to_string()));
        }

        if self.html.is_none() && self.text.is_none() {
            return Err(Error::Validation(
                "Either html or text is required".to_string(),
            ));
        }

        Ok(())
    }
}

#[derive(Clone, Debug, Serialize)]
#[serde(untagged)]
pub enum Address {
    Email(String),
    Named { address: String, name: String },
}

impl Address {
    pub fn email(email: impl Into<String>) -> Self {
        Self::Email(email.into())
    }

    pub fn named(name: impl Into<String>, email: impl Into<String>) -> Self {
        Self::Named {
            name: name.into(),
            address: email.into(),
        }
    }
}

impl From<&str> for Address {
    fn from(value: &str) -> Self {
        Self::Email(value.to_string())
    }
}

impl From<String> for Address {
    fn from(value: String) -> Self {
        Self::Email(value)
    }
}

#[derive(Clone, Debug, Serialize)]
#[serde(untagged)]
pub enum Recipients {
    One(String),
    Many(Vec<String>),
}

impl From<&str> for Recipients {
    fn from(value: &str) -> Self {
        Self::One(value.to_string())
    }
}

impl From<String> for Recipients {
    fn from(value: String) -> Self {
        Self::One(value)
    }
}

impl From<Vec<String>> for Recipients {
    fn from(value: Vec<String>) -> Self {
        Self::Many(value)
    }
}

impl<const N: usize> From<[&str; N]> for Recipients {
    fn from(value: [&str; N]) -> Self {
        Self::Many(value.into_iter().map(str::to_string).collect())
    }
}

#[derive(Clone, Debug, Serialize)]
pub struct Attachment {
    pub content: String,
    pub filename: String,
    #[serde(rename = "type")]
    pub mime_type: String,
    pub disposition: AttachmentDisposition,
    #[serde(rename = "content_id", skip_serializing_if = "Option::is_none")]
    pub content_id: Option<String>,
}

impl Attachment {
    pub fn new(
        content: impl Into<String>,
        filename: impl Into<String>,
        mime_type: impl Into<String>,
    ) -> Self {
        Self {
            content: content.into(),
            filename: filename.into(),
            mime_type: mime_type.into(),
            disposition: AttachmentDisposition::Attachment,
            content_id: None,
        }
    }

    pub fn inline(mut self, content_id: impl Into<String>) -> Self {
        self.disposition = AttachmentDisposition::Inline;
        self.content_id = Some(content_id.into());
        self
    }
}

#[derive(Clone, Debug, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum AttachmentDisposition {
    Attachment,
    Inline,
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct SendEmailResponse {
    pub id: String,
    pub delivered: Vec<String>,
    pub queued: Vec<String>,
    pub permanent_bounces: Vec<String>,
}

#[derive(Debug, thiserror::Error)]
pub enum Error {
    #[error("Missing Cloudflare API token")]
    MissingApiToken,
    #[error("No Cloudflare accounts found for this API token")]
    NoAccounts,
    #[error("Multiple Cloudflare accounts found; pass account_id explicitly")]
    MultipleAccounts,
    #[error("Cloudflare API error ({status}): {message}")]
    Api {
        status: u16,
        code: Option<String>,
        message: String,
        details: Value,
    },
    #[error("Cloudflare API response did not include a result")]
    MissingResult,
    #[error("Invalid email request: {0}")]
    Validation(String),
    #[error(transparent)]
    Http(#[from] reqwest::Error),
}

#[derive(Debug, Deserialize)]
struct Account {
    id: String,
}

#[derive(Debug, Deserialize)]
struct CloudflareResponse<T> {
    success: bool,
    errors: Vec<ApiError>,
    #[allow(dead_code)]
    messages: Vec<Value>,
    result: Option<T>,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
struct ApiError {
    code: Value,
    message: String,
}

impl ApiError {
    fn code(&self) -> String {
        match &self.code {
            Value::String(code) => code.clone(),
            code => code.to_string(),
        }
    }
}

impl From<ApiError> for Value {
    fn from(error: ApiError) -> Self {
        serde_json::json!({
            "code": error.code,
            "message": error.message,
        })
    }
}

#[derive(Debug, Deserialize)]
struct CloudflareSendResult {
    #[serde(default, alias = "messageId", alias = "message_id")]
    id: Option<String>,
    #[serde(default)]
    delivered: Vec<String>,
    #[serde(default)]
    queued: Vec<String>,
    #[serde(default)]
    permanent_bounces: Vec<String>,
}

impl CloudflareSendResult {
    fn id(&self) -> String {
        self.id.clone().unwrap_or_else(|| {
            self.delivered
                .iter()
                .chain(self.queued.iter())
                .cloned()
                .collect::<Vec<_>>()
                .join(",")
        })
    }
}