Skip to main content

zero_bounce/
lib.rs

1pub mod api;
2pub mod utility;
3
4use std::collections::HashMap;
5
6pub use crate::utility::{ZBError, ZBResult, ApiBaseUrl};
7pub use crate::utility::structures::{ActivityData, ApiUsage};
8pub use crate::utility::structures::bulk::{
9    zb_download_type, ZBFile, ZBFileFeedback, ZBFileStatus, ZBBulkResponse, ZBGetFileOptions,
10};
11pub use crate::utility::{format_get_file_error_message, get_file_json_indicates_error};
12pub use crate::utility::structures::validate_enums::{ZBValidateStatus, ZBValidateSubStatus};
13pub use crate::api::{FindEmailV2Builder, DomainSearchV2Builder};
14
15// Structure meant to generate the URLs to be accessed with the HTTP requests
16// based on the base API URLs (for the base API and bulk API).
17pub struct ZBUrlProvider {
18    pub url: String,
19    pub bulk_url: String,
20}
21
22impl ZBUrlProvider {
23    pub fn url_of(&self, endpoint: &str) -> String {
24        self.url.to_owned() + endpoint
25    }
26    pub fn bulk_url_of(&self, endpoint: &str) -> String {
27        self.bulk_url.to_owned() + endpoint
28    }
29}
30
31impl Default for ZBUrlProvider {
32    fn default() -> Self {
33        ZBUrlProvider {
34            url: ApiBaseUrl::Default.as_str().to_string(),
35            bulk_url: crate::utility::BULK_URI.to_string(),
36        }
37    }
38}
39
40// Client offering methods for different API methods and functionalities
41pub struct ZeroBounce {
42    pub api_key: String,
43    pub base_url: String,
44    pub client: reqwest::blocking::Client,
45    pub url_provider: ZBUrlProvider,
46}
47
48// More method implementations of this class can be found throughout
49// the project.
50impl ZeroBounce {
51    /// Create a new ZeroBounce client instance with the default API URL.
52    /// 
53    /// # Arguments
54    /// * `api_key` - Your ZeroBounce API key
55    /// 
56    /// # Example
57    /// ```no_run
58    /// use zero_bounce::ZeroBounce;
59    /// 
60    /// let zb = ZeroBounce::new("your_api_key");
61    /// ```
62    pub fn new(api_key: &str) -> ZeroBounce {
63        Self::with_base_url(api_key, ApiBaseUrl::Default)
64    }
65
66    /// Create a new ZeroBounce client instance with a custom base URL.
67    /// 
68    /// # Arguments
69    /// * `api_key` - Your ZeroBounce API key
70    /// * `base_url` - Base URL. Can be:
71    ///   - `ApiBaseUrl::Default` - Uses the default API URL
72    ///   - `ApiBaseUrl::USA` - Uses the USA API URL
73    ///   - `ApiBaseUrl::EU` - Uses the EU API URL
74    ///   - `String` or `&str` - Uses a custom URL string
75    /// 
76    /// # Example
77    /// ```no_run
78    /// use zero_bounce::{ZeroBounce, ApiBaseUrl};
79    /// 
80    /// // Using enum
81    /// let zb = ZeroBounce::with_base_url("your_api_key", ApiBaseUrl::USA);
82    /// 
83    /// // Using custom string
84    /// let zb = ZeroBounce::with_base_url("your_api_key", "https://custom-api.example.com/v2/");
85    /// ```
86    pub fn with_base_url<T>(api_key: &str, base_url: T) -> ZeroBounce 
87    where
88        T: Into<String>,
89    {
90        let base_url_string = base_url.into();
91        
92        let mut url_provider = ZBUrlProvider::default();
93        url_provider.url = base_url_string.clone();
94        
95        ZeroBounce {
96            api_key: api_key.to_string(),
97            base_url: base_url_string,
98            client: reqwest::blocking::Client::default(),
99            url_provider,
100        }
101    }
102
103    fn generic_get_request<'a>(&'a self, url: String, mut query_args: HashMap<&'a str, &'a str>) -> ZBResult<String> {
104        // Automatically add api_key to query arguments
105        query_args.insert("api_key", self.api_key.as_str());
106        
107        let response = self.client.get(url).query(&query_args).send()?;
108
109        let response_ok = response.status().is_success();
110        let response_content = response.text()?;
111
112        // Debug: Print raw response to examine structure in debug mode
113        #[cfg(debug_assertions)]
114        {
115            eprintln!("Raw API response: {}", response_content);
116        }
117
118        if !response_ok {
119            return Err(ZBError::ExplicitError(response_content));
120        }
121
122        Ok(response_content)
123    }
124}