zero_bounce_india/
lib.rs

1pub mod api;
2pub mod utility;
3
4use std::collections::HashMap;
5
6pub use crate::utility::{ZBError, ZBResult};
7pub use crate::utility::structures::{ActivityData, ApiUsage};
8pub use crate::utility::structures::bulk::{ZBFile, ZBFileFeedback, ZBFileStatus};
9
10// Structure meant to generate the URLs to be accessed with the HTTP requests
11// based on the base API URLs (for the base API and bulk API).
12pub struct ZBUrlProvider {
13    pub url: String,
14    pub bulk_url: String,
15}
16
17impl ZBUrlProvider {
18    pub fn url_of(&self, endpoint: &str) -> String {
19        self.url.to_owned() + endpoint
20    }
21    pub fn bulk_url_of(&self, endpoint: &str) -> String {
22        self.bulk_url.to_owned() + endpoint
23    }
24}
25
26impl Default for ZBUrlProvider {
27    fn default() -> Self {
28        ZBUrlProvider {
29            url: crate::utility::URI.clone().to_string(),
30            bulk_url: crate::utility::BULK_URI.clone().to_string(),
31        }
32    }
33}
34
35// Client offering methods for different API methods and functionalities
36pub struct ZeroBounce {
37    pub api_key: String,
38    pub client: reqwest::blocking::Client,
39    pub url_provider: ZBUrlProvider,
40}
41
42// More method implementations of this class can be found throughout
43// the project.
44impl ZeroBounce {
45    pub fn new(api_key: &str) -> ZeroBounce {
46        ZeroBounce {
47            api_key: api_key.to_string().clone(),
48            client: reqwest::blocking::Client::default(),
49            url_provider: ZBUrlProvider::default(),
50        }
51    }
52
53    fn generic_get_request(&self,url: String,query_args: HashMap<&str, &str>,) -> ZBResult<String> {
54        let response = self.client.get(url).query(&query_args).send()?;
55
56        let response_ok = response.status().is_success();
57        let response_content = response.text()?;
58
59        if !response_ok {
60            return Err(ZBError::ExplicitError(response_content));
61        }
62
63        Ok(response_content)
64    }
65}