Models#
Each typed method returns a model: an immutable msgspec Struct that holds one Battle.net
document. The models are in wow.models. Each model comes from documents that Battle.net sent, not from a
specification, because Blizzard does not publish one for Forever.
Read a model#
A field has the name that Battle.net uses. Five names change, so that each one is a valid Python name:
Battle.net |
Model |
|---|---|
|
|
|
|
|
|
|
|
|
|
druid = client.playable_classes.get(11)
druid.name # "Druid"
druid.links.self_.href
[spec.name for spec in druid.specializations]
druid = await client.playable_classes.get(11)
druid.name # "Druid"
druid.links.self_.href
[spec.name for spec in druid.specializations]
You cannot change a model. If you set a field, Python raises AttributeError. To make a changed copy, use
msgspec.structs.replace:
import msgspec
renamed = msgspec.structs.replace(druid, name="Shapeshifter")
Fields that can be missing#
Battle.net leaves out some fields on some documents. Each of these fields has a default:
Kind of field |
Default |
|---|---|
A value or a nested object |
|
A list |
|
A flag, such as |
|
For example, a talent for a whole class has no specialization, so its playable_specialization is None. Some
talents are only an id, so all their other fields have the default.
Fields that a model does not declare#
When Battle.net adds a field, the model skips it, and the response still decodes. To read the new field before a release adds it, get the raw document:
raw = client.get("/data/wow/mount/9", namespace="static").json()
raw["new_field"]
raw = (await client.get("/data/wow/mount/9", namespace="static")).json()
raw["new_field"]
Turn a model into JSON#
msgspec converts a model back to the names and the shape that Battle.net uses:
msgspec.to_builtins(druid) # dict
msgspec.json.encode(druid) # JSON bytes
Decode errors#
If a response does not agree with its model, the client raises APIResponseValidationError. The error
has the response. Its __cause__ is the msgspec.ValidationError, which gives the path of the bad field:
Object missing required field `faction` - at `$.results[3].data`
The error never shows the value of a field, so a token in a response cannot get into your logs.
How the models stay correct#
The test suite decodes each recorded document, then encodes it again. If a field is missing from the result, the test
fails and gives the path of the field. make audit-models does the same check on each document that it can get from
the live API. The development guide
says when to run it.