MCPcopy Create free account
hub / github.com/apify/impit / from_async

Method from_async

impit-python/src/response.rs:518–601  ·  view source on GitHub ↗
(
        val: Response,
        preferred_encoding: Option<String>,
        stream: bool,
    )

Source from the content-addressed store, hash-verified

516 }
517
518 pub async fn from_async(
519 val: Response,
520 preferred_encoding: Option<String>,
521 stream: bool,
522 ) -> Result<Self, ImpitError> {
523 let status_code = val.status().as_u16();
524 let url = val.url().to_string();
525 let reason_phrase = val
526 .status()
527 .canonical_reason()
528 .unwrap_or_default()
529 .to_string();
530 let http_version = match val.version() {
531 Version::HTTP_09 => "HTTP/0.9".to_string(),
532 Version::HTTP_10 => "HTTP/1.0".to_string(),
533 Version::HTTP_11 => "HTTP/1.1".to_string(),
534 Version::HTTP_2 => "HTTP/2".to_string(),
535 Version::HTTP_3 => "HTTP/3".to_string(),
536 _ => "Unknown".to_string(),
537 };
538 let is_redirect = val.status().is_redirection();
539 let headers = HashMap::from_iter(val.headers().iter().map(|(k, v)| {
540 (
541 k.as_str().to_string(),
542 v.as_bytes().iter().map(|&b| b as char).collect::<String>(),
543 )
544 }));
545
546 let content_type_charset = headers
547 .get("content-type")
548 .and_then(|ct| ContentType::from(ct).ok())
549 .and_then(|ct| ct.into());
550
551 let (content, inner_state, encoding, inner, is_closed, is_stream_consumed) = if !stream {
552 let content = val
553 .bytes()
554 .await
555 .map(|b| b.to_vec())
556 .map_err(|e| ImpitError::from(e, None))?;
557 let encoding = preferred_encoding
558 .and_then(|e| encoding_from_whatwg_label(&e))
559 .or(content_type_charset)
560 .or(impit::utils::determine_encoding(content.as_slice()))
561 .unwrap_or(impit::utils::encodings::UTF_8);
562
563 (
564 Some(content),
565 InnerResponseState::Read,
566 encoding,
567 None,
568 true,
569 true,
570 )
571 } else {
572 let encoding = preferred_encoding
573 .and_then(|e| encoding_from_whatwg_label(&e))
574 .or(content_type_charset)
575 .unwrap_or(impit::utils::encodings::UTF_8);

Callers

nothing calls this directly

Calls 4

determine_encodingFunction · 0.85
iterMethod · 0.80
bytesMethod · 0.80
getMethod · 0.45

Tested by

no test coverage detected