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