sendly 3.30.0

Official Rust SDK for the Sendly SMS 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
use reqwest::{multipart, Client, Response, StatusCode};
use std::time::Duration;

use crate::account_resource::AccountResource;
use crate::campaigns::CampaignsResource;
use crate::contacts::ContactsResource;
use crate::conversations::ConversationsResource;
use crate::drafts::DraftsResource;
use crate::enterprise::EnterpriseResource;
use crate::error::{ApiErrorResponse, Error, Result};
use crate::labels::LabelsResource;
use crate::media::Media;
use crate::rules::RulesResource;
use crate::messages::Messages;
use crate::templates::TemplatesResource;
use crate::verify::VerifyResource;
use crate::webhook_resource::WebhooksResource;

/// Default API base URL.
pub const DEFAULT_BASE_URL: &str = "https://sendly.live/api/v1";

/// SDK version.
pub const VERSION: &str = "0.9.5";

/// Configuration for the Sendly client.
#[derive(Debug, Clone)]
pub struct SendlyConfig {
    /// API base URL.
    pub base_url: String,
    /// Request timeout.
    pub timeout: Duration,
    /// Maximum retry attempts.
    pub max_retries: u32,
    pub organization_id: Option<String>,
}

impl Default for SendlyConfig {
    fn default() -> Self {
        Self {
            base_url: DEFAULT_BASE_URL.to_string(),
            timeout: Duration::from_secs(30),
            max_retries: 3,
            organization_id: None,
        }
    }
}

impl SendlyConfig {
    /// Creates a new configuration.
    pub fn new() -> Self {
        Self::default()
    }

    /// Sets the base URL.
    pub fn base_url(mut self, url: impl Into<String>) -> Self {
        self.base_url = url.into();
        self
    }

    /// Sets the timeout.
    pub fn timeout(mut self, timeout: Duration) -> Self {
        self.timeout = timeout;
        self
    }

    /// Sets the max retries.
    pub fn max_retries(mut self, retries: u32) -> Self {
        self.max_retries = retries;
        self
    }

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

/// Sendly API client.
#[derive(Debug, Clone)]
pub struct Sendly {
    api_key: String,
    config: SendlyConfig,
    client: Client,
    organization_id: Option<String>,
}

impl Sendly {
    /// Creates a new Sendly client with default configuration.
    ///
    /// # Arguments
    ///
    /// * `api_key` - Your Sendly API key
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// use sendly::Sendly;
    ///
    /// let client = Sendly::new("sk_live_v1_your_api_key");
    /// ```
    pub fn new(api_key: impl Into<String>) -> Self {
        Self::with_config(api_key, SendlyConfig::default())
    }

    /// Creates a new Sendly client with custom configuration.
    ///
    /// # Arguments
    ///
    /// * `api_key` - Your Sendly API key
    /// * `config` - Client configuration
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// use sendly::{Sendly, SendlyConfig};
    /// use std::time::Duration;
    ///
    /// let config = SendlyConfig::new()
    ///     .timeout(Duration::from_secs(60))
    ///     .max_retries(5);
    ///
    /// let client = Sendly::with_config("sk_live_v1_xxx", config);
    /// ```
    pub fn with_config(api_key: impl Into<String>, config: SendlyConfig) -> Self {
        let client = Client::builder()
            .timeout(config.timeout)
            .build()
            .expect("Failed to build HTTP client");

        let organization_id = config
            .organization_id
            .clone()
            .or_else(|| std::env::var("SENDLY_ORG_ID").ok());

        Self {
            api_key: api_key.into(),
            config,
            client,
            organization_id,
        }
    }

    /// Returns the Messages resource.
    pub fn messages(&self) -> Messages {
        Messages::new(self)
    }

    /// Returns the Webhooks resource.
    pub fn webhooks(&self) -> WebhooksResource {
        WebhooksResource::new(self)
    }

    /// Returns the Account resource.
    pub fn account(&self) -> AccountResource {
        AccountResource::new(self)
    }

    /// Returns the Verify resource.
    pub fn verify(&self) -> VerifyResource {
        VerifyResource::new(self)
    }

    /// Returns the Templates resource.
    pub fn templates(&self) -> TemplatesResource {
        TemplatesResource::new(self)
    }

    /// Returns the Campaigns resource.
    pub fn campaigns(&self) -> CampaignsResource {
        CampaignsResource::new(self)
    }

    /// Returns the Contacts resource.
    pub fn contacts(&self) -> ContactsResource {
        ContactsResource::new(self)
    }

    /// Returns the Conversations resource.
    pub fn conversations(&self) -> ConversationsResource {
        ConversationsResource::new(self)
    }

    /// Returns the Labels resource.
    pub fn labels(&self) -> LabelsResource {
        LabelsResource::new(self)
    }

    /// Returns the Rules resource.
    pub fn rules(&self) -> RulesResource {
        RulesResource::new(self)
    }

    /// Returns the Drafts resource.
    pub fn drafts(&self) -> DraftsResource {
        DraftsResource::new(self)
    }

    /// Returns the Media resource.
    pub fn media(&self) -> Media {
        Media::new(self)
    }

    /// Returns the Enterprise resource.
    pub fn enterprise(&self) -> EnterpriseResource {
        EnterpriseResource::new(self)
    }

    /// Makes a GET request.
    pub fn set_organization_id(&mut self, id: impl Into<String>) {
        self.organization_id = Some(id.into());
    }

    pub(crate) async fn get(&self, path: &str, query: &[(String, String)]) -> Result<Response> {
        self.request_with_retry(|| async {
            let url = format!("{}{}", self.config.base_url, path);

            let req = self
                .client
                .get(&url)
                .query(query)
                .header("Authorization", format!("Bearer {}", self.api_key))
                .header("Accept", "application/json")
                .header("User-Agent", format!("sendly-rs/{}", VERSION));
            let req = if let Some(ref org_id) = self.organization_id {
                req.header("X-Organization-Id", org_id)
            } else {
                req
            };
            req.send().await
        })
        .await
    }

    /// Makes a POST request.
    pub(crate) async fn post<T: serde::Serialize>(&self, path: &str, body: &T) -> Result<Response> {
        self.request_with_retry(|| async {
            let url = format!("{}{}", self.config.base_url, path);

            let req = self
                .client
                .post(&url)
                .json(body)
                .header("Authorization", format!("Bearer {}", self.api_key))
                .header("Content-Type", "application/json")
                .header("Accept", "application/json")
                .header("User-Agent", format!("sendly-rs/{}", VERSION));
            let req = if let Some(ref org_id) = self.organization_id {
                req.header("X-Organization-Id", org_id)
            } else {
                req
            };
            req.send().await
        })
        .await
    }

    /// Makes a PUT request.
    pub(crate) async fn put<T: serde::Serialize>(&self, path: &str, body: &T) -> Result<Response> {
        self.request_with_retry(|| async {
            let url = format!("{}{}", self.config.base_url, path);

            let req = self
                .client
                .put(&url)
                .json(body)
                .header("Authorization", format!("Bearer {}", self.api_key))
                .header("Content-Type", "application/json")
                .header("Accept", "application/json")
                .header("User-Agent", format!("sendly-rs/{}", VERSION));
            let req = if let Some(ref org_id) = self.organization_id {
                req.header("X-Organization-Id", org_id)
            } else {
                req
            };
            req.send().await
        })
        .await
    }

    /// Makes a PATCH request.
    pub(crate) async fn patch<T: serde::Serialize>(
        &self,
        path: &str,
        body: &T,
    ) -> Result<Response> {
        self.request_with_retry(|| async {
            let url = format!("{}{}", self.config.base_url, path);

            let req = self
                .client
                .patch(&url)
                .json(body)
                .header("Authorization", format!("Bearer {}", self.api_key))
                .header("Content-Type", "application/json")
                .header("Accept", "application/json")
                .header("User-Agent", format!("sendly-rs/{}", VERSION));
            let req = if let Some(ref org_id) = self.organization_id {
                req.header("X-Organization-Id", org_id)
            } else {
                req
            };
            req.send().await
        })
        .await
    }

    /// Makes a multipart POST request.
    pub(crate) async fn post_multipart(
        &self,
        path: &str,
        form: multipart::Form,
    ) -> Result<Response> {
        let url = format!("{}{}", self.config.base_url, path);

        let req = self
            .client
            .post(&url)
            .multipart(form)
            .header("Authorization", format!("Bearer {}", self.api_key))
            .header("Accept", "application/json")
            .header("User-Agent", format!("sendly-rs/{}", VERSION));
        let req = if let Some(ref org_id) = self.organization_id {
            req.header("X-Organization-Id", org_id)
        } else {
            req
        };
        let response = req
            .send()
            .await
            .map_err(|e| {
                if e.is_timeout() {
                    Error::Timeout
                } else if e.is_connect() {
                    Error::Network {
                        message: e.to_string(),
                    }
                } else {
                    Error::Http(e)
                }
            })?;

        self.handle_response(response).await
    }

    /// Makes a DELETE request.
    pub(crate) async fn delete(&self, path: &str) -> Result<Response> {
        self.request_with_retry(|| async {
            let url = format!("{}{}", self.config.base_url, path);

            let req = self
                .client
                .delete(&url)
                .header("Authorization", format!("Bearer {}", self.api_key))
                .header("Accept", "application/json")
                .header("User-Agent", format!("sendly-rs/{}", VERSION));
            let req = if let Some(ref org_id) = self.organization_id {
                req.header("X-Organization-Id", org_id)
            } else {
                req
            };
            req.send().await
        })
        .await
    }

    /// Executes a request with retries.
    async fn request_with_retry<F, Fut>(&self, request_fn: F) -> Result<Response>
    where
        F: Fn() -> Fut,
        Fut: std::future::Future<Output = std::result::Result<Response, reqwest::Error>>,
    {
        let mut last_error: Option<Error> = None;

        for attempt in 0..=self.config.max_retries {
            if attempt > 0 {
                let delay = Duration::from_secs(2u64.pow(attempt - 1));
                tokio::time::sleep(delay).await;
            }

            match request_fn().await {
                Ok(response) => {
                    return self.handle_response(response).await;
                }
                Err(e) => {
                    if e.is_timeout() {
                        last_error = Some(Error::Timeout);
                    } else if e.is_connect() {
                        last_error = Some(Error::Network {
                            message: e.to_string(),
                        });
                    } else {
                        return Err(Error::Http(e));
                    }
                }
            }
        }

        Err(last_error.unwrap_or(Error::Network {
            message: "Request failed after retries".to_string(),
        }))
    }

    /// Handles the response and converts errors.
    async fn handle_response(&self, response: Response) -> Result<Response> {
        let status = response.status();

        if status.is_success() {
            return Ok(response);
        }

        let retry_after = response
            .headers()
            .get("Retry-After")
            .and_then(|v| v.to_str().ok())
            .and_then(|v| v.parse().ok());

        let error_body: ApiErrorResponse = response.json().await.unwrap_or(ApiErrorResponse {
            message: None,
            error: None,
            code: None,
        });

        let message = error_body.message();

        Err(match status {
            StatusCode::UNAUTHORIZED => Error::Authentication { message },
            StatusCode::PAYMENT_REQUIRED => Error::InsufficientCredits { message },
            StatusCode::NOT_FOUND => Error::NotFound { message },
            StatusCode::TOO_MANY_REQUESTS => Error::RateLimit {
                message,
                retry_after,
            },
            StatusCode::BAD_REQUEST | StatusCode::UNPROCESSABLE_ENTITY => {
                Error::Validation { message }
            }
            _ => Error::Api {
                message,
                status_code: status.as_u16(),
                code: error_body.code,
            },
        })
    }
}