pub mod bulk;
pub mod inbox_placement;
pub mod priority3;
pub mod validation;
use std::collections::HashMap;
use chrono::{NaiveDate, Utc};
pub use crate::ZeroBounce;
use crate::utility::http_error::{parse_json_body_with_status, UNEXPECTED_JSON_SHAPE_MESSAGE};
use crate::utility::structures::generic::{
FindEmailResponse, FindEmailResponseV2, DomainSearchResponseV2, DOMAIN_SEARCH_V2_FIELD_NAMES,
};
use crate::utility::{ZBError, ZBResult, ENDPOINT_EMAIL_FINDER};
use crate::utility::structures::{ActivityData, ApiUsage};
use crate::utility::structures::generic::ScoreEmailResponse;
use crate::utility::{ENDPOINT_ACTIVITY_DATA, ENDPOINT_API_USAGE, ENDPOINT_CREDITS, ENDPOINT_SCORING};
pub struct FindEmailV2Builder<'a> {
client: &'a ZeroBounce,
first_name: Option<&'a str>,
domain: Option<&'a str>,
company_name: Option<&'a str>,
middle_name: Option<&'a str>,
last_name: Option<&'a str>,
}
pub struct DomainSearchV2Builder<'a> {
client: &'a ZeroBounce,
domain: Option<&'a str>,
company_name: Option<&'a str>,
}
impl ZeroBounce {
pub(crate) fn get_request_with_status<'a>(
&'a self,
url: String,
mut query_args: HashMap<&'a str, &'a str>,
message_only_is_failure: bool,
) -> ZBResult<(u16, String)> {
self.require_key()?;
query_args.insert("api_key", self.api_key.as_str());
let response = self
.client
.get(crate::parse_request_url(&url)?)
.query(&query_args)
.send()?;
self.read_api_response_with_status(response, message_only_is_failure)
}
pub(crate) fn form_post_request_with_status<'a>(
&'a self,
url: String,
mut form_args: HashMap<&'a str, &'a str>,
) -> ZBResult<(u16, String)> {
self.require_key()?;
form_args.insert("api_key", self.api_key.as_str());
let response = self
.client
.post(crate::parse_request_url(&url)?)
.form(&form_args)
.send()?;
self.read_api_response_with_status(response, false)
}
pub(crate) fn read_api_response_with_status(
&self,
response: reqwest::blocking::Response,
message_only_is_failure: bool,
) -> ZBResult<(u16, String)> {
let (status, content_type, content) = Self::read_api_body(response)?;
crate::utility::http_error::ensure_json_api_body(status, &content_type, &content)
.map_err(|error| self.redact_error(error))?;
if message_only_is_failure {
crate::utility::http_error::reject_message_only_body(status, &content_type, &content)
.map_err(|error| self.redact_error(error))?;
}
Ok((status, content))
}
fn get_credits_from_body(status: u16, string_value: &str) -> ZBResult<i64> {
let body = parse_json_body_with_status::<serde_json::Value>(status, string_value)?;
match body.get("Credits") {
Some(serde_json::Value::String(credits)) => credits.parse::<i64>().ok(),
Some(serde_json::Value::Number(credits)) => credits.as_i64(),
_ => None,
}
.ok_or_else(|| ZBError::api(status, UNEXPECTED_JSON_SHAPE_MESSAGE))
}
#[cfg(test)]
fn get_credits_from_string(string_value: String) -> ZBResult<i64> {
Self::get_credits_from_body(200, &string_value)
}
pub fn get_credits(&self) -> ZBResult<i64> {
let query_args = HashMap::new();
let (status, response_content) = self.get_request_with_status(
self.url_provider.url_of(ENDPOINT_CREDITS), query_args, false
)?;
Self::get_credits_from_body(status, &response_content)
}
pub fn get_api_usage(&self, start_date: NaiveDate, end_date: NaiveDate) -> ZBResult<ApiUsage> {
let start_date_str = start_date.format("%F").to_string();
let end_date_str = end_date.format("%F").to_string();
let query_args = HashMap::from([
("start_date", start_date_str.as_str()),
("end_date", end_date_str.as_str()),
]);
let (status, response_content) = self.get_request_with_status(
self.url_provider.url_of(ENDPOINT_API_USAGE), query_args, false
)?;
let api_usage = parse_json_body_with_status::<ApiUsage>(status, &response_content)?;
Ok(api_usage)
}
pub fn get_api_usage_overall(&self) -> ZBResult<ApiUsage> {
let start_date = NaiveDate::from_ymd_opt(2000, 1, 1).unwrap();
let end_date = Utc::now().naive_local().date();
self.get_api_usage(start_date, end_date)
}
pub fn get_activity_data(&self, email: &str) -> ZBResult<ActivityData> {
let query_args = HashMap::from([
("email", email),
]);
let (status, response_content) = self.get_request_with_status(
self.url_provider.url_of(ENDPOINT_ACTIVITY_DATA), query_args, false
)?;
let activity_data = crate::utility::http_error::parse_unless_message_beside_nulls::<ActivityData>(
status, &response_content, &["found", "active_in_days"],
)?;
Ok(activity_data)
}
pub fn score_email(&self, email: &str) -> ZBResult<ScoreEmailResponse> {
let query_args = HashMap::from([("email", email)]);
let (status, response_content) = self.get_request_with_status(
self.url_provider.url_of(ENDPOINT_SCORING),
query_args,
false,
)?;
let score = crate::utility::http_error::parse_unless_message_beside_nulls::<ScoreEmailResponse>(
status, &response_content, &["email", "score"],
)?;
Ok(score)
}
pub fn score_email_post(&self, email: &str) -> ZBResult<ScoreEmailResponse> {
let form_args = HashMap::from([("email", email)]);
let (status, response_content) = self.form_post_request_with_status(
self.url_provider.url_of(ENDPOINT_SCORING),
form_args,
)?;
let score = crate::utility::http_error::parse_unless_message_beside_nulls::<ScoreEmailResponse>(
status, &response_content, &["email", "score"],
)?;
Ok(score)
}
#[deprecated(
since = "1.2.0",
note = "Use `find_email_v2` instead. The new version supports both domain and company_name parameters."
)]
pub fn find_email(&self, domain: &str, first_name: &str, middle_name: &str, last_name: &str) -> ZBResult<FindEmailResponse> {
let mut query_args = HashMap::from([
("domain", domain),
]);
if !first_name.is_empty() {
query_args.insert("first_name", first_name);
}
if !middle_name.is_empty() {
query_args.insert("middle_name", middle_name);
}
if !last_name.is_empty() {
query_args.insert("last_name", last_name);
}
let (status, response_content) = self.get_request_with_status(
self.url_provider.url_of(ENDPOINT_EMAIL_FINDER), query_args, true
)?;
let activity_data = parse_json_body_with_status::<FindEmailResponse>(status, &response_content)?;
Ok(activity_data)
}
pub fn find_email_v2(&self) -> FindEmailV2Builder<'_> {
FindEmailV2Builder {
client: self,
first_name: None,
domain: None,
company_name: None,
middle_name: None,
last_name: None,
}
}
#[deprecated(
since = "1.2.0",
note = "Use `domain_search_v2` instead. The new version supports both domain and company_name parameters."
)]
#[allow(deprecated)] pub fn domain_search(&self, domain: &str) -> ZBResult<FindEmailResponse> {
self.find_email(domain, "", "", "")
}
pub fn domain_search_v2(&self) -> DomainSearchV2Builder<'_> {
DomainSearchV2Builder {
client: self,
domain: None,
company_name: None,
}
}
}
impl<'a> FindEmailV2Builder<'a> {
pub fn first_name(mut self, name: &'a str) -> Self {
self.first_name = Some(name);
self
}
pub fn domain(mut self, domain: &'a str) -> Self {
self.domain = Some(domain);
self
}
pub fn company_name(mut self, company: &'a str) -> Self {
self.company_name = Some(company);
self
}
pub fn middle_name(mut self, name: &'a str) -> Self {
self.middle_name = Some(name);
self
}
pub fn last_name(mut self, name: &'a str) -> Self {
self.last_name = Some(name);
self
}
pub fn call(self) -> ZBResult<FindEmailResponseV2> {
if let Some("") = self.first_name {
return Err(ZBError::explicit("first_name cannot be empty"));
}
match (self.domain, self.company_name) {
(Some(d), None) => {
if d.is_empty() {
return Err(ZBError::explicit("domain cannot be empty"));
}
}
(None, Some(c)) => {
if c.is_empty() {
return Err(ZBError::explicit("company_name cannot be empty"));
}
}
(Some(_), Some(_)) => {
return Err(ZBError::explicit("exactly one of domain or company_name must be provided, not both"));
}
(None, None) => {
return Err(ZBError::explicit("either domain or company_name must be provided"));
}
}
let mut query_args = HashMap::new();
if let Some(first) = self.first_name {
query_args.insert("first_name", first);
}
if let Some(d) = self.domain {
query_args.insert("domain", d);
}
if let Some(c) = self.company_name {
query_args.insert("company_name", c);
}
if let Some(middle) = self.middle_name {
if !middle.is_empty() {
query_args.insert("middle_name", middle);
}
}
if let Some(last) = self.last_name {
if !last.is_empty() {
query_args.insert("last_name", last);
}
}
let (status, response_content) = self.client.get_request_with_status(
self.client.url_provider.url_of(ENDPOINT_EMAIL_FINDER), query_args, true
)?;
let body = parse_json_body_with_status::<serde_json::Value>(status, &response_content)?;
if let Some(object) = body.as_object() {
const NAMES: [&str; 6] = [
"email",
"domain",
"email_confidence",
"company_name",
"did_you_mean",
"failure_reason",
];
if crate::utility::http_error::object_has_message_key(object)
&& !crate::utility::http_error::object_has_non_null_named_field(object, &NAMES)
{
return Err(ZBError::api(status, UNEXPECTED_JSON_SHAPE_MESSAGE));
}
}
let find_email_response =
parse_json_body_with_status::<FindEmailResponseV2>(status, &response_content)?;
Ok(find_email_response)
}
}
impl<'a> DomainSearchV2Builder<'a> {
pub fn domain(mut self, domain: &'a str) -> Self {
self.domain = Some(domain);
self
}
pub fn company_name(mut self, company: &'a str) -> Self {
self.company_name = Some(company);
self
}
pub fn call(self) -> ZBResult<DomainSearchResponseV2> {
match (self.domain, self.company_name) {
(Some(d), None) => {
if d.is_empty() {
return Err(ZBError::explicit("domain cannot be empty"));
}
}
(None, Some(c)) => {
if c.is_empty() {
return Err(ZBError::explicit("company_name cannot be empty"));
}
}
(Some(_), Some(_)) => {
return Err(ZBError::explicit("exactly one of domain or company_name must be provided, not both"));
}
(None, None) => {
return Err(ZBError::explicit("either domain or company_name must be provided"));
}
}
let mut query_args = HashMap::new();
if let Some(d) = self.domain {
query_args.insert("domain", d);
}
if let Some(c) = self.company_name {
query_args.insert("company_name", c);
}
let (status, response_content) = self.client.get_request_with_status(
self.client.url_provider.url_of(ENDPOINT_EMAIL_FINDER), query_args, true
)?;
let body = parse_json_body_with_status::<serde_json::Value>(status, &response_content)?;
let object = body.as_object();
let named = object.is_some_and(|object| {
DOMAIN_SEARCH_V2_FIELD_NAMES
.iter()
.any(|name| object.contains_key(*name))
});
let real = object.is_some_and(|object| {
crate::utility::http_error::object_has_non_null_named_field(
object,
&DOMAIN_SEARCH_V2_FIELD_NAMES,
)
});
let message = object.is_some_and(crate::utility::http_error::object_has_message_key);
if !named || (message && !real) {
return Err(ZBError::api(status, UNEXPECTED_JSON_SHAPE_MESSAGE));
}
let domain_search_response =
parse_json_body_with_status::<DomainSearchResponseV2>(status, &response_content)?;
Ok(domain_search_response)
}
}
#[cfg(test)]
mod tests {
use crate::utility::mock_constants::CREDITS_RESPONSE_OK;
use crate::utility::mock_constants::CREDITS_RESPONSE_NEGATIVE;
use super::*;
#[test]
fn test_credits_negative() {
let credits = ZeroBounce::get_credits_from_string(CREDITS_RESPONSE_NEGATIVE.to_string());
assert!(credits.is_ok());
let amount = credits.unwrap();
assert_eq!(amount, -1);
}
#[test]
fn test_credits_ok() {
let credits = ZeroBounce::get_credits_from_string(CREDITS_RESPONSE_OK.to_string());
assert!(credits.is_ok());
let amount = credits.unwrap();
assert_eq!(amount, 123456);
}
}