Skip to main content

zero_bounce/utility/
mod.rs

1use std::io::Error as IOError;
2
3pub mod structures;
4pub mod mock_constants;
5pub mod bulk_get_file;
6
7pub use bulk_get_file::{format_get_file_error_message, get_file_json_indicates_error};
8
9pub const CONTENT_TYPE_JSON: &str = "application/json";
10pub const CONTENT_TYPE_STREAM: &str = "application/octet-stream";
11
12/// Enum representing the available API base URLs
13#[derive(Debug, Clone, Copy, PartialEq, Eq)]
14pub enum ApiBaseUrl {
15    /// Default API URL: https://api.zerobounce.net/v2/
16    Default,
17    /// USA API URL: https://api-us.zerobounce.net/v2/
18    USA,
19    /// EU API URL: https://api-eu.zerobounce.net/v2/
20    EU,
21}
22
23impl ApiBaseUrl {
24    /// Get the URL string for this API base URL
25    pub fn as_str(&self) -> &'static str {
26        match self {
27            ApiBaseUrl::Default => "https://api.zerobounce.net/v2/",
28            ApiBaseUrl::USA => "https://api-us.zerobounce.net/v2/",
29            ApiBaseUrl::EU => "https://api-eu.zerobounce.net/v2/",
30        }
31    }
32}
33
34impl From<ApiBaseUrl> for String {
35    fn from(url: ApiBaseUrl) -> Self {
36        url.as_str().to_string()
37    }
38}
39
40impl From<&ApiBaseUrl> for String {
41    fn from(url: &ApiBaseUrl) -> Self {
42        url.as_str().to_string()
43    }
44}
45
46pub const BULK_URI: &str = "https://bulkapi.zerobounce.net/v2";
47pub const ENDPOINT_CREDITS: &str = "/getcredits";
48pub const ENDPOINT_ACTIVITY_DATA: &str = "/activity";
49pub const ENDPOINT_VALIDATE: &str = "/validate";
50pub const ENDPOINT_API_USAGE: &str = "/getapiusage";
51pub const ENDPOINT_BATCH_VALIDATE: &str = "/validatebatch";
52pub const ENDPOINT_FILE_SEND: &str = "/sendfile";
53pub const ENDPOINT_FILE_STATUS: &str = "/filestatus";
54pub const ENDPOINT_FILE_RESULT: &str = "/getfile";
55pub const ENDPOINT_FILE_DELETE: &str = "/deletefile";
56pub const ENDPOINT_SCORING_SEND: &str = "/scoring/sendfile";
57pub const ENDPOINT_SCORING_STATUS: &str = "/scoring/filestatus";
58pub const ENDPOINT_SCORING_RESULT: &str = "/scoring/getfile";
59pub const ENDPOINT_SCORING_DELETE: &str = "/scoring/deletefile";
60pub const ENDPOINT_EMAIL_FINDER: &str = "/guessformat";
61
62// validation statuses
63pub const S_VALID: &str = "valid";
64pub const S_INVALID: &str = "invalid";
65pub const S_CATCH_ALL: &str = "catch-all";
66pub const S_UNKNOWN: &str = "unknown";
67pub const S_SPAMTRAP: &str = "spamtrap";
68pub const S_ABUSE: &str = "abuse";
69pub const S_DO_NOT_MAIL: &str = "do_not_mail";
70
71// validation sub statuses
72pub const SS_ANTISPAM_SYSTEM: &str = "antispam_system";
73pub const SS_GREYLISTED: &str = "greylisted";
74pub const SS_MAIL_SERVER_TEMPORARY_ERROR: &str = "mail_server_temporary_error";
75pub const SS_FORCIBLE_DISCONNECT: &str = "forcible_disconnect";
76pub const SS_MAIL_SERVER_DID_NOT_RESPOND: &str = "mail_server_did_not_respond";
77pub const SS_TIMEOUT_EXCEEDED: &str = "timeout_exceeded";
78pub const SS_FAILED_SMTP_CONNECTION: &str = "failed_smtp_connection";
79pub const SS_MAILBOX_QUOTA_EXCEEDED: &str = "mailbox_quota_exceeded";
80pub const SS_EXCEPTION_OCCURRED: &str = "exception_occurred";
81pub const SS_POSSIBLE_TRAP: &str = "possible_trap";
82pub const SS_ROLE_BASED: &str = "role_based";
83pub const SS_GLOBAL_SUPPRESSION: &str = "global_suppression";
84pub const SS_MAILBOX_NOT_FOUND: &str = "mailbox_not_found";
85pub const SS_NO_DNS_ENTRIES: &str = "no_dns_entries";
86pub const SS_FAILED_SYNTAX_CHECK: &str = "failed_syntax_check";
87pub const SS_POSSIBLE_TYPO: &str = "possible_typo";
88pub const SS_UNROUTABLE_IP_ADDRESS: &str = "unroutable_ip_address";
89pub const SS_LEADING_PERIOD_REMOVED: &str = "leading_period_removed";
90pub const SS_DOES_NOT_ACCEPT_MAIL: &str = "does_not_accept_mail";
91pub const SS_ALIAS_ADDRESS: &str = "alias_address";
92pub const SS_ROLE_BASED_CATCH_ALL: &str = "role_based_catch_all";
93pub const SS_DISPOSABLE: &str = "disposable";
94pub const SS_TOXIC: &str = "toxic";
95pub const SS_ACCEPT_ALL: &str = "accept_all";
96
97#[derive(Debug)]
98pub enum ZBError {
99    ExplicitError(String),
100    JsonError(serde_json::Error),
101    IntParseError(std::num::ParseIntError),
102    RequestError(reqwest::Error),
103    IOError(IOError),
104}
105
106pub type ZBResult<T> = Result<T, ZBError>;
107
108impl ZBError {
109    pub fn explicit(string: &str) -> ZBError {
110        ZBError::ExplicitError(string.to_string())
111    }
112}
113
114// Implementation made in order to automatically convert errors
115// generated by the reqwuest library into a ZBError instance.
116//
117// This automatic conversion is expected when using the client
118// and issuing http requests.
119impl From<reqwest::Error> for ZBError {
120    fn from(value: reqwest::Error) -> ZBError {
121        ZBError::RequestError(value)
122    }
123}
124
125// Implementation made in order to automatically convert errors
126// generated by the serde_json library into a ZBError instance.
127//
128// This automatic conversion is expected when deserializing.
129impl From<serde_json::Error> for ZBError {
130    fn from(value: serde_json::Error) -> ZBError {
131        ZBError::JsonError(value)
132    }
133}
134
135impl From<IOError> for ZBError {
136    fn from(value: IOError) -> Self {
137        ZBError::IOError(value)
138    }
139}