Struct qiniu_http_client::HttpClientBuilder
source · pub struct HttpClientBuilder { /* private fields */ }
Expand description
HTTP 客户端构建器
Implementations§
source§impl HttpClientBuilder
impl HttpClientBuilder
sourcepub fn ureq() -> Self
pub fn ureq() -> Self
创建一个新的 HTTP 客户端构建器,使用 crate::ureq::Client
作为 HttpCaller
实现
sourcepub fn isahc() -> Result<Self, IsahcError>
pub fn isahc() -> Result<Self, IsahcError>
创建一个新的 HTTP 客户端构建器,使用 crate::isahc::Client
作为 HttpCaller
实现
sourcepub fn reqwest_sync() -> Self
pub fn reqwest_sync() -> Self
创建一个新的 HTTP 客户端构建器,使用 crate::reqwest::SyncClient
作为 HttpCaller
实现
sourcepub fn reqwest_async() -> Self
pub fn reqwest_async() -> Self
创建一个新的 HTTP 客户端构建器,使用 crate::reqwest::SyncClient
作为 HttpCaller
实现
sourcepub fn new(http_caller: impl HttpCaller + 'static) -> Self
pub fn new(http_caller: impl HttpCaller + 'static) -> Self
创建一个新的 HTTP 客户端构建器,需要指定 HttpCaller
实现
sourcepub fn use_https(&mut self, use_https: bool) -> &mut Self
pub fn use_https(&mut self, use_https: bool) -> &mut Self
设置是否使用 HTTPS
默认为使用 HTTPS
Examples found in repository?
More examples
sourcepub fn appended_user_agent(
&mut self,
appended_user_agent: impl Into<UserAgent>
) -> &mut Self
pub fn appended_user_agent(
&mut self,
appended_user_agent: impl Into<UserAgent>
) -> &mut Self
设置追加的 UserAgent
sourcepub fn http_caller(
&mut self,
http_caller: impl HttpCaller + 'static
) -> &mut Self
pub fn http_caller(
&mut self,
http_caller: impl HttpCaller + 'static
) -> &mut Self
设置 HTTP 客户端实现
默认根据启用的功能自动选择。
sourcepub fn request_retrier(
&mut self,
request_retrier: impl RequestRetrier + 'static
) -> &mut Self
pub fn request_retrier(
&mut self,
request_retrier: impl RequestRetrier + 'static
) -> &mut Self
设置重试器
默认使用 ErrorRetrier
,并使用 LimitedRetrier
对其进行包装。
sourcepub fn backoff(&mut self, backoff: impl Backoff + 'static) -> &mut Self
pub fn backoff(&mut self, backoff: impl Backoff + 'static) -> &mut Self
设置退避器
默认使用 ExponentialBackoff
,并使用 LimitedBackoff
和 RandomizedBackoff
对其进行包装。
sourcepub fn chooser(&mut self, chooser: impl Chooser + 'static) -> &mut Self
pub fn chooser(&mut self, chooser: impl Chooser + 'static) -> &mut Self
设置选择器
默认使用 SubnetChooser
,并使用 ShuffledChooser
和 NeverEmptyHandedChooser
对其进行包装。
sourcepub fn resolver(&mut self, resolver: impl Resolver + 'static) -> &mut Self
pub fn resolver(&mut self, resolver: impl Resolver + 'static) -> &mut Self
设置域名解析器
默认通过当前启用的功能来判定,并使用 CachedResolver
和 ShuffledResolver
对其进行包装。
sourcepub fn on_uploading_progress(
&mut self,
callback: impl Fn(&dyn SimplifiedCallbackContext, TransferProgressInfo<'_>) -> AnyResult<()> + Send + Sync + 'static
) -> &mut Self
pub fn on_uploading_progress(
&mut self,
callback: impl Fn(&dyn SimplifiedCallbackContext, TransferProgressInfo<'_>) -> AnyResult<()> + Send + Sync + 'static
) -> &mut Self
设置上传进度回调函数
sourcepub fn on_receive_response_status(
&mut self,
callback: impl Fn(&dyn SimplifiedCallbackContext, StatusCode) -> AnyResult<()> + Send + Sync + 'static
) -> &mut Self
pub fn on_receive_response_status(
&mut self,
callback: impl Fn(&dyn SimplifiedCallbackContext, StatusCode) -> AnyResult<()> + Send + Sync + 'static
) -> &mut Self
设置响应状态码回调函数
sourcepub fn on_receive_response_header(
&mut self,
callback: impl Fn(&dyn SimplifiedCallbackContext, &HeaderName, &HeaderValue) -> AnyResult<()> + Send + Sync + 'static
) -> &mut Self
pub fn on_receive_response_header(
&mut self,
callback: impl Fn(&dyn SimplifiedCallbackContext, &HeaderName, &HeaderValue) -> AnyResult<()> + Send + Sync + 'static
) -> &mut Self
设置响应 HTTP 头回调函数
sourcepub fn on_to_resolve_domain(
&mut self,
callback: impl Fn(&mut dyn CallbackContext, &str) -> AnyResult<()> + Send + Sync + 'static
) -> &mut Self
pub fn on_to_resolve_domain(
&mut self,
callback: impl Fn(&mut dyn CallbackContext, &str) -> AnyResult<()> + Send + Sync + 'static
) -> &mut Self
设置域名解析前回调函数
sourcepub fn on_domain_resolved(
&mut self,
callback: impl Fn(&mut dyn CallbackContext, &str, &ResolveAnswers) -> AnyResult<()> + Send + Sync + 'static
) -> &mut Self
pub fn on_domain_resolved(
&mut self,
callback: impl Fn(&mut dyn CallbackContext, &str, &ResolveAnswers) -> AnyResult<()> + Send + Sync + 'static
) -> &mut Self
设置域名解析成功回调函数
sourcepub fn on_to_choose_ips(
&mut self,
callback: impl Fn(&mut dyn CallbackContext, &[IpAddrWithPort]) -> AnyResult<()> + Send + Sync + 'static
) -> &mut Self
pub fn on_to_choose_ips(
&mut self,
callback: impl Fn(&mut dyn CallbackContext, &[IpAddrWithPort]) -> AnyResult<()> + Send + Sync + 'static
) -> &mut Self
设置 IP 地址选择前回调函数
sourcepub fn on_ips_chosen(
&mut self,
callback: impl Fn(&mut dyn CallbackContext, &[IpAddrWithPort], &[IpAddrWithPort]) -> AnyResult<()> + Send + Sync + 'static
) -> &mut Self
pub fn on_ips_chosen(
&mut self,
callback: impl Fn(&mut dyn CallbackContext, &[IpAddrWithPort], &[IpAddrWithPort]) -> AnyResult<()> + Send + Sync + 'static
) -> &mut Self
设置 IP 地址选择成功回调函数
sourcepub fn on_before_request_signed(
&mut self,
callback: impl Fn(&mut dyn ExtendedCallbackContext) -> AnyResult<()> + Send + Sync + 'static
) -> &mut Self
pub fn on_before_request_signed(
&mut self,
callback: impl Fn(&mut dyn ExtendedCallbackContext) -> AnyResult<()> + Send + Sync + 'static
) -> &mut Self
设置 HTTP 请求签名前回调函数
sourcepub fn on_after_request_signed(
&mut self,
callback: impl Fn(&mut dyn ExtendedCallbackContext) -> AnyResult<()> + Send + Sync + 'static
) -> &mut Self
pub fn on_after_request_signed(
&mut self,
callback: impl Fn(&mut dyn ExtendedCallbackContext) -> AnyResult<()> + Send + Sync + 'static
) -> &mut Self
设置 HTTP 请求前回调函数
sourcepub fn on_response(
&mut self,
callback: impl Fn(&mut dyn ExtendedCallbackContext, &ResponseParts) -> AnyResult<()> + Send + Sync + 'static
) -> &mut Self
pub fn on_response(
&mut self,
callback: impl Fn(&mut dyn ExtendedCallbackContext, &ResponseParts) -> AnyResult<()> + Send + Sync + 'static
) -> &mut Self
设置 HTTP 响应成功回调函数
sourcepub fn on_error(
&mut self,
callback: impl Fn(&mut dyn ExtendedCallbackContext, &mut ResponseError) -> AnyResult<()> + Send + Sync + 'static
) -> &mut Self
pub fn on_error(
&mut self,
callback: impl Fn(&mut dyn ExtendedCallbackContext, &mut ResponseError) -> AnyResult<()> + Send + Sync + 'static
) -> &mut Self
设置 HTTP 响应出错回调函数
sourcepub fn on_before_backoff(
&mut self,
callback: impl Fn(&mut dyn ExtendedCallbackContext, Duration) -> AnyResult<()> + Send + Sync + 'static
) -> &mut Self
pub fn on_before_backoff(
&mut self,
callback: impl Fn(&mut dyn ExtendedCallbackContext, Duration) -> AnyResult<()> + Send + Sync + 'static
) -> &mut Self
设置退避前回调函数
sourcepub fn on_after_backoff(
&mut self,
callback: impl Fn(&mut dyn ExtendedCallbackContext, Duration) -> AnyResult<()> + Send + Sync + 'static
) -> &mut Self
pub fn on_after_backoff(
&mut self,
callback: impl Fn(&mut dyn ExtendedCallbackContext, Duration) -> AnyResult<()> + Send + Sync + 'static
) -> &mut Self
设置退避后回调函数
sourcepub fn build(&mut self) -> HttpClient
pub fn build(&mut self) -> HttpClient
构建 HTTP 客户端
Examples found in repository?
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637
pub fn ureq() -> Self {
Self::build_ureq().build()
}
/// 创建一个新的 HTTP 客户端,使用 [`crate::isahc::Client`] 作为 [`HttpCaller`] 实现
#[inline]
#[cfg(feature = "isahc")]
#[cfg_attr(feature = "docs", doc(cfg(feature = "isahc")))]
pub fn isahc() -> Result<Self, IsahcError> {
Ok(Self::build_isahc()?.build())
}
/// 创建一个新的 HTTP 客户端,使用 [`crate::reqwest::SyncClient`] 作为 [`HttpCaller`] 实现
#[inline]
#[cfg(feature = "reqwest")]
#[cfg_attr(feature = "docs", doc(cfg(feature = "reqwest")))]
pub fn reqwest_sync() -> Self {
Self::build_reqwest_sync().build()
}
/// 创建一个新的 HTTP 客户端,使用 [`crate::reqwest::AsyncClient`] 作为 [`HttpCaller`] 实现
#[inline]
#[cfg(all(feature = "reqwest", feature = "async"))]
#[cfg_attr(feature = "docs", doc(cfg(all(feature = "reqwest", feature = "async"))))]
pub fn reqwest_async() -> Self {
Self::build_reqwest_async().build()
}
/// 创建一个新的 HTTP 客户端,根据当前环境变量选择 [`HttpCaller`] 实现
#[inline]
pub fn build_default() -> HttpClientBuilder {
HttpClientBuilder::default()
}
/// 创建一个新的 HTTP 客户端构建器,使用 [`crate::ureq::Client`] 作为 [`HttpCaller`] 实现
#[inline]
#[cfg(feature = "ureq")]
#[cfg_attr(feature = "docs", doc(cfg(feature = "ureq")))]
pub fn build_ureq() -> HttpClientBuilder {
HttpClientBuilder::ureq()
}
/// 创建一个新的 HTTP 客户端构建器,使用 [`crate::isahc::Client`] 作为 [`HttpCaller`] 实现
#[inline]
#[cfg(feature = "isahc")]
#[cfg_attr(feature = "docs", doc(cfg(feature = "isahc")))]
pub fn build_isahc() -> Result<HttpClientBuilder, IsahcError> {
HttpClientBuilder::isahc()
}
/// 创建一个新的 HTTP 客户端构建器,使用 [`crate::reqwest::SyncClient`] 作为 [`HttpCaller`] 实现
#[inline]
#[cfg(feature = "reqwest")]
#[cfg_attr(feature = "docs", doc(cfg(feature = "reqwest")))]
pub fn build_reqwest_sync() -> HttpClientBuilder {
HttpClientBuilder::reqwest_sync()
}
/// 创建一个新的 HTTP 客户端构建器,使用 [`crate::reqwest::AsyncClient`] 作为 [`HttpCaller`] 实现
#[inline]
#[cfg(all(feature = "reqwest", feature = "async"))]
#[cfg_attr(feature = "docs", doc(cfg(all(feature = "reqwest", feature = "async"))))]
pub fn build_reqwest_async() -> HttpClientBuilder {
HttpClientBuilder::reqwest_async()
}
/// 创建一个新的 HTTP 客户端,需要指定 [`HttpCaller`] 实现
#[inline]
pub fn new(http_caller: impl HttpCaller + 'static) -> Self {
HttpClientBuilder::new(http_caller).build()
}
/// 创建一个新的 HTTP 客户端构建器,需要指定 [`HttpCaller`] 实现
#[inline]
pub fn builder(http_caller: impl HttpCaller + 'static) -> HttpClientBuilder {
HttpClientBuilder::new(http_caller)
}
/// 创建 GET 请求的请求构建器
///
/// 该方法的异步版本为 [`HttpClient::async_get`]。
#[inline]
pub fn get<'r, E: EndpointsProvider + 'r>(
&'r self,
service_names: &'r [ServiceName],
endpoints_provider: E,
) -> SyncRequestBuilder<'r, E> {
self.new_request(Method::GET, service_names, endpoints_provider)
}
/// 创建 POST 请求的请求构建器
///
/// 该方法的异步版本为 [`HttpClient::async_post`]。
#[inline]
pub fn post<'r, E: EndpointsProvider + 'r>(
&'r self,
service_names: &'r [ServiceName],
endpoints_provider: E,
) -> SyncRequestBuilder<'r, E> {
self.new_request(Method::POST, service_names, endpoints_provider)
}
/// 创建 PUT 请求的请求构建器
///
/// 该方法的异步版本为 [`HttpClient::async_put`]。
#[inline]
pub fn put<'r, E: EndpointsProvider + 'r>(
&'r self,
service_names: &'r [ServiceName],
endpoints_provider: E,
) -> SyncRequestBuilder<'r, E> {
self.new_request(Method::PUT, service_names, endpoints_provider)
}
/// 创建 DELETE 请求的请求构建器
///
/// 该方法的异步版本为 [`HttpClient::async_delete`]。
#[inline]
pub fn delete<'r, E: EndpointsProvider + 'r>(
&'r self,
service_names: &'r [ServiceName],
endpoints_provider: E,
) -> SyncRequestBuilder<'r, E> {
self.new_request(Method::DELETE, service_names, endpoints_provider)
}
/// 创建请求的请求构建器
///
/// 该方法的异步版本为 [`HttpClient::new_async_request`]。
#[inline]
pub fn new_request<'r, E: EndpointsProvider + 'r>(
&'r self,
method: Method,
service_names: &'r [ServiceName],
endpoints_provider: E,
) -> SyncRequestBuilder<'r, E> {
SyncRequestBuilder::new(self, method, endpoints_provider, service_names)
}
/// 创建 GET 请求的异步请求构建器
#[inline]
#[cfg(feature = "async")]
#[cfg_attr(feature = "docs", doc(cfg(feature = "async")))]
pub fn async_get<'r, E: EndpointsProvider + 'r>(
&'r self,
service_names: &'r [ServiceName],
endpoints_provider: E,
) -> AsyncRequestBuilder<'r, E> {
self.new_async_request(Method::GET, service_names, endpoints_provider)
}
/// 创建 POST 请求的异步请求构建器
#[inline]
#[cfg(feature = "async")]
#[cfg_attr(feature = "docs", doc(cfg(feature = "async")))]
pub fn async_post<'r, E: EndpointsProvider + 'r>(
&'r self,
service_names: &'r [ServiceName],
endpoints_provider: E,
) -> AsyncRequestBuilder<'r, E> {
self.new_async_request(Method::POST, service_names, endpoints_provider)
}
/// 创建 PUT 请求的异步请求构建器
#[inline]
#[cfg(feature = "async")]
#[cfg_attr(feature = "docs", doc(cfg(feature = "async")))]
pub fn async_put<'r, E: EndpointsProvider + 'r>(
&'r self,
service_names: &'r [ServiceName],
endpoints_provider: E,
) -> AsyncRequestBuilder<'r, E> {
self.new_async_request(Method::PUT, service_names, endpoints_provider)
}
/// 创建 DELETE 请求的异步请求构建器
#[inline]
#[cfg(feature = "async")]
#[cfg_attr(feature = "docs", doc(cfg(feature = "async")))]
pub fn async_delete<'r, E: EndpointsProvider + 'r>(
&'r self,
service_names: &'r [ServiceName],
endpoints_provider: E,
) -> AsyncRequestBuilder<'r, E> {
self.new_async_request(Method::DELETE, service_names, endpoints_provider)
}
/// 创建异步请求的请求构建器
#[cfg(feature = "async")]
pub fn new_async_request<'r, E: EndpointsProvider + 'r>(
&'r self,
method: Method,
service_names: &'r [ServiceName],
endpoints_provider: E,
) -> AsyncRequestBuilder<'r, E> {
AsyncRequestBuilder::new(self, method, endpoints_provider, service_names)
}
pub(super) fn use_https(&self) -> bool {
self.inner.use_https
}
pub(super) fn appended_user_agent(&self) -> &UserAgent {
&self.inner.appended_user_agent
}
pub(super) fn callbacks(&self) -> &Callbacks<'static> {
&self.inner.callbacks
}
pub(super) fn http_caller(&self) -> &dyn HttpCaller {
self.inner.http_caller.as_ref()
}
pub(super) fn request_retrier(&self) -> &dyn RequestRetrier {
self.inner.request_retrier.as_ref()
}
pub(super) fn backoff(&self) -> &dyn Backoff {
self.inner.backoff.as_ref()
}
pub(super) fn chooser(&self) -> &dyn Chooser {
self.inner.chooser.as_ref()
}
pub(super) fn resolver(&self) -> &dyn Resolver {
self.inner.resolver.as_ref()
}
}
/// HTTP 客户端构建器
#[derive(Debug)]
pub struct HttpClientBuilder {
use_https: bool,
appended_user_agent: UserAgent,
http_caller: Option<Box<dyn HttpCaller>>,
request_retrier: Option<Box<dyn RequestRetrier>>,
backoff: Option<Box<dyn Backoff>>,
chooser: Option<Box<dyn Chooser>>,
resolver: Option<Box<dyn Resolver>>,
callbacks: CallbacksBuilder<'static>,
}
impl HttpClientBuilder {
/// 创建一个新的 HTTP 客户端构建器,使用 [`crate::ureq::Client`] 作为 [`HttpCaller`] 实现
#[inline]
#[cfg(feature = "ureq")]
pub fn ureq() -> Self {
Self::_new(Some(Box::<qiniu_ureq::Client>::default()))
}
/// 创建一个新的 HTTP 客户端构建器,使用 [`crate::isahc::Client`] 作为 [`HttpCaller`] 实现
#[inline]
#[cfg(feature = "isahc")]
pub fn isahc() -> Result<Self, IsahcError> {
Ok(Self::_new(Some(Box::new(qiniu_isahc::Client::default_client()?))))
}
/// 创建一个新的 HTTP 客户端构建器,使用 [`crate::reqwest::SyncClient`] 作为 [`HttpCaller`] 实现
#[inline]
#[cfg(feature = "reqwest")]
pub fn reqwest_sync() -> Self {
Self::_new(Some(Box::<qiniu_reqwest::SyncClient>::default()))
}
/// 创建一个新的 HTTP 客户端构建器,使用 [`crate::reqwest::SyncClient`] 作为 [`HttpCaller`] 实现
#[inline]
#[cfg(all(feature = "reqwest", feature = "async"))]
pub fn reqwest_async() -> Self {
Self::_new(Some(Box::<qiniu_reqwest::AsyncClient>::default()))
}
/// 创建一个新的 HTTP 客户端构建器,需要指定 [`HttpCaller`] 实现
#[inline]
pub fn new(http_caller: impl HttpCaller + 'static) -> Self {
Self::_new(Some(Box::new(http_caller)))
}
fn _new(http_caller: Option<Box<dyn HttpCaller>>) -> Self {
HttpClientBuilder {
http_caller,
use_https: true,
appended_user_agent: Default::default(),
request_retrier: Default::default(),
backoff: Default::default(),
chooser: Default::default(),
resolver: Default::default(),
callbacks: Default::default(),
}
}
/// 设置是否使用 HTTPS
///
/// 默认为使用 HTTPS
#[inline]
pub fn use_https(&mut self, use_https: bool) -> &mut Self {
self.use_https = use_https;
self
}
/// 设置追加的 UserAgent
#[inline]
pub fn appended_user_agent(&mut self, appended_user_agent: impl Into<UserAgent>) -> &mut Self {
self.appended_user_agent = appended_user_agent.into();
self
}
/// 设置 HTTP 客户端实现
///
/// 默认根据启用的功能自动选择。
#[inline]
pub fn http_caller(&mut self, http_caller: impl HttpCaller + 'static) -> &mut Self {
self.http_caller = Some(Box::new(http_caller));
self
}
/// 设置重试器
///
/// 默认使用 [`ErrorRetrier`],并使用 [`LimitedRetrier`] 对其进行包装。
#[inline]
pub fn request_retrier(&mut self, request_retrier: impl RequestRetrier + 'static) -> &mut Self {
self.request_retrier = Some(Box::new(request_retrier));
self
}
/// 设置退避器
///
/// 默认使用 [`ExponentialBackoff`],并使用 [`LimitedBackoff`] 和 [`RandomizedBackoff`] 对其进行包装。
#[inline]
pub fn backoff(&mut self, backoff: impl Backoff + 'static) -> &mut Self {
self.backoff = Some(Box::new(backoff));
self
}
/// 设置选择器
///
/// 默认使用 [`SubnetChooser`],并使用 [`ShuffledChooser`] 和 [`NeverEmptyHandedChooser`] 对其进行包装。
#[inline]
pub fn chooser(&mut self, chooser: impl Chooser + 'static) -> &mut Self {
self.chooser = Some(Box::new(chooser));
self
}
/// 设置域名解析器
///
/// 默认通过当前启用的功能来判定,并使用 [`CachedResolver`] 和 [`ShuffledResolver`] 对其进行包装。
#[inline]
pub fn resolver(&mut self, resolver: impl Resolver + 'static) -> &mut Self {
self.resolver = Some(Box::new(resolver));
self
}
/// 设置上传进度回调函数
#[inline]
pub fn on_uploading_progress(
&mut self,
callback: impl Fn(&dyn SimplifiedCallbackContext, TransferProgressInfo<'_>) -> AnyResult<()> + Send + Sync + 'static,
) -> &mut Self {
self.callbacks.on_uploading_progress(callback);
self
}
/// 设置响应状态码回调函数
#[inline]
pub fn on_receive_response_status(
&mut self,
callback: impl Fn(&dyn SimplifiedCallbackContext, StatusCode) -> AnyResult<()> + Send + Sync + 'static,
) -> &mut Self {
self.callbacks.on_receive_response_status(callback);
self
}
/// 设置响应 HTTP 头回调函数
#[inline]
pub fn on_receive_response_header(
&mut self,
callback: impl Fn(&dyn SimplifiedCallbackContext, &HeaderName, &HeaderValue) -> AnyResult<()>
+ Send
+ Sync
+ 'static,
) -> &mut Self {
self.callbacks.on_receive_response_header(callback);
self
}
/// 设置域名解析前回调函数
#[inline]
pub fn on_to_resolve_domain(
&mut self,
callback: impl Fn(&mut dyn CallbackContext, &str) -> AnyResult<()> + Send + Sync + 'static,
) -> &mut Self {
self.callbacks.on_to_resolve_domain(callback);
self
}
/// 设置域名解析成功回调函数
#[inline]
pub fn on_domain_resolved(
&mut self,
callback: impl Fn(&mut dyn CallbackContext, &str, &ResolveAnswers) -> AnyResult<()> + Send + Sync + 'static,
) -> &mut Self {
self.callbacks.on_domain_resolved(callback);
self
}
/// 设置 IP 地址选择前回调函数
#[inline]
pub fn on_to_choose_ips(
&mut self,
callback: impl Fn(&mut dyn CallbackContext, &[IpAddrWithPort]) -> AnyResult<()> + Send + Sync + 'static,
) -> &mut Self {
self.callbacks.on_to_choose_ips(callback);
self
}
/// 设置 IP 地址选择成功回调函数
#[inline]
pub fn on_ips_chosen(
&mut self,
callback: impl Fn(&mut dyn CallbackContext, &[IpAddrWithPort], &[IpAddrWithPort]) -> AnyResult<()>
+ Send
+ Sync
+ 'static,
) -> &mut Self {
self.callbacks.on_ips_chosen(callback);
self
}
/// 设置 HTTP 请求签名前回调函数
#[inline]
pub fn on_before_request_signed(
&mut self,
callback: impl Fn(&mut dyn ExtendedCallbackContext) -> AnyResult<()> + Send + Sync + 'static,
) -> &mut Self {
self.callbacks.on_before_request_signed(callback);
self
}
/// 设置 HTTP 请求前回调函数
#[inline]
pub fn on_after_request_signed(
&mut self,
callback: impl Fn(&mut dyn ExtendedCallbackContext) -> AnyResult<()> + Send + Sync + 'static,
) -> &mut Self {
self.callbacks.on_after_request_signed(callback);
self
}
/// 设置 HTTP 响应成功回调函数
#[inline]
pub fn on_response(
&mut self,
callback: impl Fn(&mut dyn ExtendedCallbackContext, &ResponseParts) -> AnyResult<()> + Send + Sync + 'static,
) -> &mut Self {
self.callbacks.on_response(callback);
self
}
/// 设置 HTTP 响应出错回调函数
#[inline]
pub fn on_error(
&mut self,
callback: impl Fn(&mut dyn ExtendedCallbackContext, &mut ResponseError) -> AnyResult<()> + Send + Sync + 'static,
) -> &mut Self {
self.callbacks.on_error(callback);
self
}
/// 设置退避前回调函数
#[inline]
pub fn on_before_backoff(
&mut self,
callback: impl Fn(&mut dyn ExtendedCallbackContext, Duration) -> AnyResult<()> + Send + Sync + 'static,
) -> &mut Self {
self.callbacks.on_before_backoff(callback);
self
}
/// 设置退避后回调函数
#[inline]
pub fn on_after_backoff(
&mut self,
callback: impl Fn(&mut dyn ExtendedCallbackContext, Duration) -> AnyResult<()> + Send + Sync + 'static,
) -> &mut Self {
self.callbacks.on_after_backoff(callback);
self
}
/// 构建 HTTP 客户端
pub fn build(&mut self) -> HttpClient {
HttpClient {
inner: Arc::new(HttpClientInner {
use_https: replace(&mut self.use_https, true),
appended_user_agent: take(&mut self.appended_user_agent),
http_caller: take(&mut self.http_caller).unwrap_or_else(HttpClient::default_http_caller),
request_retrier: take(&mut self.request_retrier).unwrap_or_else(HttpClient::default_retrier),
backoff: take(&mut self.backoff).unwrap_or_else(HttpClient::default_backoff),
chooser: take(&mut self.chooser).unwrap_or_else(HttpClient::default_chooser),
resolver: take(&mut self.resolver).unwrap_or_else(HttpClient::default_resolver),
callbacks: self.callbacks.build(),
}),
}
}
#[allow(dead_code)]
fn assert() {
assert_impl!(Send: Self);
assert_impl!(Sync: Self);
}
}
impl Default for HttpClientBuilder {
#[inline]
fn default() -> Self {
HttpClientBuilder::_new(None)
}
}
impl Default for HttpClient {
#[inline]
fn default() -> Self {
HttpClientBuilder::default().build()
}
More examples
Trait Implementations§
source§impl Debug for HttpClientBuilder
impl Debug for HttpClientBuilder
Auto Trait Implementations§
impl !RefUnwindSafe for HttpClientBuilder
impl Send for HttpClientBuilder
impl Sync for HttpClientBuilder
impl Unpin for HttpClientBuilder
impl !UnwindSafe for HttpClientBuilder
Blanket Implementations§
source§impl<T> Instrument for T
impl<T> Instrument for T
source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
source§impl<T> Instrument for T
impl<T> Instrument for T
source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
source§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere
T: ?Sized,
source§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
source§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
self
and passes that borrow into the pipe function. Read moresource§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
self
and passes that borrow into the pipe function. Read moresource§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> Rwhere
Self: Borrow<B>,
B: 'a + ?Sized,
R: 'a,
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> Rwhere
Self: Borrow<B>,
B: 'a + ?Sized,
R: 'a,
source§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R
) -> Rwhere
Self: BorrowMut<B>,
B: 'a + ?Sized,
R: 'a,
fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R
) -> Rwhere
Self: BorrowMut<B>,
B: 'a + ?Sized,
R: 'a,
source§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> Rwhere
Self: AsRef<U>,
U: 'a + ?Sized,
R: 'a,
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> Rwhere
Self: AsRef<U>,
U: 'a + ?Sized,
R: 'a,
self
, then passes self.as_ref()
into the pipe function.source§fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> Rwhere
Self: AsMut<U>,
U: 'a + ?Sized,
R: 'a,
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> Rwhere
Self: AsMut<U>,
U: 'a + ?Sized,
R: 'a,
self
, then passes self.as_mut()
into the pipe
function.source§impl<T> Tap for T
impl<T> Tap for T
source§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Selfwhere
Self: Borrow<B>,
B: ?Sized,
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Selfwhere
Self: Borrow<B>,
B: ?Sized,
Borrow<B>
of a value. Read moresource§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Selfwhere
Self: BorrowMut<B>,
B: ?Sized,
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Selfwhere
Self: BorrowMut<B>,
B: ?Sized,
BorrowMut<B>
of a value. Read moresource§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Selfwhere
Self: AsRef<R>,
R: ?Sized,
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Selfwhere
Self: AsRef<R>,
R: ?Sized,
AsRef<R>
view of a value. Read moresource§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Selfwhere
Self: AsMut<R>,
R: ?Sized,
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Selfwhere
Self: AsMut<R>,
R: ?Sized,
AsMut<R>
view of a value. Read moresource§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Selfwhere
Self: Deref<Target = T>,
T: ?Sized,
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Selfwhere
Self: Deref<Target = T>,
T: ?Sized,
Deref::Target
of a value. Read moresource§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Selfwhere
Self: DerefMut<Target = T> + Deref,
T: ?Sized,
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Selfwhere
Self: DerefMut<Target = T> + Deref,
T: ?Sized,
Deref::Target
of a value. Read moresource§fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
.tap()
only in debug builds, and is erased in release builds.source§fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
.tap_mut()
only in debug builds, and is erased in release
builds.source§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Selfwhere
Self: Borrow<B>,
B: ?Sized,
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Selfwhere
Self: Borrow<B>,
B: ?Sized,
.tap_borrow()
only in debug builds, and is erased in release
builds.source§fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Selfwhere
Self: BorrowMut<B>,
B: ?Sized,
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Selfwhere
Self: BorrowMut<B>,
B: ?Sized,
.tap_borrow_mut()
only in debug builds, and is erased in release
builds.source§fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Selfwhere
Self: AsRef<R>,
R: ?Sized,
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Selfwhere
Self: AsRef<R>,
R: ?Sized,
.tap_ref()
only in debug builds, and is erased in release
builds.source§fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Selfwhere
Self: AsMut<R>,
R: ?Sized,
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Selfwhere
Self: AsMut<R>,
R: ?Sized,
.tap_ref_mut()
only in debug builds, and is erased in release
builds.