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
56
57
58
59
60
61
62
63
64
65
pub mod api;
pub mod utility;

use std::collections::HashMap;

pub use crate::utility::{ZBError, ZBResult};
pub use crate::utility::structures::{ActivityData, ApiUsage};
pub use crate::utility::structures::bulk::{ZBFile, ZBFileFeedback, ZBFileStatus};

// Structure meant to generate the URLs to be accessed with the HTTP requests
// based on the base API URLs (for the base API and bulk API).
pub struct ZBUrlProvider {
    pub url: String,
    pub bulk_url: String,
}

impl ZBUrlProvider {
    pub fn url_of(&self, endpoint: &str) -> String {
        return self.url.to_owned() + endpoint;
    }
    pub fn bulk_url_of(&self, endpoint: &str) -> String {
        return self.bulk_url.to_owned() + endpoint;
    }
}

impl Default for ZBUrlProvider {
    fn default() -> Self {
        ZBUrlProvider {
            url: crate::utility::URI.clone().to_string(),
            bulk_url: crate::utility::BULK_URI.clone().to_string(),
        }
    }
}

// Client offering methods for different API methods and functionalities
pub struct ZeroBounce {
    pub api_key: String,
    pub client: reqwest::blocking::Client,
    pub url_provider: ZBUrlProvider,
}

// More method implementations of this class can be found throughout
// the project.
impl ZeroBounce {
    pub fn new(api_key: &str) -> ZeroBounce {
        ZeroBounce {
            api_key: api_key.to_string().clone(),
            client: reqwest::blocking::Client::default(),
            url_provider: ZBUrlProvider::default(),
        }
    }

    fn generic_get_request(&self,url: String,query_args: HashMap<&str, &str>,) -> ZBResult<String> {
        let response = self.client.get(url).query(&query_args).send()?;

        let response_ok = response.status().is_success();
        let response_content = response.text()?;

        if !response_ok {
            return Err(ZBError::ExplicitError(response_content));
        }

        Ok(response_content)
    }
}