Struct qiniu_http_client::Response
source · pub struct Response<B>(_);
Expand description
HTTP 响应
Implementations§
source§impl<B> Response<B>
impl<B> Response<B>
sourcepub fn into_body(self) -> B
pub fn into_body(self) -> B
转换为 HTTP 响应体
Examples found in repository?
src/regions/endpoints_provider/bucket_domains_provider.rs (line 283)
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
fn do_sync_query(&self) -> ApiResult<Endpoints> {
let endpoints: Endpoints = self
.queryer
.http_client
.get(&[ServiceName::Uc], &self.queryer.uc_endpoints)
.path("/v2/domains")
.authorization(Authorization::v2(&self.credential))
.append_query_pair("tbl", self.bucket_name.as_str())
.accept_json()
.call()?
.parse_json::<Vec<String>>()?
.into_body()
.into_iter()
.map(Endpoint::from)
.collect();
Ok(endpoints)
}
#[cfg(feature = "async")]
async fn do_async_query(&self) -> ApiResult<Endpoints> {
let endpoints: Endpoints = self
.queryer
.http_client
.async_get(&[ServiceName::Uc], &self.queryer.uc_endpoints)
.path("/v2/domains")
.authorization(Authorization::v2(&self.credential))
.append_query_pair("tbl", self.bucket_name.as_str())
.accept_json()
.call()
.await?
.parse_json::<Vec<String>>()
.await?
.into_body()
.into_iter()
.map(Endpoint::from)
.collect();
Ok(endpoints)
}
sourcepub fn into_parts_and_body(self) -> (HttpResponseParts, B)
pub fn into_parts_and_body(self) -> (HttpResponseParts, B)
转换为 HTTP 响应信息和响应体
Examples found in repository?
src/client/call/utils.rs (line 388)
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
fn to_status_code_error(response: SyncResponse, retried: &RetriedStatsInfo) -> ApiResult<SyncResponse> {
let status_code = response.status_code();
let (parts, body) = response.parse_json::<ErrorResponseBody>()?.into_parts_and_body();
Err(
ResponseError::new_with_msg(ResponseErrorKind::StatusCodeError(status_code), body.into_error())
.response_parts(&parts)
.retried(retried),
)
}
}
fn check_x_req_id(response: &mut SyncResponse, retried: &RetriedStatsInfo) -> ApiResult<()> {
if response.x_reqid().is_some() {
Ok(())
} else {
Err(make_malicious_response(response.parts(), retried).read_response_body_sample(response.body_mut())?)
}
}
#[cfg(feature = "async")]
async fn async_check_x_req_id(response: &mut AsyncResponse, retried: &RetriedStatsInfo) -> ApiResult<()> {
if response.x_reqid().is_some() {
Ok(())
} else {
Err(make_malicious_response(response.parts(), retried)
.async_read_response_body_sample(response.body_mut())
.await?)
}
}
fn make_malicious_response(parts: &ResponseParts, retried: &RetriedStatsInfo) -> ResponseError {
ResponseError::new_with_msg(
ResponseErrorKind::MaliciousResponse,
"cannot find X-ReqId header from response, might be malicious response",
)
.response_parts(parts)
.retried(retried)
}
fn make_unexpected_status_code_error(parts: &ResponseParts, retried: &RetriedStatsInfo) -> ResponseError {
ResponseError::new_with_msg(
ResponseErrorKind::UnexpectedStatusCode(parts.status_code()),
format!("status code {} is unexpected", parts.status_code()),
)
.response_parts(parts)
.retried(retried)
}
#[cfg(feature = "async")]
mod async_utils {
use super::{
super::super::{AsyncResponse, InnerRequestParts},
*,
};
use qiniu_http::AsyncRequest as AsyncHttpRequest;
use std::future::Future;
pub(in super::super) async fn sign_async_request(
request: &mut AsyncHttpRequest<'_>,
authorization: Option<&Authorization<'_>>,
retried: &RetriedStatsInfo,
) -> Result<(), TryError> {
if let Some(authorization) = authorization {
authorization
.async_sign(request)
.await
.map_err(|err| handle_sign_request_error(err, retried))?;
}
Ok(())
}
pub(in super::super) async fn async_resolve(
parts: &InnerRequestParts<'_>,
domain_with_port: &DomainWithPort,
extensions: &mut Extensions,
retried: &RetriedStatsInfo,
) -> Result<Vec<IpAddrWithPort>, TryError> {
let answers = with_resolve_domain(parts, domain_with_port.domain(), extensions, retried, || async {
parts
.http_client()
.resolver()
.async_resolve(
domain_with_port.domain(),
ResolveOptions::builder().retried(retried).build(),
)
.await
});
return Ok(answers
.await?
.into_ip_addrs()
.iter()
.map(|&ip| IpAddrWithPort::new(ip, domain_with_port.port()))
.collect());
async fn with_resolve_domain<F: FnOnce() -> Fu, Fu: Future<Output = ResolveResult>>(
parts: &InnerRequestParts<'_>,
domain: &str,
extensions: &mut Extensions,
retried: &RetriedStatsInfo,
f: F,
) -> Result<ResolveAnswers, TryError> {
call_to_resolve_domain_callbacks(parts, domain, extensions, retried)?;
let answers = f()
.await
.map_err(|err| TryError::new(err, RetryDecision::TryNextServer.into()))?;
call_domain_resolved_callbacks(parts, domain, &answers, extensions, retried)?;
Ok(answers)
}
}
pub(in super::super) async fn async_choose(
parts: &InnerRequestParts<'_>,
ips: &[IpAddrWithPort],
extensions: &mut Extensions,
retried: &RetriedStatsInfo,
) -> Result<Vec<IpAddrWithPort>, TryError> {
call_to_choose_ips_callbacks(parts, ips, extensions, retried)?;
let chosen_ips = parts
.http_client()
.chooser()
.async_choose(ips, Default::default())
.await
.into_ip_addrs();
call_ips_chosen_callbacks(parts, ips, &chosen_ips, extensions, retried)?;
Ok(chosen_ips)
}
pub(in super::super) async fn async_judge(
mut response: AsyncResponse,
retried: &RetriedStatsInfo,
) -> ApiResult<AsyncResponse> {
return match response.status_code().as_u16() {
0..=199 | 300..=399 => Err(make_unexpected_status_code_error(response.parts(), retried)),
200..=299 => {
async_check_x_req_id(&mut response, retried).await?;
Ok(response)
}
_ => to_status_code_error(response, retried).await,
};
async fn to_status_code_error(response: AsyncResponse, retried: &RetriedStatsInfo) -> ApiResult<AsyncResponse> {
let status_code = response.status_code();
let (parts, body) = response.parse_json::<ErrorResponseBody>().await?.into_parts_and_body();
Err(
ResponseError::new_with_msg(ResponseErrorKind::StatusCodeError(status_code), body.into_error())
.response_parts(&parts)
.retried(retried),
)
}
More examples
sourcepub fn from_parts_and_body(parts: HttpResponseParts, body: B) -> Self
pub fn from_parts_and_body(parts: HttpResponseParts, body: B) -> Self
根据 HTTP 响应信息和响应体创建 HTTP 响应
sourcepub fn x_reqid(&self) -> Option<&HeaderValue>
pub fn x_reqid(&self) -> Option<&HeaderValue>
获取 HTTP 响应的 X-ReqId 信息
Examples found in repository?
src/client/call/utils.rs (line 398)
397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
fn check_x_req_id(response: &mut SyncResponse, retried: &RetriedStatsInfo) -> ApiResult<()> {
if response.x_reqid().is_some() {
Ok(())
} else {
Err(make_malicious_response(response.parts(), retried).read_response_body_sample(response.body_mut())?)
}
}
#[cfg(feature = "async")]
async fn async_check_x_req_id(response: &mut AsyncResponse, retried: &RetriedStatsInfo) -> ApiResult<()> {
if response.x_reqid().is_some() {
Ok(())
} else {
Err(make_malicious_response(response.parts(), retried)
.async_read_response_body_sample(response.body_mut())
.await?)
}
}
sourcepub fn x_log(&self) -> Option<&HeaderValue>
pub fn x_log(&self) -> Option<&HeaderValue>
获取 HTTP 响应的 X-Log 信息
source§impl Response<SyncResponseBody>
impl Response<SyncResponseBody>
sourcepub fn parse_json<T: DeserializeOwned>(self) -> ApiResult<Response<T>>
pub fn parse_json<T: DeserializeOwned>(self) -> ApiResult<Response<T>>
解析 JSON 响应体
Examples found in repository?
More examples
src/regions/regions_provider/bucket_regions_queryer.rs (line 296)
286 287 288 289 290 291 292 293 294 295 296 297 298
fn do_sync_query(&self) -> ApiResult<GotRegions> {
handle_response_body(
self.queryer
.http_client
.get(&[ServiceName::Uc, ServiceName::Api], &self.queryer.uc_endpoints)
.path("/v4/query")
.append_query_pair("ak", self.access_key.as_str())
.append_query_pair("bucket", self.bucket_name.as_str())
.accept_json()
.call()?
.parse_json::<ResponseBody>()?,
)
}
src/regions/endpoints_provider/bucket_domains_provider.rs (line 282)
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
fn do_sync_query(&self) -> ApiResult<Endpoints> {
let endpoints: Endpoints = self
.queryer
.http_client
.get(&[ServiceName::Uc], &self.queryer.uc_endpoints)
.path("/v2/domains")
.authorization(Authorization::v2(&self.credential))
.append_query_pair("tbl", self.bucket_name.as_str())
.accept_json()
.call()?
.parse_json::<Vec<String>>()?
.into_body()
.into_iter()
.map(Endpoint::from)
.collect();
Ok(endpoints)
}
source§impl Response<AsyncResponseBody>
impl Response<AsyncResponseBody>
sourcepub async fn parse_json<T: DeserializeOwned>(self) -> ApiResult<Response<T>>
pub async fn parse_json<T: DeserializeOwned>(self) -> ApiResult<Response<T>>
异步解析 JSON 响应体
Examples found in repository?
More examples
src/regions/regions_provider/all_regions_provider.rs (line 279)
270 271 272 273 274 275 276 277 278 279 280 281 282
async fn do_async_query(&self) -> ApiResult<GotRegions> {
handle_response_body(
self.http_client
.async_get(&[ServiceName::Uc], &self.uc_endpoints)
.path("/regions")
.authorization(Authorization::v2(&self.credential_provider))
.accept_json()
.call()
.await?
.parse_json::<ResponseBody>()
.await?,
)
}
src/regions/regions_provider/bucket_regions_queryer.rs (line 312)
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
async fn do_async_query(&self) -> ApiResult<GotRegions> {
handle_response_body(
self.queryer
.http_client
.async_get(&[ServiceName::Uc, ServiceName::Api], &self.queryer.uc_endpoints)
.path("/v4/query")
.append_query_pair("ak", self.access_key.as_str())
.append_query_pair("bucket", self.bucket_name.as_str())
.accept_json()
.call()
.await?
.parse_json::<ResponseBody>()
.await?,
)
}
src/regions/endpoints_provider/bucket_domains_provider.rs (line 302)
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
async fn do_async_query(&self) -> ApiResult<Endpoints> {
let endpoints: Endpoints = self
.queryer
.http_client
.async_get(&[ServiceName::Uc], &self.queryer.uc_endpoints)
.path("/v2/domains")
.authorization(Authorization::v2(&self.credential))
.append_query_pair("tbl", self.bucket_name.as_str())
.accept_json()
.call()
.await?
.parse_json::<Vec<String>>()
.await?
.into_body()
.into_iter()
.map(Endpoint::from)
.collect();
Ok(endpoints)
}
Methods from Deref<Target = HttpResponse<B>>§
Methods from Deref<Target = ResponseParts>§
sourcepub fn status_code(&self) -> StatusCode
pub fn status_code(&self) -> StatusCode
获取 HTTP 状态码
sourcepub fn status_code_mut(&mut self) -> &mut StatusCode
pub fn status_code_mut(&mut self) -> &mut StatusCode
获取 HTTP 状态码 的可变引用
sourcepub fn headers(&self) -> &HeaderMap<HeaderValue>
pub fn headers(&self) -> &HeaderMap<HeaderValue>
获取 HTTP Headers
sourcepub fn headers_mut(&mut self) -> &mut HeaderMap<HeaderValue>
pub fn headers_mut(&mut self) -> &mut HeaderMap<HeaderValue>
获取 HTTP Headers 的可变引用
sourcepub fn version_mut(&mut self) -> &mut Version
pub fn version_mut(&mut self) -> &mut Version
获取 HTTP 版本的可变引用
sourcepub fn extensions(&self) -> &Extensions
pub fn extensions(&self) -> &Extensions
获取 扩展信息
sourcepub fn extensions_mut(&mut self) -> &mut Extensions
pub fn extensions_mut(&mut self) -> &mut Extensions
获取扩展信息的可变引用
sourcepub fn header(&self, header_name: impl AsHeaderName) -> Option<&HeaderValue>
pub fn header(&self, header_name: impl AsHeaderName) -> Option<&HeaderValue>
获取 HTTP 响应 Header
sourcepub fn server_ip_mut(&mut self) -> &mut Option<IpAddr>
pub fn server_ip_mut(&mut self) -> &mut Option<IpAddr>
获取 HTTP 服务器 IP 地址的可变引用
sourcepub fn server_port(&self) -> Option<NonZeroU16>
pub fn server_port(&self) -> Option<NonZeroU16>
获取 HTTP 服务器端口号
sourcepub fn server_port_mut(&mut self) -> &mut Option<NonZeroU16>
pub fn server_port_mut(&mut self) -> &mut Option<NonZeroU16>
获取 HTTP 服务器端口号的可变引用
sourcepub fn metrics_mut(&mut self) -> &mut Option<Metrics>
pub fn metrics_mut(&mut self) -> &mut Option<Metrics>
获取 HTTP 响应的指标信息的可变引用
Trait Implementations§
source§impl<B> From<Response<B>> for HttpResponse<B>
impl<B> From<Response<B>> for HttpResponse<B>
Auto Trait Implementations§
impl<B> !RefUnwindSafe for Response<B>
impl<B> Send for Response<B>where
B: Send,
impl<B> Sync for Response<B>where
B: Sync,
impl<B> Unpin for Response<B>where
B: Unpin,
impl<B> !UnwindSafe for Response<B>
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,
Pipes by value. This is generally the method you want to use. Read more
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,
Borrows
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,
Mutably borrows
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,
Borrows
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,
Mutably borrows
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,
Immutable access to the
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,
Mutable access to the
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,
Immutable access to the
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,
Mutable access to the
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,
Immutable access to the
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,
Mutable access to the
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
Calls
.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
Calls
.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,
Calls
.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,
Calls
.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,
Calls
.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,
Calls
.tap_ref_mut()
only in debug builds, and is erased in release
builds.