1#[derive(Debug, Clone, Default)]
4#[doc = "调用 API 所用的路径参数"]
5pub struct PathParams {
6 r#from_url: Option<std::borrow::Cow<'static, str>>,
7 r#to_entry: Option<std::borrow::Cow<'static, str>>,
8 r#host: Option<std::borrow::Cow<'static, str>>,
9 extended_segments: Vec<std::borrow::Cow<'static, str>>,
10}
11impl PathParams {
12 #[inline]
13 #[must_use]
14 #[doc = "追加新的路径段"]
15 pub fn push_segment(mut self, segment: impl Into<std::borrow::Cow<'static, str>>) -> Self {
16 self.extended_segments.push(segment.into());
17 self
18 }
19 fn build(self) -> Vec<std::borrow::Cow<'static, str>> {
20 let mut all_segments: Vec<_> = Default::default();
21 if let Some(segment) = self.r#from_url {
22 all_segments.push(segment);
23 }
24 if let Some(segment) = self.r#to_entry {
25 all_segments.push(std::borrow::Cow::Borrowed("to"));
26 all_segments.push(segment);
27 }
28 if let Some(segment) = self.r#host {
29 all_segments.push(std::borrow::Cow::Borrowed("host"));
30 all_segments.push(segment);
31 }
32 all_segments.extend(self.extended_segments);
33 all_segments
34 }
35}
36impl PathParams {
37 #[inline]
38 #[must_use]
39 #[doc = "指定抓取的 URL"]
40 pub fn set_from_url_as_str(mut self, value: impl Into<std::borrow::Cow<'static, str>>) -> Self {
41 self.r#from_url = Some(qiniu_utils::base64::urlsafe(value.into().as_bytes()).into());
42 self
43 }
44 #[inline]
45 #[must_use]
46 #[doc = "指定目标对象空间与目标对象名称"]
47 pub fn set_to_entry_as_str(mut self, value: impl Into<std::borrow::Cow<'static, str>>) -> Self {
48 self.r#to_entry = Some(qiniu_utils::base64::urlsafe(value.into().as_bytes()).into());
49 self
50 }
51 #[inline]
52 #[must_use]
53 #[doc = "指定抓取 URL 请求用的 HOST 参数"]
54 pub fn set_host_as_str(mut self, value: impl Into<std::borrow::Cow<'static, str>>) -> Self {
55 self.r#host = Some(qiniu_utils::base64::urlsafe(value.into().as_bytes()).into());
56 self
57 }
58}
59#[derive(Clone, Debug, serde :: Serialize, serde :: Deserialize)]
60#[serde(transparent)]
61#[doc = "获取 API 所用的响应体参数"]
62pub struct ResponseBody(serde_json::Value);
63impl ResponseBody {
64 #[allow(dead_code)]
65 pub(crate) fn new(value: serde_json::Value) -> Self {
66 Self(value)
67 }
68}
69impl Default for ResponseBody {
70 #[inline]
71 fn default() -> Self {
72 Self(serde_json::Value::Object(Default::default()))
73 }
74}
75impl From<ResponseBody> for serde_json::Value {
76 #[inline]
77 fn from(val: ResponseBody) -> Self {
78 val.0
79 }
80}
81impl AsRef<serde_json::Value> for ResponseBody {
82 #[inline]
83 fn as_ref(&self) -> &serde_json::Value {
84 &self.0
85 }
86}
87impl AsMut<serde_json::Value> for ResponseBody {
88 #[inline]
89 fn as_mut(&mut self) -> &mut serde_json::Value {
90 &mut self.0
91 }
92}
93impl ResponseBody {
94 #[doc = "获取 抓取的对象内容的 Etag 值"]
95 pub fn get_hash_as_str(&self) -> &str {
96 self.0.as_object().unwrap().get("hash").unwrap().as_str().unwrap()
97 }
98}
99impl ResponseBody {
100 #[doc = "设置 抓取的对象内容的 Etag 值"]
101 pub fn set_hash_as_str(&mut self, new: String) -> Option<String> {
102 self.0
103 .as_object_mut()
104 .unwrap()
105 .insert("hash".to_owned(), new.into())
106 .and_then(|val| match val {
107 serde_json::Value::String(s) => Some(s),
108 _ => None,
109 })
110 }
111}
112impl ResponseBody {
113 #[doc = "获取 抓取后保存的对象名称"]
114 pub fn get_object_name_as_str(&self) -> &str {
115 self.0.as_object().unwrap().get("key").unwrap().as_str().unwrap()
116 }
117}
118impl ResponseBody {
119 #[doc = "设置 抓取后保存的对象名称"]
120 pub fn set_object_name_as_str(&mut self, new: String) -> Option<String> {
121 self.0
122 .as_object_mut()
123 .unwrap()
124 .insert("key".to_owned(), new.into())
125 .and_then(|val| match val {
126 serde_json::Value::String(s) => Some(s),
127 _ => None,
128 })
129 }
130}
131impl ResponseBody {
132 #[doc = "获取 对象大小,单位为字节"]
133 pub fn get_size_as_i64(&self) -> i64 {
134 self.0.as_object().unwrap().get("fsize").unwrap().as_i64().unwrap()
135 }
136}
137impl ResponseBody {
138 #[doc = "设置 对象大小,单位为字节"]
139 pub fn set_size_as_i64(&mut self, new: i64) -> Option<i64> {
140 self.0
141 .as_object_mut()
142 .unwrap()
143 .insert("fsize".to_owned(), new.into())
144 .and_then(|val| val.as_i64())
145 }
146}
147impl ResponseBody {
148 #[doc = "获取 对象大小,单位为字节"]
149 pub fn get_size_as_u64(&self) -> u64 {
150 self.0.as_object().unwrap().get("fsize").unwrap().as_u64().unwrap()
151 }
152}
153impl ResponseBody {
154 #[doc = "设置 对象大小,单位为字节"]
155 pub fn set_size_as_u64(&mut self, new: u64) -> Option<u64> {
156 self.0
157 .as_object_mut()
158 .unwrap()
159 .insert("fsize".to_owned(), new.into())
160 .and_then(|val| val.as_u64())
161 }
162}
163impl ResponseBody {
164 #[doc = "获取 对象 MIME 类型"]
165 pub fn get_mime_type_as_str(&self) -> &str {
166 self.0.as_object().unwrap().get("mimeType").unwrap().as_str().unwrap()
167 }
168}
169impl ResponseBody {
170 #[doc = "设置 对象 MIME 类型"]
171 pub fn set_mime_type_as_str(&mut self, new: String) -> Option<String> {
172 self.0
173 .as_object_mut()
174 .unwrap()
175 .insert("mimeType".to_owned(), new.into())
176 .and_then(|val| match val {
177 serde_json::Value::String(s) => Some(s),
178 _ => None,
179 })
180 }
181}
182#[doc = "API 调用客户端"]
183#[derive(Debug, Clone)]
184pub struct Client<'client>(&'client qiniu_http_client::HttpClient);
185impl<'client> Client<'client> {
186 pub(super) fn new(http_client: &'client qiniu_http_client::HttpClient) -> Self {
187 Self(http_client)
188 }
189}
190impl<'client> Client<'client> {
191 #[inline]
192 #[doc = "创建一个新的阻塞请求,该方法的异步版本为 [`Self::new_async_request`]"]
193 pub fn new_request<E: qiniu_http_client::EndpointsProvider + 'client>(
194 &self,
195 endpoints_provider: E,
196 path_params: PathParams,
197 credential: impl qiniu_http_client::credential::CredentialProvider + Clone + 'client,
198 ) -> SyncRequestBuilder<'client, E> {
199 RequestBuilder({
200 let mut builder = self.0.post(&[qiniu_http_client::ServiceName::Io], endpoints_provider);
201 builder.authorization(qiniu_http_client::Authorization::v2(credential));
202 builder.idempotent(qiniu_http_client::Idempotent::Always);
203 builder.path(crate::base_utils::join_path("/fetch", "", path_params.build()));
204 builder.accept_json();
205 builder
206 })
207 }
208 #[inline]
209 #[cfg(feature = "async")]
210 #[doc = "创建一个新的异步请求"]
211 pub fn new_async_request<E: qiniu_http_client::EndpointsProvider + 'client>(
212 &self,
213 endpoints_provider: E,
214 path_params: PathParams,
215 credential: impl qiniu_http_client::credential::CredentialProvider + Clone + 'client,
216 ) -> AsyncRequestBuilder<'client, E> {
217 RequestBuilder({
218 let mut builder = self
219 .0
220 .async_post(&[qiniu_http_client::ServiceName::Io], endpoints_provider);
221 builder.authorization(qiniu_http_client::Authorization::v2(credential));
222 builder.idempotent(qiniu_http_client::Idempotent::Always);
223 builder.path(crate::base_utils::join_path("/fetch", "", path_params.build()));
224 builder.accept_json();
225 builder
226 })
227 }
228}
229#[derive(Debug)]
230#[doc = "API 请求构造器"]
231pub struct RequestBuilder<'req, B, E>(qiniu_http_client::RequestBuilder<'req, B, E>);
232#[doc = "API 阻塞请求构造器"]
233pub type SyncRequestBuilder<'req, E> = RequestBuilder<'req, qiniu_http_client::SyncRequestBody<'req>, E>;
234#[cfg(feature = "async")]
235#[cfg_attr(feature = "docs", doc(cfg(feature = "async")))]
236#[doc = "API 异步请求构造器"]
237pub type AsyncRequestBuilder<'req, E> = RequestBuilder<'req, qiniu_http_client::AsyncRequestBody<'req>, E>;
238impl<'req, B, E> RequestBuilder<'req, B, E> {
239 #[inline]
240 #[doc = "设置是否使用 HTTPS"]
241 pub fn use_https(&mut self, use_https: bool) -> &mut Self {
242 self.0.use_https(use_https);
243 self
244 }
245 #[inline]
246 #[doc = "设置 HTTP 协议版本"]
247 pub fn version(&mut self, version: qiniu_http_client::http::Version) -> &mut Self {
248 self.0.version(version);
249 self
250 }
251 #[inline]
252 #[doc = "设置 HTTP 请求头"]
253 pub fn headers(
254 &mut self,
255 headers: impl Into<std::borrow::Cow<'req, qiniu_http_client::http::HeaderMap>>,
256 ) -> &mut Self {
257 self.0.headers(headers);
258 self
259 }
260 #[inline]
261 #[doc = "添加 HTTP 请求头"]
262 pub fn set_header(
263 &mut self,
264 header_name: impl qiniu_http_client::http::header::IntoHeaderName,
265 header_value: impl Into<qiniu_http_client::http::HeaderValue>,
266 ) -> &mut Self {
267 self.0.set_header(header_name, header_value);
268 self
269 }
270 #[inline]
271 #[doc = "设置查询参数"]
272 pub fn query(&mut self, query: impl Into<std::borrow::Cow<'req, str>>) -> &mut Self {
273 self.0.query(query);
274 self
275 }
276 #[inline]
277 #[doc = "设置查询参数"]
278 pub fn query_pairs(&mut self, query_pairs: impl Into<Vec<qiniu_http_client::QueryPair<'req>>>) -> &mut Self {
279 self.0.query_pairs(query_pairs);
280 self
281 }
282 #[inline]
283 #[doc = "追加查询参数"]
284 pub fn append_query_pair(
285 &mut self,
286 query_pair_key: impl Into<qiniu_http_client::QueryPairKey<'req>>,
287 query_pair_value: impl Into<qiniu_http_client::QueryPairValue<'req>>,
288 ) -> &mut Self {
289 self.0.append_query_pair(query_pair_key, query_pair_value);
290 self
291 }
292 #[inline]
293 #[doc = "设置扩展信息"]
294 pub fn extensions(&mut self, extensions: qiniu_http_client::http::Extensions) -> &mut Self {
295 self.0.extensions(extensions);
296 self
297 }
298 #[doc = "添加扩展信息"]
299 #[inline]
300 pub fn add_extension<T: Send + Sync + 'static>(&mut self, val: T) -> &mut Self {
301 self.0.add_extension(val);
302 self
303 }
304 #[inline]
305 #[doc = "上传进度回调函数"]
306 pub fn on_uploading_progress(
307 &mut self,
308 callback: impl Fn(
309 &dyn qiniu_http_client::SimplifiedCallbackContext,
310 qiniu_http_client::http::TransferProgressInfo,
311 ) -> anyhow::Result<()>
312 + Send
313 + Sync
314 + 'req,
315 ) -> &mut Self {
316 self.0.on_uploading_progress(callback);
317 self
318 }
319 #[inline]
320 #[doc = "设置响应状态码回调函数"]
321 pub fn on_receive_response_status(
322 &mut self,
323 callback: impl Fn(
324 &dyn qiniu_http_client::SimplifiedCallbackContext,
325 qiniu_http_client::http::StatusCode,
326 ) -> anyhow::Result<()>
327 + Send
328 + Sync
329 + 'req,
330 ) -> &mut Self {
331 self.0.on_receive_response_status(callback);
332 self
333 }
334 #[inline]
335 #[doc = "设置响应 HTTP 头回调函数"]
336 pub fn on_receive_response_header(
337 &mut self,
338 callback: impl Fn(
339 &dyn qiniu_http_client::SimplifiedCallbackContext,
340 &qiniu_http_client::http::HeaderName,
341 &qiniu_http_client::http::HeaderValue,
342 ) -> anyhow::Result<()>
343 + Send
344 + Sync
345 + 'req,
346 ) -> &mut Self {
347 self.0.on_receive_response_header(callback);
348 self
349 }
350 #[inline]
351 #[doc = "设置域名解析前回调函数"]
352 pub fn on_to_resolve_domain(
353 &mut self,
354 callback: impl Fn(&mut dyn qiniu_http_client::CallbackContext, &str) -> anyhow::Result<()> + Send + Sync + 'req,
355 ) -> &mut Self {
356 self.0.on_to_resolve_domain(callback);
357 self
358 }
359 #[inline]
360 #[doc = "设置域名解析成功回调函数"]
361 pub fn on_domain_resolved(
362 &mut self,
363 callback: impl Fn(
364 &mut dyn qiniu_http_client::CallbackContext,
365 &str,
366 &qiniu_http_client::ResolveAnswers,
367 ) -> anyhow::Result<()>
368 + Send
369 + Sync
370 + 'req,
371 ) -> &mut Self {
372 self.0.on_domain_resolved(callback);
373 self
374 }
375 #[inline]
376 #[doc = "设置 IP 地址选择前回调函数"]
377 pub fn on_to_choose_ips(
378 &mut self,
379 callback: impl Fn(&mut dyn qiniu_http_client::CallbackContext, &[qiniu_http_client::IpAddrWithPort]) -> anyhow::Result<()>
380 + Send
381 + Sync
382 + 'req,
383 ) -> &mut Self {
384 self.0.on_to_choose_ips(callback);
385 self
386 }
387 #[inline]
388 #[doc = "设置 IP 地址选择成功回调函数"]
389 pub fn on_ips_chosen(
390 &mut self,
391 callback: impl Fn(
392 &mut dyn qiniu_http_client::CallbackContext,
393 &[qiniu_http_client::IpAddrWithPort],
394 &[qiniu_http_client::IpAddrWithPort],
395 ) -> anyhow::Result<()>
396 + Send
397 + Sync
398 + 'req,
399 ) -> &mut Self {
400 self.0.on_ips_chosen(callback);
401 self
402 }
403 #[inline]
404 #[doc = "设置 HTTP 请求签名前回调函数"]
405 pub fn on_before_request_signed(
406 &mut self,
407 callback: impl Fn(&mut dyn qiniu_http_client::ExtendedCallbackContext) -> anyhow::Result<()> + Send + Sync + 'req,
408 ) -> &mut Self {
409 self.0.on_before_request_signed(callback);
410 self
411 }
412 #[inline]
413 #[doc = "设置 HTTP 请求前回调函数"]
414 pub fn on_after_request_signed(
415 &mut self,
416 callback: impl Fn(&mut dyn qiniu_http_client::ExtendedCallbackContext) -> anyhow::Result<()> + Send + Sync + 'req,
417 ) -> &mut Self {
418 self.0.on_after_request_signed(callback);
419 self
420 }
421 #[inline]
422 #[doc = "设置响应成功回调函数"]
423 pub fn on_response(
424 &mut self,
425 callback: impl Fn(
426 &mut dyn qiniu_http_client::ExtendedCallbackContext,
427 &qiniu_http_client::http::ResponseParts,
428 ) -> anyhow::Result<()>
429 + Send
430 + Sync
431 + 'req,
432 ) -> &mut Self {
433 self.0.on_response(callback);
434 self
435 }
436 #[inline]
437 #[doc = "设置响应错误回调函数"]
438 pub fn on_error(
439 &mut self,
440 callback: impl Fn(
441 &mut dyn qiniu_http_client::ExtendedCallbackContext,
442 &mut qiniu_http_client::ResponseError,
443 ) -> anyhow::Result<()>
444 + Send
445 + Sync
446 + 'req,
447 ) -> &mut Self {
448 self.0.on_error(callback);
449 self
450 }
451 #[inline]
452 #[doc = "设置退避前回调函数"]
453 pub fn on_before_backoff(
454 &mut self,
455 callback: impl Fn(&mut dyn qiniu_http_client::ExtendedCallbackContext, std::time::Duration) -> anyhow::Result<()>
456 + Send
457 + Sync
458 + 'req,
459 ) -> &mut Self {
460 self.0.on_before_backoff(callback);
461 self
462 }
463 #[inline]
464 #[doc = "设置退避后回调函数"]
465 pub fn on_after_backoff(
466 &mut self,
467 callback: impl Fn(&mut dyn qiniu_http_client::ExtendedCallbackContext, std::time::Duration) -> anyhow::Result<()>
468 + Send
469 + Sync
470 + 'req,
471 ) -> &mut Self {
472 self.0.on_after_backoff(callback);
473 self
474 }
475 #[inline]
476 #[doc = "获取 HTTP 请求构建器部分参数"]
477 pub fn parts(&self) -> &qiniu_http_client::RequestBuilderParts<'req> {
478 self.0.parts()
479 }
480 #[inline]
481 #[doc = "获取 HTTP 请求构建器部分参数的可变引用"]
482 pub fn parts_mut(&mut self) -> &mut qiniu_http_client::RequestBuilderParts<'req> {
483 self.0.parts_mut()
484 }
485}
486impl<'req, E: qiniu_http_client::EndpointsProvider + Clone + 'req> SyncRequestBuilder<'req, E> {
487 #[doc = "阻塞发起 HTTP 请求"]
488 pub fn call(&mut self) -> qiniu_http_client::ApiResult<qiniu_http_client::Response<ResponseBody>> {
489 let request = &mut self.0;
490 let response = request.call()?;
491 let parsed = response.parse_json()?;
492 Ok(parsed)
493 }
494}
495#[cfg(feature = "async")]
496impl<'req, E: qiniu_http_client::EndpointsProvider + Clone + 'req> AsyncRequestBuilder<'req, E> {
497 #[doc = "异步发起 HTTP 请求"]
498 pub async fn call(&mut self) -> qiniu_http_client::ApiResult<qiniu_http_client::Response<ResponseBody>> {
499 let request = &mut self.0;
500 let response = request.call().await?;
501 let parsed = response.parse_json().await?;
502 Ok(parsed)
503 }
504}