1#[derive(Debug, Clone, Default)]
4#[doc = "调用 API 所用的 URL 查询参数"]
5pub struct QueryParams<'a> {
6 map: indexmap::IndexMap<qiniu_http_client::QueryPairKey<'a>, qiniu_http_client::QueryPairValue<'a>>,
7}
8impl<'a> QueryParams<'a> {
9 #[inline]
10 #[must_use]
11 #[doc = "插入一个新的查询参数对"]
12 pub fn insert(
13 mut self,
14 query_pair_key: qiniu_http_client::QueryPairKey<'a>,
15 query_pair_value: qiniu_http_client::QueryPairValue<'a>,
16 ) -> Self {
17 self.map.insert(query_pair_key, query_pair_value);
18 self
19 }
20 fn build(self) -> Vec<qiniu_http_client::QueryPair<'a>> {
21 Vec::from_iter(self.map)
22 }
23}
24impl<'a> From<QueryParams<'a>> for Vec<qiniu_http_client::QueryPair<'a>> {
25 #[inline]
26 fn from(map: QueryParams<'a>) -> Self {
27 map.build()
28 }
29}
30impl<'a> QueryParams<'a> {
31 #[inline]
32 #[must_use]
33 #[doc = "异步任务 ID"]
34 pub fn set_id_as_str(self, value: impl Into<qiniu_http_client::QueryPairValue<'a>>) -> Self {
35 self.insert("id".into(), value.into())
36 }
37}
38#[derive(Clone, Debug, serde :: Serialize, serde :: Deserialize)]
39#[serde(transparent)]
40#[doc = "获取 API 所用的响应体参数"]
41pub struct ResponseBody(serde_json::Value);
42impl ResponseBody {
43 #[allow(dead_code)]
44 pub(crate) fn new(value: serde_json::Value) -> Self {
45 Self(value)
46 }
47}
48impl Default for ResponseBody {
49 #[inline]
50 fn default() -> Self {
51 Self(serde_json::Value::Object(Default::default()))
52 }
53}
54impl From<ResponseBody> for serde_json::Value {
55 #[inline]
56 fn from(val: ResponseBody) -> Self {
57 val.0
58 }
59}
60impl AsRef<serde_json::Value> for ResponseBody {
61 #[inline]
62 fn as_ref(&self) -> &serde_json::Value {
63 &self.0
64 }
65}
66impl AsMut<serde_json::Value> for ResponseBody {
67 #[inline]
68 fn as_mut(&mut self) -> &mut serde_json::Value {
69 &mut self.0
70 }
71}
72impl ResponseBody {
73 #[doc = "获取 异步任务 ID"]
74 pub fn get_id_as_str(&self) -> &str {
75 self.0.as_object().unwrap().get("id").unwrap().as_str().unwrap()
76 }
77}
78impl ResponseBody {
79 #[doc = "设置 异步任务 ID"]
80 pub fn set_id_as_str(&mut self, new: String) -> Option<String> {
81 self.0
82 .as_object_mut()
83 .unwrap()
84 .insert("id".to_owned(), new.into())
85 .and_then(|val| match val {
86 serde_json::Value::String(s) => Some(s),
87 _ => None,
88 })
89 }
90}
91impl ResponseBody {
92 #[doc = "获取 当前任务前面的排队任务数量,`0` 表示当前任务正在进行,`-1` 表示任务已经至少被处理过一次(可能会进入重试逻辑)"]
93 pub fn get_queued_tasks_count_as_i64(&self) -> i64 {
94 self.0.as_object().unwrap().get("wait").unwrap().as_i64().unwrap()
95 }
96}
97impl ResponseBody {
98 #[doc = "设置 当前任务前面的排队任务数量,`0` 表示当前任务正在进行,`-1` 表示任务已经至少被处理过一次(可能会进入重试逻辑)"]
99 pub fn set_queued_tasks_count_as_i64(&mut self, new: i64) -> Option<i64> {
100 self.0
101 .as_object_mut()
102 .unwrap()
103 .insert("wait".to_owned(), new.into())
104 .and_then(|val| val.as_i64())
105 }
106}
107impl ResponseBody {
108 #[doc = "获取 当前任务前面的排队任务数量,`0` 表示当前任务正在进行,`-1` 表示任务已经至少被处理过一次(可能会进入重试逻辑)"]
109 pub fn get_queued_tasks_count_as_u64(&self) -> u64 {
110 self.0.as_object().unwrap().get("wait").unwrap().as_u64().unwrap()
111 }
112}
113impl ResponseBody {
114 #[doc = "设置 当前任务前面的排队任务数量,`0` 表示当前任务正在进行,`-1` 表示任务已经至少被处理过一次(可能会进入重试逻辑)"]
115 pub fn set_queued_tasks_count_as_u64(&mut self, new: u64) -> Option<u64> {
116 self.0
117 .as_object_mut()
118 .unwrap()
119 .insert("wait".to_owned(), new.into())
120 .and_then(|val| val.as_u64())
121 }
122}
123#[doc = "API 调用客户端"]
124#[derive(Debug, Clone)]
125pub struct Client<'client>(&'client qiniu_http_client::HttpClient);
126impl<'client> Client<'client> {
127 pub(super) fn new(http_client: &'client qiniu_http_client::HttpClient) -> Self {
128 Self(http_client)
129 }
130}
131impl<'client> Client<'client> {
132 #[inline]
133 #[doc = "创建一个新的阻塞请求,该方法的异步版本为 [`Self::new_async_request`]"]
134 pub fn new_request<E: qiniu_http_client::EndpointsProvider + 'client>(
135 &self,
136 endpoints_provider: E,
137 ) -> SyncRequestBuilder<'client, E> {
138 RequestBuilder({
139 let mut builder = self.0.get(&[qiniu_http_client::ServiceName::Api], endpoints_provider);
140 builder.idempotent(qiniu_http_client::Idempotent::Default);
141 builder.path("sisyphus/fetch");
142 builder.accept_json();
143 builder
144 })
145 }
146 #[inline]
147 #[cfg(feature = "async")]
148 #[doc = "创建一个新的异步请求"]
149 pub fn new_async_request<E: qiniu_http_client::EndpointsProvider + 'client>(
150 &self,
151 endpoints_provider: E,
152 ) -> AsyncRequestBuilder<'client, E> {
153 RequestBuilder({
154 let mut builder = self
155 .0
156 .async_get(&[qiniu_http_client::ServiceName::Api], endpoints_provider);
157 builder.idempotent(qiniu_http_client::Idempotent::Default);
158 builder.path("sisyphus/fetch");
159 builder.accept_json();
160 builder
161 })
162 }
163}
164#[derive(Debug)]
165#[doc = "API 请求构造器"]
166pub struct RequestBuilder<'req, B, E>(qiniu_http_client::RequestBuilder<'req, B, E>);
167#[doc = "API 阻塞请求构造器"]
168pub type SyncRequestBuilder<'req, E> = RequestBuilder<'req, qiniu_http_client::SyncRequestBody<'req>, E>;
169#[cfg(feature = "async")]
170#[cfg_attr(feature = "docs", doc(cfg(feature = "async")))]
171#[doc = "API 异步请求构造器"]
172pub type AsyncRequestBuilder<'req, E> = RequestBuilder<'req, qiniu_http_client::AsyncRequestBody<'req>, E>;
173impl<'req, B, E> RequestBuilder<'req, B, E> {
174 #[inline]
175 #[doc = "设置是否使用 HTTPS"]
176 pub fn use_https(&mut self, use_https: bool) -> &mut Self {
177 self.0.use_https(use_https);
178 self
179 }
180 #[inline]
181 #[doc = "设置 HTTP 协议版本"]
182 pub fn version(&mut self, version: qiniu_http_client::http::Version) -> &mut Self {
183 self.0.version(version);
184 self
185 }
186 #[inline]
187 #[doc = "设置 HTTP 请求头"]
188 pub fn headers(
189 &mut self,
190 headers: impl Into<std::borrow::Cow<'req, qiniu_http_client::http::HeaderMap>>,
191 ) -> &mut Self {
192 self.0.headers(headers);
193 self
194 }
195 #[inline]
196 #[doc = "添加 HTTP 请求头"]
197 pub fn set_header(
198 &mut self,
199 header_name: impl qiniu_http_client::http::header::IntoHeaderName,
200 header_value: impl Into<qiniu_http_client::http::HeaderValue>,
201 ) -> &mut Self {
202 self.0.set_header(header_name, header_value);
203 self
204 }
205 #[inline]
206 #[doc = "设置查询参数"]
207 pub fn query(&mut self, query: impl Into<std::borrow::Cow<'req, str>>) -> &mut Self {
208 self.0.query(query);
209 self
210 }
211 #[inline]
212 #[doc = "设置查询参数"]
213 pub fn query_pairs(&mut self, query_pairs: impl Into<Vec<qiniu_http_client::QueryPair<'req>>>) -> &mut Self {
214 self.0.query_pairs(query_pairs);
215 self
216 }
217 #[inline]
218 #[doc = "追加查询参数"]
219 pub fn append_query_pair(
220 &mut self,
221 query_pair_key: impl Into<qiniu_http_client::QueryPairKey<'req>>,
222 query_pair_value: impl Into<qiniu_http_client::QueryPairValue<'req>>,
223 ) -> &mut Self {
224 self.0.append_query_pair(query_pair_key, query_pair_value);
225 self
226 }
227 #[inline]
228 #[doc = "设置扩展信息"]
229 pub fn extensions(&mut self, extensions: qiniu_http_client::http::Extensions) -> &mut Self {
230 self.0.extensions(extensions);
231 self
232 }
233 #[doc = "添加扩展信息"]
234 #[inline]
235 pub fn add_extension<T: Send + Sync + 'static>(&mut self, val: T) -> &mut Self {
236 self.0.add_extension(val);
237 self
238 }
239 #[inline]
240 #[doc = "上传进度回调函数"]
241 pub fn on_uploading_progress(
242 &mut self,
243 callback: impl Fn(
244 &dyn qiniu_http_client::SimplifiedCallbackContext,
245 qiniu_http_client::http::TransferProgressInfo,
246 ) -> anyhow::Result<()>
247 + Send
248 + Sync
249 + 'req,
250 ) -> &mut Self {
251 self.0.on_uploading_progress(callback);
252 self
253 }
254 #[inline]
255 #[doc = "设置响应状态码回调函数"]
256 pub fn on_receive_response_status(
257 &mut self,
258 callback: impl Fn(
259 &dyn qiniu_http_client::SimplifiedCallbackContext,
260 qiniu_http_client::http::StatusCode,
261 ) -> anyhow::Result<()>
262 + Send
263 + Sync
264 + 'req,
265 ) -> &mut Self {
266 self.0.on_receive_response_status(callback);
267 self
268 }
269 #[inline]
270 #[doc = "设置响应 HTTP 头回调函数"]
271 pub fn on_receive_response_header(
272 &mut self,
273 callback: impl Fn(
274 &dyn qiniu_http_client::SimplifiedCallbackContext,
275 &qiniu_http_client::http::HeaderName,
276 &qiniu_http_client::http::HeaderValue,
277 ) -> anyhow::Result<()>
278 + Send
279 + Sync
280 + 'req,
281 ) -> &mut Self {
282 self.0.on_receive_response_header(callback);
283 self
284 }
285 #[inline]
286 #[doc = "设置域名解析前回调函数"]
287 pub fn on_to_resolve_domain(
288 &mut self,
289 callback: impl Fn(&mut dyn qiniu_http_client::CallbackContext, &str) -> anyhow::Result<()> + Send + Sync + 'req,
290 ) -> &mut Self {
291 self.0.on_to_resolve_domain(callback);
292 self
293 }
294 #[inline]
295 #[doc = "设置域名解析成功回调函数"]
296 pub fn on_domain_resolved(
297 &mut self,
298 callback: impl Fn(
299 &mut dyn qiniu_http_client::CallbackContext,
300 &str,
301 &qiniu_http_client::ResolveAnswers,
302 ) -> anyhow::Result<()>
303 + Send
304 + Sync
305 + 'req,
306 ) -> &mut Self {
307 self.0.on_domain_resolved(callback);
308 self
309 }
310 #[inline]
311 #[doc = "设置 IP 地址选择前回调函数"]
312 pub fn on_to_choose_ips(
313 &mut self,
314 callback: impl Fn(&mut dyn qiniu_http_client::CallbackContext, &[qiniu_http_client::IpAddrWithPort]) -> anyhow::Result<()>
315 + Send
316 + Sync
317 + 'req,
318 ) -> &mut Self {
319 self.0.on_to_choose_ips(callback);
320 self
321 }
322 #[inline]
323 #[doc = "设置 IP 地址选择成功回调函数"]
324 pub fn on_ips_chosen(
325 &mut self,
326 callback: impl Fn(
327 &mut dyn qiniu_http_client::CallbackContext,
328 &[qiniu_http_client::IpAddrWithPort],
329 &[qiniu_http_client::IpAddrWithPort],
330 ) -> anyhow::Result<()>
331 + Send
332 + Sync
333 + 'req,
334 ) -> &mut Self {
335 self.0.on_ips_chosen(callback);
336 self
337 }
338 #[inline]
339 #[doc = "设置 HTTP 请求签名前回调函数"]
340 pub fn on_before_request_signed(
341 &mut self,
342 callback: impl Fn(&mut dyn qiniu_http_client::ExtendedCallbackContext) -> anyhow::Result<()> + Send + Sync + 'req,
343 ) -> &mut Self {
344 self.0.on_before_request_signed(callback);
345 self
346 }
347 #[inline]
348 #[doc = "设置 HTTP 请求前回调函数"]
349 pub fn on_after_request_signed(
350 &mut self,
351 callback: impl Fn(&mut dyn qiniu_http_client::ExtendedCallbackContext) -> anyhow::Result<()> + Send + Sync + 'req,
352 ) -> &mut Self {
353 self.0.on_after_request_signed(callback);
354 self
355 }
356 #[inline]
357 #[doc = "设置响应成功回调函数"]
358 pub fn on_response(
359 &mut self,
360 callback: impl Fn(
361 &mut dyn qiniu_http_client::ExtendedCallbackContext,
362 &qiniu_http_client::http::ResponseParts,
363 ) -> anyhow::Result<()>
364 + Send
365 + Sync
366 + 'req,
367 ) -> &mut Self {
368 self.0.on_response(callback);
369 self
370 }
371 #[inline]
372 #[doc = "设置响应错误回调函数"]
373 pub fn on_error(
374 &mut self,
375 callback: impl Fn(
376 &mut dyn qiniu_http_client::ExtendedCallbackContext,
377 &mut qiniu_http_client::ResponseError,
378 ) -> anyhow::Result<()>
379 + Send
380 + Sync
381 + 'req,
382 ) -> &mut Self {
383 self.0.on_error(callback);
384 self
385 }
386 #[inline]
387 #[doc = "设置退避前回调函数"]
388 pub fn on_before_backoff(
389 &mut self,
390 callback: impl Fn(&mut dyn qiniu_http_client::ExtendedCallbackContext, std::time::Duration) -> anyhow::Result<()>
391 + Send
392 + Sync
393 + 'req,
394 ) -> &mut Self {
395 self.0.on_before_backoff(callback);
396 self
397 }
398 #[inline]
399 #[doc = "设置退避后回调函数"]
400 pub fn on_after_backoff(
401 &mut self,
402 callback: impl Fn(&mut dyn qiniu_http_client::ExtendedCallbackContext, std::time::Duration) -> anyhow::Result<()>
403 + Send
404 + Sync
405 + 'req,
406 ) -> &mut Self {
407 self.0.on_after_backoff(callback);
408 self
409 }
410 #[inline]
411 #[doc = "获取 HTTP 请求构建器部分参数"]
412 pub fn parts(&self) -> &qiniu_http_client::RequestBuilderParts<'req> {
413 self.0.parts()
414 }
415 #[inline]
416 #[doc = "获取 HTTP 请求构建器部分参数的可变引用"]
417 pub fn parts_mut(&mut self) -> &mut qiniu_http_client::RequestBuilderParts<'req> {
418 self.0.parts_mut()
419 }
420}
421impl<'req, E: qiniu_http_client::EndpointsProvider + Clone + 'req> SyncRequestBuilder<'req, E> {
422 #[doc = "阻塞发起 HTTP 请求"]
423 pub fn call(&mut self) -> qiniu_http_client::ApiResult<qiniu_http_client::Response<ResponseBody>> {
424 let request = &mut self.0;
425 let response = request.call()?;
426 let parsed = response.parse_json()?;
427 Ok(parsed)
428 }
429}
430#[cfg(feature = "async")]
431impl<'req, E: qiniu_http_client::EndpointsProvider + Clone + 'req> AsyncRequestBuilder<'req, E> {
432 #[doc = "异步发起 HTTP 请求"]
433 pub async fn call(&mut self) -> qiniu_http_client::ApiResult<qiniu_http_client::Response<ResponseBody>> {
434 let request = &mut self.0;
435 let response = request.call().await?;
436 let parsed = response.parse_json().await?;
437 Ok(parsed)
438 }
439}