1#[derive(Debug, Clone, Default)]
4#[doc = "调用 API 所用的路径参数"]
5pub struct PathParams {
6 r#entry: Option<std::borrow::Cow<'static, str>>,
7 r#mime_type: Option<std::borrow::Cow<'static, str>>,
8 r#condition: 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#entry {
22 all_segments.push(segment);
23 }
24 if let Some(segment) = self.r#mime_type {
25 all_segments.push(std::borrow::Cow::Borrowed("mime"));
26 all_segments.push(segment);
27 }
28 if let Some(segment) = self.r#condition {
29 all_segments.push(std::borrow::Cow::Borrowed("cond"));
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 = "指定目标对象空间与目标对象名称"]
40 pub fn set_entry_as_str(mut self, value: impl Into<std::borrow::Cow<'static, str>>) -> Self {
41 self.r#entry = Some(qiniu_utils::base64::urlsafe(value.into().as_bytes()).into());
42 self
43 }
44 #[inline]
45 #[must_use]
46 #[doc = "新的 MIME 类型"]
47 pub fn set_mime_type_as_str(mut self, value: impl Into<std::borrow::Cow<'static, str>>) -> Self {
48 self.r#mime_type = Some(qiniu_utils::base64::urlsafe(value.into().as_bytes()).into());
49 self
50 }
51 #[inline]
52 #[must_use]
53 #[doc = "条件匹配,当前支持设置 hash、mime、fsize、putTime 条件,只有条件匹配才会执行修改操作,格式为 condKey1=condVal1&condKey2=condVal2"]
54 pub fn set_condition_as_str(mut self, value: impl Into<std::borrow::Cow<'static, str>>) -> Self {
55 self.r#condition = Some(qiniu_utils::base64::urlsafe(value.into().as_bytes()).into());
56 self
57 }
58 #[inline]
59 #[must_use]
60 #[doc = "对象存储元信息,键可以自定义,它可以由字母、数字、下划线、减号组成,必须以 x-qn-meta- 为前缀,且长度小于等于 50,单个文件键和值总和大小不能超过 1024 字节,可以同时修改多个键"]
61 pub fn append_meta_data_as_str(
62 mut self,
63 key: impl Into<std::borrow::Cow<'static, str>>,
64 value: impl Into<std::borrow::Cow<'static, str>>,
65 ) -> Self {
66 self.extended_segments.push(key.into());
67 self.extended_segments
68 .push(qiniu_utils::base64::urlsafe(value.into().as_bytes()).into());
69 self
70 }
71}
72#[derive(Clone, Debug, serde :: Serialize, serde :: Deserialize)]
73#[serde(transparent)]
74#[doc = "获取 API 所用的响应体参数"]
75pub struct ResponseBody(serde_json::Value);
76impl ResponseBody {
77 #[allow(dead_code)]
78 pub(crate) fn new(value: serde_json::Value) -> Self {
79 Self(value)
80 }
81}
82impl Default for ResponseBody {
83 #[inline]
84 fn default() -> Self {
85 Self(serde_json::Value::Object(Default::default()))
86 }
87}
88impl From<ResponseBody> for serde_json::Value {
89 #[inline]
90 fn from(val: ResponseBody) -> Self {
91 val.0
92 }
93}
94impl AsRef<serde_json::Value> for ResponseBody {
95 #[inline]
96 fn as_ref(&self) -> &serde_json::Value {
97 &self.0
98 }
99}
100impl AsMut<serde_json::Value> for ResponseBody {
101 #[inline]
102 fn as_mut(&mut self) -> &mut serde_json::Value {
103 &mut self.0
104 }
105}
106#[doc = "API 调用客户端"]
107#[derive(Debug, Clone)]
108pub struct Client<'client>(&'client qiniu_http_client::HttpClient);
109impl<'client> Client<'client> {
110 pub(super) fn new(http_client: &'client qiniu_http_client::HttpClient) -> Self {
111 Self(http_client)
112 }
113}
114impl<'client> Client<'client> {
115 #[inline]
116 #[doc = "创建一个新的阻塞请求,该方法的异步版本为 [`Self::new_async_request`]"]
117 pub fn new_request<E: qiniu_http_client::EndpointsProvider + 'client>(
118 &self,
119 endpoints_provider: E,
120 path_params: PathParams,
121 credential: impl qiniu_http_client::credential::CredentialProvider + Clone + 'client,
122 ) -> SyncRequestBuilder<'client, E> {
123 RequestBuilder({
124 let mut builder = self.0.post(&[qiniu_http_client::ServiceName::Rs], endpoints_provider);
125 builder.authorization(qiniu_http_client::Authorization::v2(credential));
126 builder.idempotent(qiniu_http_client::Idempotent::Always);
127 builder.path(crate::base_utils::join_path("/chgm", "", path_params.build()));
128 builder.accept_json();
129 builder
130 })
131 }
132 #[inline]
133 #[cfg(feature = "async")]
134 #[doc = "创建一个新的异步请求"]
135 pub fn new_async_request<E: qiniu_http_client::EndpointsProvider + 'client>(
136 &self,
137 endpoints_provider: E,
138 path_params: PathParams,
139 credential: impl qiniu_http_client::credential::CredentialProvider + Clone + 'client,
140 ) -> AsyncRequestBuilder<'client, E> {
141 RequestBuilder({
142 let mut builder = self
143 .0
144 .async_post(&[qiniu_http_client::ServiceName::Rs], endpoints_provider);
145 builder.authorization(qiniu_http_client::Authorization::v2(credential));
146 builder.idempotent(qiniu_http_client::Idempotent::Always);
147 builder.path(crate::base_utils::join_path("/chgm", "", path_params.build()));
148 builder.accept_json();
149 builder
150 })
151 }
152}
153#[derive(Debug)]
154#[doc = "API 请求构造器"]
155pub struct RequestBuilder<'req, B, E>(qiniu_http_client::RequestBuilder<'req, B, E>);
156#[doc = "API 阻塞请求构造器"]
157pub type SyncRequestBuilder<'req, E> = RequestBuilder<'req, qiniu_http_client::SyncRequestBody<'req>, E>;
158#[cfg(feature = "async")]
159#[cfg_attr(feature = "docs", doc(cfg(feature = "async")))]
160#[doc = "API 异步请求构造器"]
161pub type AsyncRequestBuilder<'req, E> = RequestBuilder<'req, qiniu_http_client::AsyncRequestBody<'req>, E>;
162impl<'req, B, E> RequestBuilder<'req, B, E> {
163 #[inline]
164 #[doc = "设置是否使用 HTTPS"]
165 pub fn use_https(&mut self, use_https: bool) -> &mut Self {
166 self.0.use_https(use_https);
167 self
168 }
169 #[inline]
170 #[doc = "设置 HTTP 协议版本"]
171 pub fn version(&mut self, version: qiniu_http_client::http::Version) -> &mut Self {
172 self.0.version(version);
173 self
174 }
175 #[inline]
176 #[doc = "设置 HTTP 请求头"]
177 pub fn headers(
178 &mut self,
179 headers: impl Into<std::borrow::Cow<'req, qiniu_http_client::http::HeaderMap>>,
180 ) -> &mut Self {
181 self.0.headers(headers);
182 self
183 }
184 #[inline]
185 #[doc = "添加 HTTP 请求头"]
186 pub fn set_header(
187 &mut self,
188 header_name: impl qiniu_http_client::http::header::IntoHeaderName,
189 header_value: impl Into<qiniu_http_client::http::HeaderValue>,
190 ) -> &mut Self {
191 self.0.set_header(header_name, header_value);
192 self
193 }
194 #[inline]
195 #[doc = "设置查询参数"]
196 pub fn query(&mut self, query: impl Into<std::borrow::Cow<'req, str>>) -> &mut Self {
197 self.0.query(query);
198 self
199 }
200 #[inline]
201 #[doc = "设置查询参数"]
202 pub fn query_pairs(&mut self, query_pairs: impl Into<Vec<qiniu_http_client::QueryPair<'req>>>) -> &mut Self {
203 self.0.query_pairs(query_pairs);
204 self
205 }
206 #[inline]
207 #[doc = "追加查询参数"]
208 pub fn append_query_pair(
209 &mut self,
210 query_pair_key: impl Into<qiniu_http_client::QueryPairKey<'req>>,
211 query_pair_value: impl Into<qiniu_http_client::QueryPairValue<'req>>,
212 ) -> &mut Self {
213 self.0.append_query_pair(query_pair_key, query_pair_value);
214 self
215 }
216 #[inline]
217 #[doc = "设置扩展信息"]
218 pub fn extensions(&mut self, extensions: qiniu_http_client::http::Extensions) -> &mut Self {
219 self.0.extensions(extensions);
220 self
221 }
222 #[doc = "添加扩展信息"]
223 #[inline]
224 pub fn add_extension<T: Send + Sync + 'static>(&mut self, val: T) -> &mut Self {
225 self.0.add_extension(val);
226 self
227 }
228 #[inline]
229 #[doc = "上传进度回调函数"]
230 pub fn on_uploading_progress(
231 &mut self,
232 callback: impl Fn(
233 &dyn qiniu_http_client::SimplifiedCallbackContext,
234 qiniu_http_client::http::TransferProgressInfo,
235 ) -> anyhow::Result<()>
236 + Send
237 + Sync
238 + 'req,
239 ) -> &mut Self {
240 self.0.on_uploading_progress(callback);
241 self
242 }
243 #[inline]
244 #[doc = "设置响应状态码回调函数"]
245 pub fn on_receive_response_status(
246 &mut self,
247 callback: impl Fn(
248 &dyn qiniu_http_client::SimplifiedCallbackContext,
249 qiniu_http_client::http::StatusCode,
250 ) -> anyhow::Result<()>
251 + Send
252 + Sync
253 + 'req,
254 ) -> &mut Self {
255 self.0.on_receive_response_status(callback);
256 self
257 }
258 #[inline]
259 #[doc = "设置响应 HTTP 头回调函数"]
260 pub fn on_receive_response_header(
261 &mut self,
262 callback: impl Fn(
263 &dyn qiniu_http_client::SimplifiedCallbackContext,
264 &qiniu_http_client::http::HeaderName,
265 &qiniu_http_client::http::HeaderValue,
266 ) -> anyhow::Result<()>
267 + Send
268 + Sync
269 + 'req,
270 ) -> &mut Self {
271 self.0.on_receive_response_header(callback);
272 self
273 }
274 #[inline]
275 #[doc = "设置域名解析前回调函数"]
276 pub fn on_to_resolve_domain(
277 &mut self,
278 callback: impl Fn(&mut dyn qiniu_http_client::CallbackContext, &str) -> anyhow::Result<()> + Send + Sync + 'req,
279 ) -> &mut Self {
280 self.0.on_to_resolve_domain(callback);
281 self
282 }
283 #[inline]
284 #[doc = "设置域名解析成功回调函数"]
285 pub fn on_domain_resolved(
286 &mut self,
287 callback: impl Fn(
288 &mut dyn qiniu_http_client::CallbackContext,
289 &str,
290 &qiniu_http_client::ResolveAnswers,
291 ) -> anyhow::Result<()>
292 + Send
293 + Sync
294 + 'req,
295 ) -> &mut Self {
296 self.0.on_domain_resolved(callback);
297 self
298 }
299 #[inline]
300 #[doc = "设置 IP 地址选择前回调函数"]
301 pub fn on_to_choose_ips(
302 &mut self,
303 callback: impl Fn(&mut dyn qiniu_http_client::CallbackContext, &[qiniu_http_client::IpAddrWithPort]) -> anyhow::Result<()>
304 + Send
305 + Sync
306 + 'req,
307 ) -> &mut Self {
308 self.0.on_to_choose_ips(callback);
309 self
310 }
311 #[inline]
312 #[doc = "设置 IP 地址选择成功回调函数"]
313 pub fn on_ips_chosen(
314 &mut self,
315 callback: impl Fn(
316 &mut dyn qiniu_http_client::CallbackContext,
317 &[qiniu_http_client::IpAddrWithPort],
318 &[qiniu_http_client::IpAddrWithPort],
319 ) -> anyhow::Result<()>
320 + Send
321 + Sync
322 + 'req,
323 ) -> &mut Self {
324 self.0.on_ips_chosen(callback);
325 self
326 }
327 #[inline]
328 #[doc = "设置 HTTP 请求签名前回调函数"]
329 pub fn on_before_request_signed(
330 &mut self,
331 callback: impl Fn(&mut dyn qiniu_http_client::ExtendedCallbackContext) -> anyhow::Result<()> + Send + Sync + 'req,
332 ) -> &mut Self {
333 self.0.on_before_request_signed(callback);
334 self
335 }
336 #[inline]
337 #[doc = "设置 HTTP 请求前回调函数"]
338 pub fn on_after_request_signed(
339 &mut self,
340 callback: impl Fn(&mut dyn qiniu_http_client::ExtendedCallbackContext) -> anyhow::Result<()> + Send + Sync + 'req,
341 ) -> &mut Self {
342 self.0.on_after_request_signed(callback);
343 self
344 }
345 #[inline]
346 #[doc = "设置响应成功回调函数"]
347 pub fn on_response(
348 &mut self,
349 callback: impl Fn(
350 &mut dyn qiniu_http_client::ExtendedCallbackContext,
351 &qiniu_http_client::http::ResponseParts,
352 ) -> anyhow::Result<()>
353 + Send
354 + Sync
355 + 'req,
356 ) -> &mut Self {
357 self.0.on_response(callback);
358 self
359 }
360 #[inline]
361 #[doc = "设置响应错误回调函数"]
362 pub fn on_error(
363 &mut self,
364 callback: impl Fn(
365 &mut dyn qiniu_http_client::ExtendedCallbackContext,
366 &mut qiniu_http_client::ResponseError,
367 ) -> anyhow::Result<()>
368 + Send
369 + Sync
370 + 'req,
371 ) -> &mut Self {
372 self.0.on_error(callback);
373 self
374 }
375 #[inline]
376 #[doc = "设置退避前回调函数"]
377 pub fn on_before_backoff(
378 &mut self,
379 callback: impl Fn(&mut dyn qiniu_http_client::ExtendedCallbackContext, std::time::Duration) -> anyhow::Result<()>
380 + Send
381 + Sync
382 + 'req,
383 ) -> &mut Self {
384 self.0.on_before_backoff(callback);
385 self
386 }
387 #[inline]
388 #[doc = "设置退避后回调函数"]
389 pub fn on_after_backoff(
390 &mut self,
391 callback: impl Fn(&mut dyn qiniu_http_client::ExtendedCallbackContext, std::time::Duration) -> anyhow::Result<()>
392 + Send
393 + Sync
394 + 'req,
395 ) -> &mut Self {
396 self.0.on_after_backoff(callback);
397 self
398 }
399 #[inline]
400 #[doc = "获取 HTTP 请求构建器部分参数"]
401 pub fn parts(&self) -> &qiniu_http_client::RequestBuilderParts<'req> {
402 self.0.parts()
403 }
404 #[inline]
405 #[doc = "获取 HTTP 请求构建器部分参数的可变引用"]
406 pub fn parts_mut(&mut self) -> &mut qiniu_http_client::RequestBuilderParts<'req> {
407 self.0.parts_mut()
408 }
409}
410impl<'req, E: qiniu_http_client::EndpointsProvider + Clone + 'req> SyncRequestBuilder<'req, E> {
411 #[doc = "阻塞发起 HTTP 请求"]
412 pub fn call(&mut self) -> qiniu_http_client::ApiResult<qiniu_http_client::Response<ResponseBody>> {
413 let request = &mut self.0;
414 let response = request.call()?;
415 let parsed = response.parse_json()?;
416 Ok(parsed)
417 }
418}
419#[cfg(feature = "async")]
420impl<'req, E: qiniu_http_client::EndpointsProvider + Clone + 'req> AsyncRequestBuilder<'req, E> {
421 #[doc = "异步发起 HTTP 请求"]
422 pub async fn call(&mut self) -> qiniu_http_client::ApiResult<qiniu_http_client::Response<ResponseBody>> {
423 let request = &mut self.0;
424 let response = request.call().await?;
425 let parsed = response.parse_json().await?;
426 Ok(parsed)
427 }
428}