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
[docs]
42class Link(Model):
43 """A link to another API document."""
44
45 href: str
46
47
[docs]
48class Links(Model):
49 """The ``_links`` block every document carries."""
50
51 self_: Link = field(name="self")
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
[docs]
80class NamedLink(Model):
81 """A reference that carries no id, such as a talent tree link."""
82
83 key: Link
84 name: str
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
116
117
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)