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
use std::sync::Arc;
use std::{env, fmt};
#[cfg(feature = "blocking")]
use reqwest::blocking::Client as ReqwestClient;
#[cfg(not(feature = "blocking"))]
use reqwest::Client as ReqwestClient;
use crate::services::{ApiKeysSvc, AudiencesSvc, ContactsSvc, DomainsSvc, EmailsSvc};
use crate::{batch::BatchSvc, config::Config};
/// 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 audiences: AudiencesSvc,
/// `Resend` APIs for `/audiences/:id/contacts` endpoints.
pub contacts: ContactsSvc,
/// `Resend` APIs for `/domains` endpoints.
pub domains: DomainsSvc,
}
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 inner = Arc::new(Config::new(api_key, client));
Self {
api_keys: ApiKeysSvc(inner.clone()),
audiences: AudiencesSvc(inner.clone()),
contacts: ContactsSvc(inner.clone()),
domains: DomainsSvc(inner.clone()),
emails: EmailsSvc(inner.clone()),
batch: BatchSvc(inner),
}
}
/// Returns the reference to the used `User-Agent` header value.
///
/// ### Notes
///
/// Use the `RESEND_USER_AGENT` environment variable to override.
#[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)
}
}