Source code for human_requests.abstraction.response

import json
from dataclasses import dataclass
from time import time
from typing import TYPE_CHECKING, Literal, Optional

from .http import URL
from .request import FetchRequest

if TYPE_CHECKING:
    from ..human_page import HumanPage


@dataclass(frozen=True)
[docs] class FetchResponse: """Represents the response of a request."""
[docs] request: FetchRequest
"""The request that was made."""
[docs] page: "HumanPage"
"""The page that made the request."""
[docs] url: URL
"""The URL of the response. Due to redirects, it can differ from `request.url`."""
[docs] headers: dict
"""The headers of the response."""
[docs] raw: bytes
"""The raw body of the response."""
[docs] status_code: int
"""The status code of the response."""
[docs] status_text: str
"""Человеко-читаемое представление status_code"""
[docs] redirected: bool
"""Был ли ответ сформировапн в следствии редиректа"""
[docs] type: Literal["basic", "cors", "error", "opaque", "opaqueredirect"]
[docs] duration: float
"""The duration of the request in seconds."""
[docs] end_time: float
"""Current time in seconds since the Epoch.""" @property
[docs] def text(self) -> str: """The body of the response.""" defchar = "utf-8" ct = self.headers.get("content-type", "") charset = ct.split("charset=")[-1] if "charset=" in ct else defchar return self.raw.decode(charset, errors="replace")
[docs] def json(self) -> dict | list: to_return = json.loads(self.text) assert isinstance(to_return, list) or isinstance( to_return, dict ), f"Response body is not JSON: {type(self.text).__name__}" return to_return
[docs] def seconds_ago(self) -> float: """How long ago was the request?""" return time() - self.end_time
[docs] async def render( self, retry: int = 2, timeout: Optional[float] = None, wait_until: Literal["commit", "load", "domcontentloaded", "networkidle"] = "commit", referer: Optional[str] = None, ) -> "HumanPage": """Renders the response content in the current browser. It will look like we requested it through the browser from the beginning. Recommended to use in cases when the server returns a JS challenge instead of a response.""" page = await self.page.context.new_page() await page.goto_render( self, wait_until=wait_until, referer=referer, timeout=timeout, retry=retry ) return page