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
use reqwest::blocking::Client;
use reqwest::Error;
use serde::Deserialize;
use std::error::Error as StdError;

#[derive(Deserialize, Debug)]
pub struct ResponseData {
    pub email_address: String,
    pub domain: String,
    pub valid_syntax: bool,
    pub disposable: bool,
    pub webmail: bool,
    pub deliverable: bool,
    pub catch_all: bool,
    pub gibberish: bool,
    pub spam: bool,
}

#[derive(Deserialize, Debug)]
pub struct ApiResponse {
    pub status: String,
    pub data: ResponseData,
}

pub fn fetch_email_data(email: &str) -> Result<ApiResponse, Box<dyn StdError>> {
    let url = format!("https://api.eva.pingutil.com/email?email={}", email);

    // Create a client with disabled SSL verification
    let client = Client::builder()
        .danger_accept_invalid_certs(true)
        .build()?;

    let response = client.get(&url).send()?;

    if response.status().is_success() {
        let body = response.text()?;
        let api_response: ApiResponse = serde_json::from_str(&body)?;
        Ok(api_response)
    } else {
        Err(Box::from(format!("HTTP request failed with status: {}", response.status())))
    }
}

pub fn get_domain_from_email(email: &str) -> Option<&str> {
    if let Some(domain) = email.split('@').nth(1) {
        if domain.is_empty() {
            None
        } else {
            Some(domain)
        }
    } else {
        None
    }
}