Represents an HTTP response from an API request. Attributes: status: HTTP status code. status_text: HTTP status text. headers: Response headers as a dict. url: The request URL.
| 50 | |
| 51 | |
| 52 | class APIResponse: |
| 53 | """Represents an HTTP response from an API request. |
| 54 | |
| 55 | Attributes: |
| 56 | status: HTTP status code. |
| 57 | status_text: HTTP status text. |
| 58 | headers: Response headers as a dict. |
| 59 | url: The request URL. |
| 60 | """ |
| 61 | |
| 62 | def __init__(self, status: int, status_text: str, headers: dict[str, str], url: str, body: bytes) -> None: |
| 63 | self.status = status |
| 64 | self.status_text = status_text |
| 65 | self.headers = headers |
| 66 | self.url = url |
| 67 | self._body = body |
| 68 | |
| 69 | @property |
| 70 | def ok(self) -> bool: |
| 71 | """Whether the response status is in the 200-299 range.""" |
| 72 | return 200 <= self.status <= 299 |
| 73 | |
| 74 | def json(self) -> Any: |
| 75 | """Parse the response body as JSON. |
| 76 | |
| 77 | Returns: |
| 78 | The parsed JSON object. |
| 79 | """ |
| 80 | return json.loads(self._body) |
| 81 | |
| 82 | def text(self) -> str: |
| 83 | """Decode the response body as UTF-8 text. |
| 84 | |
| 85 | Returns: |
| 86 | The response body as a string. |
| 87 | """ |
| 88 | return self._body.decode("utf-8") |
| 89 | |
| 90 | def body(self) -> bytes: |
| 91 | """Return the raw response body bytes. |
| 92 | |
| 93 | Returns: |
| 94 | The response body as bytes. |
| 95 | """ |
| 96 | return self._body |
| 97 | |
| 98 | def dispose(self) -> None: |
| 99 | """Free the response body memory.""" |
| 100 | self._body = b"" |
| 101 | |
| 102 | |
| 103 | def _cookie_matches(cookie: Cookie, url: str, default_domain: str = "") -> bool: |
no outgoing calls