emailvalidation_rs/api/
mod.rs

1//! Module that contains the main [Emailvalidation] struct
2
3use std::sync::Arc;
4use reqwest::Client;
5use crate::error::EmailvalidationError;
6use crate::{error, models, utils};
7use crate::utils::baseline::construct_base_url;
8
9/// Settings struct that contains the api key
10#[derive(Debug, Clone)]
11pub struct Settings {
12    api_key: String,
13}
14
15/// The main struct of the crate giving access to the emailvalidation..
16/// Create a new instance of the struct with your api key as parameter.
17#[derive(Debug, Clone)]
18pub struct Emailvalidation {
19    client: Client,
20    settings: Arc<Settings>,
21}
22
23impl<'a> Emailvalidation {
24    /// Creates a new instance of the Emailvalidation struct by passing your api key as
25    /// function parameter.
26    pub fn new(api_key: &'a str) -> Result<Self, EmailvalidationError> {
27        let settings = std::sync::Arc::new(Settings {
28            api_key: String::from(api_key),
29        });
30        let client = utils::baseline::construct_client(None, &settings)?;
31        Ok(Self { client, settings })
32    }
33
34    pub async fn status(
35        &self,
36    ) -> Result<models::DetailsResponse, error::EmailvalidationError> {
37        let url = construct_base_url(&self.settings.api_key, Some("status"))?;
38        let res_body = self
39            .client
40            .get(url)
41            .send()
42            .await
43            .map_err(|err| error::EmailvalidationError::RequestError { source: err })?
44            .text()
45            .await
46            .map_err(|err| error::EmailvalidationError::RequestError { source: err })?;
47        serde_json::from_str::<models::DetailsResponse>(&res_body)
48            .map_err(|_| error::EmailvalidationError::ResponseParsingError { body: res_body })
49    }
50
51    pub async fn info(
52        &self,
53        email: &'a str,
54        catch_all: &'a str,
55    ) -> Result<models::DetailsResponse, error::EmailvalidationError> {
56        let mut url = construct_base_url(&self.settings.api_key, Some("info"))?;
57        url.query_pairs_mut()
58            .append_pair("email", email)
59            .append_pair("catch_all", catch_all);
60        let res_body = self
61            .client
62            .get(url)
63            .send()
64            .await
65            .map_err(|err| error::EmailvalidationError::RequestError { source: err })?
66            .text()
67            .await
68            .map_err(|err| error::EmailvalidationError::RequestError { source: err })?;
69        serde_json::from_str::<models::DetailsResponse>(&res_body)
70            .map_err(|_| error::EmailvalidationError::ResponseParsingError { body: res_body })
71    }
72}