1use std::collections::HashMap;
2
3use crate::utility::bulk_get_file::should_treat_get_file_body_as_error;
4use crate::utility::format_get_file_error_message;
5use crate::utility::structures::bulk::{
6 ZBBulkResponse, ZBFile, ZBFileFeedback, ZBFileStatus, ZBGetFileOptions,
7};
8use crate::utility::{ZBError, ZBResult};
9use crate::utility::{
10 ENDPOINT_FILE_DELETE, ENDPOINT_FILE_RESULT, ENDPOINT_FILE_SEND, ENDPOINT_FILE_STATUS,
11};
12use crate::utility::{
13 ENDPOINT_SCORING_DELETE, ENDPOINT_SCORING_RESULT, ENDPOINT_SCORING_SEND,
14 ENDPOINT_SCORING_STATUS,
15};
16use crate::ZeroBounce;
17
18impl ZeroBounce {
19 fn generic_file_submit(&self, endpoint: &str, zb_file: &ZBFile) -> ZBResult<ZBFileFeedback> {
20 self.require_key()?;
21 let include_person_columns = endpoint == ENDPOINT_FILE_SEND;
22 let mut multi_part_form = zb_file
23 .generate_multipart_with(include_person_columns)?
24 .text("api_key", self.api_key.clone());
25
26 if endpoint == ENDPOINT_FILE_SEND {
27 if let Some(v) = zb_file.allow_phase_2 {
28 multi_part_form = multi_part_form.text("allow_phase_2", v.to_string());
29 }
30 }
31
32 let url = crate::parse_request_url(&self.url_provider.bulk_url_of(endpoint))?;
33 let response = self.client.post(url).multipart(multi_part_form).send()?;
34
35 let (status, response_content) = self.read_api_response_with_status(response, false)?;
36 crate::utility::http_error::parse_unless_message_beside_nulls(status, &response_content, &["success", "file_name", "file_id"])
37 }
38
39 fn generic_file_status_check(&self, endpoint: &str, file_id: &str) -> ZBResult<ZBFileStatus> {
40 let query_args = HashMap::from([("file_id", file_id)]);
41
42 let (status, response_content) = self.get_request_with_status(
43 self.url_provider.bulk_url_of(endpoint),
44 query_args,
45 false,
46 )?;
47
48 crate::utility::http_error::parse_json_body_with_status(status, &response_content)
49 }
50
51 pub(crate) fn result_fetch_at(
56 &self,
57 url: String,
58 file_id: &str,
59 options: Option<&ZBGetFileOptions>,
60 is_scoring: bool,
61 ) -> ZBResult<ZBBulkResponse> {
62 self.result_fetch_unredacted(url, file_id, options, is_scoring)
63 .map_err(|error| self.redact_error(error))
64 }
65
66 fn result_fetch_unredacted(
69 &self,
70 url: String,
71 file_id: &str,
72 options: Option<&ZBGetFileOptions>,
73 is_scoring: bool,
74 ) -> ZBResult<ZBBulkResponse> {
75 self.require_key()?;
76 let mut query: Vec<(&str, String)> = vec![
77 ("api_key", self.api_key.clone()),
78 ("file_id", file_id.to_string()),
79 ];
80 if let Some(opts) = options {
81 if let Some(dt) = &opts.download_type {
82 query.push(("download_type", dt.clone()));
83 }
84 if !is_scoring {
85 if let Some(ad) = opts.activity_data {
86 query.push((
87 "activity_data",
88 if ad { "true".into() } else { "false".into() },
89 ));
90 }
91 }
92 }
93
94 let url = crate::parse_request_url(&url)?;
95 let response = self.client.get(url).query(&query).send()?;
96
97 let status = response.status();
101 if status.is_redirection() {
104 return Err(ZBError::api(
105 status.as_u16(),
106 crate::utility::http_error::REDIRECT_REFUSED_MESSAGE,
107 ));
108 }
109
110 let encodings: Vec<Option<&str>> = response
113 .headers()
114 .get_all(reqwest::header::CONTENT_ENCODING)
115 .iter()
116 .map(|value| value.to_str().ok())
117 .collect();
118 if crate::utility::bulk_get_file::content_encoding_is_transformed(encodings) {
119 return Err(ZBError::api(
120 status.as_u16(),
121 crate::utility::bulk_get_file::ENCODED_BODY_MESSAGE,
122 ));
123 }
124
125 let content_type = response
126 .headers()
127 .get(reqwest::header::CONTENT_TYPE)
128 .ok_or_else(|| ZBError::api(status.as_u16(), "content type not specified in response"))?
129 .to_str()
130 .map_err(|e| ZBError::api(status.as_u16(), e.to_string()))?
131 .to_string();
132
133 let body_bytes = crate::utility::read_limited(
134 response,
135 if status.is_success() {
138 crate::utility::MAX_GETFILE_BYTES
139 } else {
140 crate::utility::MAX_JSON_RESPONSE_BYTES
141 },
142 status.as_u16(),
143 "getfile response exceeds size limit",
144 )?;
145 let body_str = String::from_utf8_lossy(&body_bytes).into_owned();
146 let sniffed = crate::utility::bulk_get_file::strip_leading_boms(&body_str);
150
151 if crate::utility::http_error::body_looks_like_html(sniffed)
153 || crate::utility::http_error::content_type_is_html(&content_type)
154 || !status.is_success()
155 || sniffed.trim().is_empty()
156 {
157 return Err(ZBError::api(
158 status.as_u16(),
159 crate::utility::http_error::message_from_body(
160 status.as_u16(),
161 &content_type,
162 sniffed,
163 ),
164 ));
165 }
166
167 if should_treat_get_file_body_as_error(&body_str, &content_type) {
168 let message = if serde_json::from_str::<serde_json::Value>(sniffed.trim()).is_ok() {
171 crate::utility::http_error::strip_error_prefix(&format_get_file_error_message(
172 sniffed.trim(),
173 ))
174 } else {
175 String::new()
176 };
177 if message.is_empty()
178 || message.len() > 500
179 || crate::utility::http_error::body_looks_like_html(&message)
180 {
181 return Err(ZBError::api(
182 status.as_u16(),
183 crate::utility::http_error::message_from_body(
184 status.as_u16(),
185 &content_type,
186 sniffed,
187 ),
188 ));
189 }
190 return Err(ZBError::api(status.as_u16(), message));
191 }
192
193 Ok(ZBBulkResponse::Content(body_bytes.into()))
194 }
195
196 fn generic_result_fetch(
197 &self,
198 endpoint: &str,
199 file_id: &str,
200 options: Option<&ZBGetFileOptions>,
201 is_scoring: bool,
202 ) -> ZBResult<ZBBulkResponse> {
203 self.result_fetch_at(
204 self.url_provider.bulk_url_of(endpoint),
205 file_id,
206 options,
207 is_scoring,
208 )
209 }
210
211 pub(crate) fn result_delete_at(&self, url: String, file_id: &str) -> ZBResult<ZBFileFeedback> {
213 let query_args = HashMap::from([("file_id", file_id)]);
214
215 let (status, response_content) = self.get_request_with_status(url, query_args, false)?;
216
217 crate::utility::http_error::parse_unless_message_beside_nulls(status, &response_content, &["success", "file_name", "file_id"])
218 }
219
220 fn generic_result_delete(&self, endpoint: &str, file_id: &str) -> ZBResult<ZBFileFeedback> {
221 self.result_delete_at(self.url_provider.bulk_url_of(endpoint), file_id)
222 }
223
224 pub fn bulk_validation_file_submit(&self, zb_file: &ZBFile) -> ZBResult<ZBFileFeedback> {
225 self.generic_file_submit(ENDPOINT_FILE_SEND, zb_file)
226 }
227
228 pub fn bulk_validation_file_status_check(&self, file_id: &str) -> ZBResult<ZBFileStatus> {
229 self.generic_file_status_check(ENDPOINT_FILE_STATUS, file_id)
230 }
231
232 pub fn bulk_validation_result_fetch(&self, file_id: &str) -> ZBResult<ZBBulkResponse> {
233 self.generic_result_fetch(ENDPOINT_FILE_RESULT, file_id, None, false)
234 }
235
236 pub fn bulk_validation_result_fetch_with_options(
238 &self,
239 file_id: &str,
240 options: &ZBGetFileOptions,
241 ) -> ZBResult<ZBBulkResponse> {
242 self.generic_result_fetch(ENDPOINT_FILE_RESULT, file_id, Some(options), false)
243 }
244
245 pub fn bulk_validation_result_delete(&self, file_id: &str) -> ZBResult<ZBFileFeedback> {
246 self.generic_result_delete(ENDPOINT_FILE_DELETE, file_id)
247 }
248
249 pub fn ai_scoring_file_submit(&self, zb_file: &ZBFile) -> ZBResult<ZBFileFeedback> {
250 self.generic_file_submit(ENDPOINT_SCORING_SEND, zb_file)
251 }
252
253 pub fn ai_scoring_file_status_check(&self, file_id: &str) -> ZBResult<ZBFileStatus> {
254 self.generic_file_status_check(ENDPOINT_SCORING_STATUS, file_id)
255 }
256
257 pub fn ai_scoring_result_fetch(&self, file_id: &str) -> ZBResult<ZBBulkResponse> {
258 self.generic_result_fetch(ENDPOINT_SCORING_RESULT, file_id, None, true)
259 }
260
261 pub fn ai_scoring_result_fetch_with_options(
263 &self,
264 file_id: &str,
265 options: &ZBGetFileOptions,
266 ) -> ZBResult<ZBBulkResponse> {
267 self.generic_result_fetch(ENDPOINT_SCORING_RESULT, file_id, Some(options), true)
268 }
269
270 pub fn ai_scoring_result_delete(&self, file_id: &str) -> ZBResult<ZBFileFeedback> {
271 self.generic_result_delete(ENDPOINT_SCORING_DELETE, file_id)
272 }
273}