1use std::collections::HashMap;
2
3use serde_json::from_str;
4
5use crate::ZeroBounce;
6use crate::utility::{ZBResult, ZBError};
7use crate::utility::{ENDPOINT_FILE_SEND, ENDPOINT_FILE_STATUS, ENDPOINT_FILE_RESULT, ENDPOINT_FILE_DELETE};
8use crate::utility::{ENDPOINT_SCORING_DELETE, ENDPOINT_SCORING_STATUS, ENDPOINT_SCORING_RESULT, ENDPOINT_SCORING_SEND};
9use crate::utility::structures::bulk::{ZBBulkResponse, ZBFile, ZBFileFeedback, ZBFileStatus, ZBGetFileOptions};
10use crate::utility::bulk_get_file::should_treat_get_file_body_as_error;
11use crate::utility::format_get_file_error_message;
12
13
14impl ZeroBounce {
15
16 fn generic_file_submit(&self, endpoint: &str, zb_file: &ZBFile) -> ZBResult<ZBFileFeedback> {
17 let mut multi_part_form = zb_file.generate_multipart()?
18 .text("api_key", self.api_key.clone());
19
20 if endpoint == ENDPOINT_FILE_SEND {
21 if let Some(v) = zb_file.allow_phase_2 {
22 multi_part_form = multi_part_form.text("allow_phase_2", v.to_string());
23 }
24 }
25
26 let url = self.url_provider.bulk_url_of(endpoint);
27 let response = self.client.post(url)
28 .multipart(multi_part_form)
29 .send()?;
30
31 let response_ok = response.status().is_success();
32 let response_content = response.text()?;
33
34 #[cfg(debug_assertions)]
36 {
37 eprintln!("Raw API response: {}", response_content);
38 }
39
40 if !response_ok {
41 return Err(ZBError::ExplicitError(response_content));
42 }
43
44 let feedback_object = from_str::<ZBFileFeedback>(&response_content)?;
45 Ok(feedback_object)
46 }
47
48 fn generic_file_status_check(&self, endpoint: &str, file_id: &str) -> ZBResult<ZBFileStatus> {
49 let query_args = HashMap::from([
50 ("file_id", file_id),
51 ]);
52
53 let response_content = self.generic_get_request(
54 self.url_provider.bulk_url_of(endpoint), query_args
55 )?;
56
57 let file_status = from_str::<ZBFileStatus>(&response_content)?;
58 Ok(file_status)
59 }
60
61 fn generic_result_fetch(
62 &self,
63 endpoint: &str,
64 file_id: &str,
65 options: Option<&ZBGetFileOptions>,
66 is_scoring: bool,
67 ) -> ZBResult<ZBBulkResponse> {
68 let url = self.url_provider.bulk_url_of(endpoint);
69 let mut query: Vec<(&str, String)> = vec![
70 ("api_key", self.api_key.clone()),
71 ("file_id", file_id.to_string()),
72 ];
73 if let Some(opts) = options {
74 if let Some(dt) = &opts.download_type {
75 query.push(("download_type", dt.clone()));
76 }
77 if !is_scoring {
78 if let Some(ad) = opts.activity_data {
79 query.push(("activity_data", if ad { "true".into() } else { "false".into() }));
80 }
81 }
82 }
83
84 let response = self.client.get(&url).query(&query).send()?;
85
86 let status = response.status();
87 let content_type = response
88 .headers()
89 .get(reqwest::header::CONTENT_TYPE)
90 .ok_or_else(|| ZBError::explicit("content type not specified in response"))?
91 .to_str()
92 .map_err(|e| ZBError::ExplicitError(e.to_string()))?
93 .to_string();
94
95 let body_bytes = response.bytes()?;
96 let body_str = String::from_utf8_lossy(&body_bytes).into_owned();
97
98 #[cfg(debug_assertions)]
99 if !status.is_success() || should_treat_get_file_body_as_error(&body_str, &content_type) {
100 eprintln!("Raw API response: {}", body_str);
101 }
102
103 if !status.is_success() {
104 let msg = if body_str.trim_start().starts_with('{') {
105 format_get_file_error_message(body_str.trim())
106 } else if body_str.is_empty() {
107 format!("HTTP {}", status.as_u16())
108 } else {
109 body_str
110 };
111 return Err(ZBError::ExplicitError(msg));
112 }
113
114 if should_treat_get_file_body_as_error(&body_str, &content_type) {
115 return Err(ZBError::ExplicitError(format_get_file_error_message(body_str.trim())));
116 }
117
118 Ok(ZBBulkResponse::Content(body_bytes))
119 }
120
121 fn generic_result_delete(&self, endpoint: &str, file_id: &str) -> ZBResult<ZBFileFeedback>{
122 let query_args = HashMap::from([
123 ("file_id", file_id),
124 ]);
125
126 let response_content = self.generic_get_request(
127 self.url_provider.bulk_url_of(endpoint), query_args
128 )?;
129
130 let file_status = from_str::<ZBFileFeedback>(&response_content)?;
131 Ok(file_status)
132 }
133
134 pub fn bulk_validation_file_submit(&self, zb_file: &ZBFile) -> ZBResult<ZBFileFeedback> {
135 self.generic_file_submit(ENDPOINT_FILE_SEND, zb_file)
136 }
137
138 pub fn bulk_validation_file_status_check(&self, file_id: &str) -> ZBResult<ZBFileStatus> {
139 self.generic_file_status_check(ENDPOINT_FILE_STATUS, file_id)
140 }
141
142 pub fn bulk_validation_result_fetch(&self, file_id: &str) -> ZBResult<ZBBulkResponse> {
143 self.generic_result_fetch(ENDPOINT_FILE_RESULT, file_id, None, false)
144 }
145
146 pub fn bulk_validation_result_fetch_with_options(
148 &self,
149 file_id: &str,
150 options: &ZBGetFileOptions,
151 ) -> ZBResult<ZBBulkResponse> {
152 self.generic_result_fetch(ENDPOINT_FILE_RESULT, file_id, Some(options), false)
153 }
154
155 pub fn bulk_validation_result_delete(&self, file_id: &str) -> ZBResult<ZBFileFeedback> {
156 self.generic_result_delete(ENDPOINT_FILE_DELETE, file_id)
157 }
158
159 pub fn ai_scoring_file_submit(&self, zb_file: &ZBFile) -> ZBResult<ZBFileFeedback> {
160 self.generic_file_submit(ENDPOINT_SCORING_SEND, zb_file)
161 }
162
163 pub fn ai_scoring_file_status_check(&self, file_id: &str) -> ZBResult<ZBFileStatus> {
164 self.generic_file_status_check(ENDPOINT_SCORING_STATUS, file_id)
165 }
166
167 pub fn ai_scoring_result_fetch(&self, file_id: &str) -> ZBResult<ZBBulkResponse> {
168 self.generic_result_fetch(ENDPOINT_SCORING_RESULT, file_id, None, true)
169 }
170
171 pub fn ai_scoring_result_fetch_with_options(
173 &self,
174 file_id: &str,
175 options: &ZBGetFileOptions,
176 ) -> ZBResult<ZBBulkResponse> {
177 self.generic_result_fetch(ENDPOINT_SCORING_RESULT, file_id, Some(options), true)
178 }
179
180 pub fn ai_scoring_result_delete(&self, file_id: &str) -> ZBResult<ZBFileFeedback> {
181 self.generic_result_delete(ENDPOINT_SCORING_DELETE, file_id)
182 }
183
184
185}