Skip to content

Commit db1505f

Browse files
committed
Type report responses as JSON
1 parent 488ec06 commit db1505f

7 files changed

Lines changed: 41 additions & 17 deletions

File tree

docs/source/api-reference.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ API Reference
3333
:undoc-members:
3434
:members:
3535

36+
.. automodule:: vws.json_types
37+
:undoc-members:
38+
:members:
39+
3640
.. automodule:: vws.include_target_data
3741
:undoc-members:
3842
:members:

src/vws/_json_utils.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55

66
from beartype.door import TypeHint
77

8-
type JSONValue = (
9-
bool | int | float | str | list[JSONValue] | dict[str, JSONValue] | None
10-
)
8+
from vws.json_types import JSONValue
119

1210

1311
def _is_json_object(value: object, /) -> TypeGuard[dict[str, JSONValue]]:

src/vws/_model_targets.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
from beartype import BeartypeConf, beartype
99

10-
from vws._json_utils import JSONValue, json_object
10+
from vws._json_utils import json_object
1111
from vws.exceptions.custom_exceptions import ServerError
1212
from vws.exceptions.model_target_exceptions import (
1313
ModelTargetAuthenticationError,
@@ -18,6 +18,7 @@
1818
UnknownModelTargetDatasetError,
1919
)
2020
from vws.exceptions.vws_exceptions import TooManyRequestsError
21+
from vws.json_types import JSONValue
2122
from vws.model_target_datasets import (
2223
ModelTargetDatasetType,
2324
ModelTargetModel,

src/vws/exceptions/model_target_exceptions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99
from beartype import beartype
1010

1111
from vws._json_utils import (
12-
JSONValue,
1312
json_object,
1413
object_field,
1514
object_list_field,
1615
string_field,
1716
)
17+
from vws.json_types import JSONValue
1818
from vws.reports import ModelTargetGenerationDetail
1919
from vws.response import Response
2020

src/vws/json_types.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
"""JSON value types used by Vuforia API requests and responses."""
2+
3+
type JSONValue = (
4+
bool | int | float | str | list[JSONValue] | dict[str, JSONValue] | None
5+
)
6+
"""A value which can be represented in a JSON document."""

src/vws/reports.py

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
from beartype import BeartypeConf, beartype
1212
from beartype.door import TypeHint
1313

14+
from vws.json_types import JSONValue
15+
1416

1517
def _checked[T](value: object, hint: type[T], /) -> T:
1618
"""Return a value after checking its runtime type."""
@@ -64,7 +66,9 @@ class DatabaseSummaryReport:
6466
total_recos: int
6567

6668
@classmethod
67-
def from_response_dict(cls, response_dict: Mapping[str, object]) -> Self:
69+
def from_response_dict(
70+
cls, response_dict: Mapping[str, JSONValue]
71+
) -> Self:
6872
"""Construct from a VWS API response dict."""
6973
return cls(
7074
active_images=int(_number(response_dict["active_images"])),
@@ -120,7 +124,9 @@ class TargetSummaryReport:
120124
previous_month_recos: int
121125

122126
@classmethod
123-
def from_response_dict(cls, response_dict: Mapping[str, object]) -> Self:
127+
def from_response_dict(
128+
cls, response_dict: Mapping[str, JSONValue]
129+
) -> Self:
124130
"""Construct from a VWS API response dict."""
125131
return cls(
126132
status=TargetStatuses(
@@ -185,13 +191,13 @@ class QueryResult:
185191
@classmethod
186192
def from_response_dict(
187193
cls,
188-
response_dict: Mapping[str, object],
194+
response_dict: Mapping[str, JSONValue],
189195
) -> Self:
190196
"""Construct from a VWS API query result item dict."""
191197
target_data: TargetData | None = None
192198
if "target_data" in response_dict:
193199
target_data_dict = _checked(
194-
response_dict["target_data"], dict[str, object]
200+
response_dict["target_data"], dict[str, JSONValue]
195201
)
196202
target_timestamp = datetime.datetime.fromtimestamp(
197203
timestamp=_number(target_data_dict["target_timestamp"]),
@@ -223,11 +229,13 @@ class TargetStatusAndRecord:
223229
target_record: TargetRecord
224230

225231
@classmethod
226-
def from_response_dict(cls, response_dict: Mapping[str, object]) -> Self:
232+
def from_response_dict(
233+
cls, response_dict: Mapping[str, JSONValue]
234+
) -> Self:
227235
"""Construct from a VWS API response dict."""
228236
status = TargetStatuses(value=_checked(response_dict["status"], str))
229237
target_record_dict = _checked(
230-
response_dict["target_record"], dict[str, object]
238+
response_dict["target_record"], dict[str, JSONValue]
231239
)
232240
target_record = TargetRecord(
233241
target_id=_checked(target_record_dict["target_id"], str),
@@ -260,7 +268,9 @@ class RecoCountsReportRequest:
260268
"""
261269

262270
@classmethod
263-
def from_response_dict(cls, response_dict: Mapping[str, object]) -> Self:
271+
def from_response_dict(
272+
cls, response_dict: Mapping[str, JSONValue]
273+
) -> Self:
264274
"""Construct from a VWS API response dict."""
265275
return cls(
266276
transaction_id=_checked(response_dict["transaction_id"], str),
@@ -353,12 +363,12 @@ class ModelTargetDatasetStatusReport:
353363
@classmethod
354364
def from_response_dict(
355365
cls,
356-
response_dict: Mapping[str, object],
366+
response_dict: Mapping[str, JSONValue],
357367
) -> Self:
358368
"""Construct from a Model Target Web API response dict."""
359369
error: ModelTargetGenerationError | None = None
360370
if "error" in response_dict:
361-
error_dict = _checked(response_dict["error"], dict[str, object])
371+
error_dict = _checked(response_dict["error"], dict[str, JSONValue])
362372
error = ModelTargetGenerationError(
363373
code=_checked(error_dict["code"], str),
364374
message=_checked(error_dict["message"], str),
@@ -367,10 +377,10 @@ def from_response_dict(
367377
warning: ModelTargetGenerationWarning | None = None
368378
if "warning" in response_dict:
369379
warning_dict = _checked(
370-
response_dict["warning"], dict[str, object]
380+
response_dict["warning"], dict[str, JSONValue]
371381
)
372382
details = _checked(
373-
warning_dict["details"], list[dict[str, object]]
383+
warning_dict["details"], list[dict[str, JSONValue]]
374384
)
375385
warning = ModelTargetGenerationWarning(
376386
code=_checked(warning_dict["code"], str),

tests/test_reports.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44

55
from vws.reports import QueryResult
66

7+
type JSONValue = (
8+
bool | int | float | str | list[JSONValue] | dict[str, JSONValue] | None
9+
)
10+
711

812
@pytest.mark.parametrize(
913
argnames="response",
@@ -28,7 +32,8 @@
2832
],
2933
)
3034
def test_query_result_rejects_invalid_response_values(
31-
*, response: dict[str, object]
35+
*,
36+
response: dict[str, JSONValue],
3237
) -> None:
3338
"""Query reports reject values of the wrong type."""
3439
with pytest.raises(expected_exception=TypeError):

0 commit comments

Comments
 (0)