resend_rust/
lib.rs

1pub mod api_keys;
2pub mod batch;
3pub mod domains;
4pub mod emails;
5mod http;
6mod utils;
7
8const DEFAULT_BASE_URL: &str = "https://api.resend.com";
9
10async fn parse_response(res: reqwest::Response) -> Result<String, Error> {
11    if res.status() != 200 && res.status() != 201 {
12        return Err(Error::Resend(
13            serde_json::from_str(&res.text().await.unwrap()).unwrap(),
14        ));
15    }
16
17    res.text().await.map_err(Error::Client)
18}
19
20#[derive(Debug, Clone)]
21pub struct Client {
22    base_url: String,
23    client: http::Client,
24}
25
26impl Client {
27    pub fn new(api_key: &str) -> Self {
28        Self {
29            base_url: DEFAULT_BASE_URL.to_owned(),
30            client: http::Client::new(api_key),
31        }
32    }
33}
34
35impl Client {
36    async fn perform(&self, r: http::Request) -> Result<reqwest::Response, reqwest::Error> {
37        self.client.perform(r).await
38    }
39}
40
41#[derive(Debug)]
42pub enum Error {
43    Resend(ResendErrorResponse),
44    JSON(serde_json::Error),
45    #[cfg(feature = "reqwest")]
46    Client(reqwest::Error),
47    Internal,
48}
49
50#[derive(Debug, Clone, Default, serde_derive::Serialize, serde_derive::Deserialize)]
51pub struct ResendErrorResponse {
52    name: String,
53    message: String,
54}