Source code for wow._response

 1"""Raw access to a Battle.net response for callers who need headers or untyped JSON."""
 2
 3import email.utils
 4from typing import TYPE_CHECKING, Any
 5
 6from msgspec import Struct
 7
 8from wow._base import parse_model
 9
10if TYPE_CHECKING:
11    from datetime import datetime
12
13    import httpx
14
15__all__ = ["APIResponse"]
16
17
[docs] 18class APIResponse: 19 """A successful response from :meth:`Client.get <wow.Client.get>`. 20 21 Typed resource methods return models directly; this wrapper is for headers, documents the package has no model 22 for yet, and links followed by hand. 23 """ 24 25 __slots__ = ("http_response",) 26 27 http_response: httpx.Response 28 """The underlying ``httpx`` response.""" 29 30 def __init__(self, http_response: httpx.Response) -> None: 31 self.http_response = http_response 32 33 @property 34 def status_code(self) -> int: 35 """The HTTP status code.""" 36 return self.http_response.status_code 37 38 @property 39 def url(self) -> str: 40 """The URL that was requested, including query parameters.""" 41 return str(self.http_response.url) 42 43 @property 44 def headers(self) -> httpx.Headers: 45 """The response headers.""" 46 return self.http_response.headers 47 48 @property 49 def namespace(self) -> str | None: 50 """The namespace Battle.net resolved, such as ``static-1.60.1_70399-forever-us`` for ``static-forever-us``.""" 51 return self.headers.get("battlenet-namespace") 52 53 @property 54 def last_modified(self) -> datetime | None: 55 """When the document last changed, from the ``Last-Modified`` header.""" 56 header = self.headers.get("last-modified") 57 return email.utils.parsedate_to_datetime(header) if header else None 58
[docs] 59 def json(self) -> Any: # noqa: ANN401 - arbitrary JSON, like httpx.Response.json 60 """Decode the body as JSON.""" 61 return self.http_response.json()
62
[docs] 63 def parse[ModelT: Struct](self, model: type[ModelT]) -> ModelT: 64 """Validate the body into ``model``. 65 66 Raises: 67 APIResponseValidationError: The body does not match ``model``. 68 """ 69 return parse_model(self.http_response, model)