1#[derive(Debug, Clone, Default)]
4#[doc = "调用 API 所用的路径参数"]
5pub struct PathParams {
6 r#src_entry: Option<std::borrow::Cow<'static, str>>,
7 r#dest_entry: Option<std::borrow::Cow<'static, str>>,
8 r#is_force: 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#src_entry {
22 all_segments.push(segment);
23 }
24 if let Some(segment) = self.r#dest_entry {
25 all_segments.push(segment);
26 }
27 if let Some(segment) = self.r#is_force {
28 all_segments.push(std::borrow::Cow::Borrowed("force"));
29 all_segments.push(segment);
30 }
31 all_segments.extend(self.extended_segments);
32 all_segments
33 }
34}
35impl PathParams {
36 #[inline]
37 #[must_use]
38 #[doc = "指定源对象空间与源对象名称"]
39 pub fn set_src_entry_as_str(mut self, value: impl Into<std::borrow::Cow<'static, str>>) -> Self {
40 self.r#src_entry = Some(qiniu_utils::base64::urlsafe(value.into().as_bytes()).into());
41 self
42 }
43 #[inline]
44 #[must_use]
45 #[doc = "指定目标对象空间与目标对象名称"]
46 pub fn set_dest_entry_as_str(mut self, value: impl Into<std::borrow::Cow<'static, str>>) -> Self {
47 self.r#dest_entry = Some(qiniu_utils::base64::urlsafe(value.into().as_bytes()).into());
48 self
49 }
50 #[inline]
51 #[must_use]
52 #[doc = "如果目标对象名已被占用,则返回错误码 614,且不做任何覆盖操作;如果指定为 true,会强制覆盖目标对象"]
53 pub fn set_is_force_as_bool(mut self, value: bool) -> Self {
54 self.r#is_force = Some(value.to_string().into());
55 self
56 }
57}
58#[derive(Clone, Debug, serde :: Serialize, serde :: Deserialize)]
59#[serde(transparent)]
60#[doc = "获取 API 所用的响应体参数"]
61pub struct ResponseBody(serde_json::Value);
62impl ResponseBody {
63 #[allow(dead_code)]
64 pub(crate) fn new(value: serde_json::Value) -> Self {
65 Self(value)
66 }
67}
68impl Default for ResponseBody {
69 #[inline]
70 fn default() -> Self {
71 Self(serde_json::Value::Object(Default::default()))
72 }
73}
74impl From<ResponseBody> for serde_json::Value {
75 #[inline]
76 fn from(val: ResponseBody) -> Self {
77 val.0
78 }
79}
80impl AsRef<serde_json::Value> for ResponseBody {
81 #[inline]
82 fn as_ref(&self) -> &serde_json::Value {
83 &self.0
84 }
85}
86impl AsMut<serde_json::Value> for ResponseBody {
87 #[inline]
88 fn as_mut(&mut self) -> &mut serde_json::Value {
89 &mut self.0
90 }
91}
92#[doc = "API 调用客户端"]
93#[derive(Debug, Clone)]
94pub struct Client<'client>(&'client qiniu_http_client::HttpClient);
95impl<'client> Client<'client> {
96 pub(super) fn new(http_client: &'client qiniu_http_client::HttpClient) -> Self {
97 Self(http_client)
98 }
99}
100impl<'client> Client<'client> {
101 #[inline]
102 #[doc = "创建一个新的阻塞请求,该方法的异步版本为 [`Self::new_async_request`]"]
103 pub fn new_request<E: qiniu_http_client::EndpointsProvider + 'client>(
104 &self,
105 endpoints_provider: E,
106 path_params: PathParams,
107 credential: impl qiniu_http_client::credential::CredentialProvider + Clone + 'client,
108 ) -> SyncRequestBuilder<'client, E> {
109 RequestBuilder({
110 let mut builder = self.0.post(&[qiniu_http_client::ServiceName::Rs], endpoints_provider);
111 builder.authorization(qiniu_http_client::Authorization::v2(credential));
112 builder.idempotent(qiniu_http_client::Idempotent::Default);
113 builder.path(crate::base_utils::join_path("/move", "", path_params.build()));
114 builder.accept_json();
115 builder
116 })
117 }
118 #[inline]
119 #[cfg(feature = "async")]
120 #[doc = "创建一个新的异步请求"]
121 pub fn new_async_request<E: qiniu_http_client::EndpointsProvider + 'client>(
122 &self,
123 endpoints_provider: E,
124 path_params: PathParams,
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_post(&[qiniu_http_client::ServiceName::Rs], endpoints_provider);
131 builder.authorization(qiniu_http_client::Authorization::v2(credential));
132 builder.idempotent(qiniu_http_client::Idempotent::Default);
133 builder.path(crate::base_utils::join_path("/move", "", path_params.build()));
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}