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
use std::sync::Arc;
use std::{env, fmt};
#[cfg(not(feature = "blocking"))]
use reqwest::Client as ReqwestClient;
#[cfg(feature = "blocking")]
use reqwest::blocking::Client as ReqwestClient;
use crate::{batch::BatchSvc, config::Config, services::ReceivingSvc, webhooks::WebhookSvc};
use crate::{
services::{
ApiKeysSvc, BroadcastsSvc, ContactsSvc, DomainsSvc, EmailsSvc, SegmentsSvc, TemplateSvc,
},
topics::TopicsSvc,
};
#[cfg(doc)]
use crate::ConfigBuilder;
/// The [Resend](https://resend.com) client.
#[must_use]
#[derive(Clone)]
pub struct Resend {
/// `Resend` APIs for `/emails` endpoints.
pub emails: EmailsSvc,
/// `Resend` APIs for the batch `/emails` endpoints.
pub batch: BatchSvc,
/// `Resend` APIs for `/api-keys` endpoints.
pub api_keys: ApiKeysSvc,
/// `Resend` APIs for `/audiences` endpoints.
pub segments: SegmentsSvc,
/// `Resend` APIs for `/audiences/:id/contacts` endpoints.
pub contacts: ContactsSvc,
/// `Resend` APIs for `/domains` endpoints.
pub domains: DomainsSvc,
/// `Resend` APIs for `/broadcasts` endpoints.
pub broadcasts: BroadcastsSvc,
/// `Resend` APIs for `/templates` endpoints.
pub templates: TemplateSvc,
/// `Resend` APIs for `/topics` endpoints.
pub topics: TopicsSvc,
/// `Resend` APIs for `/emails/receiving` endpoints.
pub receiving: ReceivingSvc,
/// `Resend` APIs for `/webhooks` endpoints.
pub webhooks: WebhookSvc,
}
impl Resend {
/// Creates a new [`Resend`] client.
///
/// ### Panics
///
/// - Panics if the environment variable `RESEND_BASE_URL` is set but is not a valid `URL`.
///
/// [`Resend`]: https://resend.com
pub fn new(api_key: &str) -> Self {
Self::with_client(api_key, ReqwestClient::default())
}
/// Creates a new [`Resend`] client with a provided [`reqwest::Client`].
///
/// ### Panics
///
/// - Panics if the environment variable `RESEND_BASE_URL` is set but is not a valid `URL`.
///
/// [`Resend`]: https://resend.com
/// [`reqwest::Client`]: ReqwestClient
pub fn with_client(api_key: &str, client: ReqwestClient) -> Self {
let config = Config::new(api_key.to_owned(), client, None);
Self::with_config(config)
}
/// Creates a new [`Resend`] client with a provided [`Config`].
///
/// Use [`ConfigBuilder::new`] to construct a [`Config`] instance.
///
/// ### Panics
///
/// - Panics if the base url has not been set with [`ConfigBuilder::base_url`]
/// and the environment variable `RESEND_BASE_URL` _is_ set but is not a valid `URL`.
///
/// [`Resend`]: https://resend.com
/// [`reqwest::Client`]: ReqwestClient
pub fn with_config(config: Config) -> Self {
let inner = Arc::new(config);
Self {
api_keys: ApiKeysSvc(Arc::clone(&inner)),
segments: SegmentsSvc(Arc::clone(&inner)),
contacts: ContactsSvc(Arc::clone(&inner)),
domains: DomainsSvc(Arc::clone(&inner)),
emails: EmailsSvc(Arc::clone(&inner)),
batch: BatchSvc(Arc::clone(&inner)),
broadcasts: BroadcastsSvc(Arc::clone(&inner)),
templates: TemplateSvc(Arc::clone(&inner)),
topics: TopicsSvc(Arc::clone(&inner)),
receiving: ReceivingSvc(Arc::clone(&inner)),
webhooks: WebhookSvc(inner),
}
}
/// Returns the reference to the used `User-Agent` header value.
#[inline]
#[must_use]
pub fn user_agent(&self) -> &str {
self.config().user_agent.as_str()
}
/// Returns the reference to the provided `API key`.
#[inline]
#[must_use]
pub fn api_key(&self) -> &str {
self.config().api_key.as_ref()
}
/// Returns the reference to the used `base URL`.
///
/// ### Notes
///
/// Use the `RESEND_BASE_URL` environment variable to override.
#[inline]
#[must_use]
pub fn base_url(&self) -> &str {
self.config().base_url.as_str()
}
/// Returns the underlying [`reqwest::Client`].
///
/// [`reqwest::Client`]: ReqwestClient
#[inline]
#[must_use]
pub fn client(&self) -> ReqwestClient {
self.config().client.clone()
}
/// Returns the reference to the inner [`Config`].
#[inline]
fn config(&self) -> &Config {
&self.emails.0
}
}
impl Default for Resend {
/// Creates a new [`Resend`] client from the `RESEND_API_KEY` environment variable .
///
/// ### Panics
///
/// - Panics if the environment variable `RESEND_API_KEY` is not set.
/// - Panics if the environment variable `RESEND_BASE_URL` is set but is not a valid `URL`.
fn default() -> Self {
let api_key = env::var("RESEND_API_KEY")
.expect("env variable `RESEND_API_KEY` should be a valid API key");
Self::new(api_key.as_str())
}
}
impl fmt::Debug for Resend {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(&self.emails, f)
}
}