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