Model class for JSON. Serializes and de-serializes between JSON and the Python object representation of HTTP request and response bodies.
| 265 | |
| 266 | |
| 267 | class JsonModel(BaseModel): |
| 268 | """Model class for JSON. |
| 269 | |
| 270 | Serializes and de-serializes between JSON and the Python |
| 271 | object representation of HTTP request and response bodies. |
| 272 | """ |
| 273 | |
| 274 | accept = "application/json" |
| 275 | content_type = "application/json" |
| 276 | alt_param = "json" |
| 277 | |
| 278 | def __init__(self, data_wrapper=False): |
| 279 | """Construct a JsonModel. |
| 280 | |
| 281 | Args: |
| 282 | data_wrapper: boolean, wrap requests and responses in a data wrapper |
| 283 | """ |
| 284 | self._data_wrapper = data_wrapper |
| 285 | |
| 286 | def serialize(self, body_value): |
| 287 | if ( |
| 288 | isinstance(body_value, dict) |
| 289 | and "data" not in body_value |
| 290 | and self._data_wrapper |
| 291 | ): |
| 292 | body_value = {"data": body_value} |
| 293 | return json.dumps(body_value) |
| 294 | |
| 295 | def deserialize(self, content): |
| 296 | try: |
| 297 | content = content.decode("utf-8") |
| 298 | except AttributeError: |
| 299 | pass |
| 300 | try: |
| 301 | body = json.loads(content) |
| 302 | except json.decoder.JSONDecodeError: |
| 303 | body = content |
| 304 | else: |
| 305 | if self._data_wrapper and "data" in body: |
| 306 | body = body["data"] |
| 307 | return body |
| 308 | |
| 309 | @property |
| 310 | def no_content_response(self): |
| 311 | return {} |
| 312 | |
| 313 | |
| 314 | class RawModel(JsonModel): |
no outgoing calls
searching dependent graphs…