Source code for wow.models._common

  1"""Building blocks shared by every document model."""
  2
  3from typing import dataclass_transform
  4
  5from msgspec import Struct, field
  6
  7__all__ = [
  8    "Document",
  9    "GenderedName",
 10    "IdName",
 11    "IdRef",
 12    "Link",
 13    "Links",
 14    "LocalizedString",
 15    "Media",
 16    "MediaAsset",
 17    "Model",
 18    "NamedLink",
 19    "Ref",
 20    "SearchPage",
 21    "SearchResult",
 22    "TypedName",
 23]
 24
 25type LocalizedString = dict[str, str]
 26"""Text in every locale, keyed like ``{"en_US": "Sword", "de_DE": "Schwert"}``.
 27
 28Search results always come back this way, whatever ``locale`` the client uses.
 29"""
 30
 31
 32# msgspec subclasses inherit ``frozen`` at runtime; this tells type checkers so (PEP 681).
 33@dataclass_transform(frozen_default=True, field_specifiers=(field,))
[docs] 34class Model(Struct, frozen=True): 35 """Base for every response model. 36 37 Models are immutable. Fields they do not declare are skipped, so a field Blizzard adds tomorrow is never fatal; read 38 it from the raw document with :meth:`wow.Client.get` until a release models it. 39 """
40 41 46 47 52 53
[docs] 54class Document(Model): 55 """A top-level API document.""" 56 57 links: Links = field(name="_links")
58 59
[docs] 60class IdName(Model): 61 """An id and a localized name.""" 62 63 id: int 64 name: str
65 66
[docs] 67class Ref(IdName): 68 """A reference to another document: its id, name and link, as in every index.""" 69 70 key: Link
71 72
[docs] 73class IdRef(Model): 74 """A reference that carries no name, such as a media or appearance link.""" 75 76 id: int 77 key: Link
78 79 85 86
[docs] 87class TypedName(Model): 88 """A constant plus its localized label, such as ``{"type": "HORDE", "name": "Horde"}``. 89 90 ``type`` is kept as a plain string so values Blizzard adds later still validate. 91 """ 92 93 type: str 94 name: str
95 96
[docs] 97class GenderedName(Model): 98 """A name with a male and a female form.""" 99 100 male: str 101 female: str
102 103
[docs] 104class MediaAsset(Model): 105 """One rendered asset. 106 107 Attributes: 108 key: The asset kind, such as ``icon``. 109 value: Its URL on ``render.worldofwarcraft.com``. Forever assets live under ``/forever-{region}/``. 110 file_data_id: The game client's file id. Guild crest media has none. 111 """ 112 113 key: str 114 value: str 115 file_data_id: int | None = None
116 117
[docs] 118class Media(Document): 119 """A media document: the rendered assets for one item, class, or other entity.""" 120 121 id: int 122 assets: tuple[MediaAsset, ...]
123 124
[docs] 125class SearchResult[DataT: Model](Model): 126 """One search hit: the link to the full document plus its indexed, all-locale data.""" 127 128 key: Link 129 data: DataT
130 131
[docs] 132class SearchPage[DataT: Model](Model): 133 """One page of search results. 134 135 Battle.net stops at 1,000 results per query however many pages that spans, and sets ``result_count_capped`` when 136 it cut the results short. 137 """ 138 139 page: int 140 page_size: int = field(name="pageSize") 141 max_page_size: int = field(name="maxPageSize") 142 page_count: int = field(name="pageCount") 143 results: tuple[SearchResult[DataT], ...] 144 result_count_capped: bool = field(name="resultCountCapped", default=False)