This commit is contained in:
@@ -119,8 +119,17 @@ def ensure_discussion(domain: str) -> tuple[str, str]:
|
|||||||
|
|
||||||
|
|
||||||
def unwrap(body: object, root: str) -> dict:
|
def unwrap(body: object, root: str) -> dict:
|
||||||
"""AMS wraps responses with a root key (`{"groups": {...}}`); accept a bare object too."""
|
"""Unwrap an AMS response.
|
||||||
if isinstance(body, dict) and isinstance(body.get(root), dict):
|
|
||||||
return body[root]
|
`respond_with_collection` serializes even a single resource as an array under the root key, so
|
||||||
|
the shapes seen are `{"groups": [{...}]}`, `{"discussions": [{...}]}`, or (bare) `{...}`.
|
||||||
|
"""
|
||||||
|
if isinstance(body, dict) and root in body:
|
||||||
|
value = body[root]
|
||||||
|
if isinstance(value, list):
|
||||||
|
assert value, f"empty {root!r} collection in API response: {body!r}"
|
||||||
|
value = value[0]
|
||||||
|
if isinstance(value, dict):
|
||||||
|
return value
|
||||||
assert isinstance(body, dict), f"expected a JSON object, got {type(body).__name__}: {body!r}"
|
assert isinstance(body, dict), f"expected a JSON object, got {type(body).__name__}: {body!r}"
|
||||||
return body
|
return body
|
||||||
|
|||||||
Reference in New Issue
Block a user