Changeset View
Changeset View
Standalone View
Standalone View
swh/core/api/serializers.py
Show All 34 Lines | def _encode_paged_result(obj: PagedResult) -> Dict[str, Any]: | ||||
} | } | ||||
def _decode_paged_result(obj: Dict[str, Any]) -> PagedResult: | def _decode_paged_result(obj: Dict[str, Any]) -> PagedResult: | ||||
"""Deserialize Dict into PagedResult""" | """Deserialize Dict into PagedResult""" | ||||
return PagedResult(results=obj["results"], next_page_token=obj["next_page_token"],) | return PagedResult(results=obj["results"], next_page_token=obj["next_page_token"],) | ||||
def exception_to_dict(exception: Exception) -> Dict[str, Any]: | |||||
tb = traceback.format_exception(None, exception, exception.__traceback__) | |||||
exc_type = type(exception) | |||||
return { | |||||
vlorentz: that's redundant now (complete document is something like `{"swhtype": "exception", "value"… | |||||
Done Inline ActionsI did that in a first place but was not sure about its impact so I reverted it. If you say it is not needed, I will update the diff. anlambert: I did that in a first place but was not sure about its impact so I reverted it.
If you say it… | |||||
"type": exc_type.__name__, | |||||
"module": exc_type.__module__, | |||||
"args": exception.args, | |||||
"message": str(exception), | |||||
"traceback": tb, | |||||
} | |||||
def dict_to_exception(exc_dict: Dict[str, Any]) -> Exception: | |||||
temp = __import__(exc_dict["module"], fromlist=[exc_dict["type"]]) | |||||
return getattr(temp, exc_dict["type"])(*exc_dict["args"]) | |||||
ENCODERS = [ | ENCODERS = [ | ||||
(arrow.Arrow, "arrow", arrow.Arrow.isoformat), | (arrow.Arrow, "arrow", arrow.Arrow.isoformat), | ||||
(datetime.datetime, "datetime", encode_datetime), | (datetime.datetime, "datetime", encode_datetime), | ||||
( | ( | ||||
datetime.timedelta, | datetime.timedelta, | ||||
"timedelta", | "timedelta", | ||||
lambda o: { | lambda o: { | ||||
"days": o.days, | "days": o.days, | ||||
"seconds": o.seconds, | "seconds": o.seconds, | ||||
"microseconds": o.microseconds, | "microseconds": o.microseconds, | ||||
}, | }, | ||||
), | ), | ||||
(UUID, "uuid", str), | (UUID, "uuid", str), | ||||
(PagedResult, "paged_result", _encode_paged_result), | (PagedResult, "paged_result", _encode_paged_result), | ||||
# Only for JSON: | # Only for JSON: | ||||
(bytes, "bytes", lambda o: base64.b85encode(o).decode("ascii")), | (bytes, "bytes", lambda o: base64.b85encode(o).decode("ascii")), | ||||
Not Done Inline ActionsWhy not directly: (Exception, "exception", exception_to_dict) ? Also, from where does exception-to-dict come from? ardumont: Why not directly:
```
(Exception, "exception", exception_to_dict)
```
?
Also, from where does… | |||||
Done Inline Actionsoh right, exception_to_dict is defined at the end of the file anlambert: oh right, `exception_to_dict` is defined at the end of the file | |||||
(Exception, "exception", exception_to_dict), | |||||
] | ] | ||||
DECODERS = { | DECODERS = { | ||||
"arrow": arrow.get, | "arrow": arrow.get, | ||||
"datetime": lambda d: iso8601.parse_date(d, default_timezone=None), | "datetime": lambda d: iso8601.parse_date(d, default_timezone=None), | ||||
"timedelta": lambda d: datetime.timedelta(**d), | "timedelta": lambda d: datetime.timedelta(**d), | ||||
"uuid": UUID, | "uuid": UUID, | ||||
"paged_result": _decode_paged_result, | "paged_result": _decode_paged_result, | ||||
# Only for JSON: | # Only for JSON: | ||||
"bytes": base64.b85decode, | "bytes": base64.b85decode, | ||||
"exception": dict_to_exception, | |||||
} | } | ||||
class MsgpackExtTypeCodes(Enum): | class MsgpackExtTypeCodes(Enum): | ||||
LONG_INT = 1 | LONG_INT = 1 | ||||
def encode_data_client(data: Any, extra_encoders=None) -> bytes: | def encode_data_client(data: Any, extra_encoders=None) -> bytes: | ||||
▲ Show 20 Lines • Show All 197 Lines • ▼ Show 20 Lines | try: | ||||
except TypeError: # msgpack < 0.6.0 | except TypeError: # msgpack < 0.6.0 | ||||
return msgpack.unpackb( | return msgpack.unpackb( | ||||
data, raw=False, object_hook=decode_types, ext_hook=ext_hook | data, raw=False, object_hook=decode_types, ext_hook=ext_hook | ||||
) | ) | ||||
except TypeError: # msgpack < 0.5.2 | except TypeError: # msgpack < 0.5.2 | ||||
return msgpack.unpackb( | return msgpack.unpackb( | ||||
data, encoding="utf-8", object_hook=decode_types, ext_hook=ext_hook | data, encoding="utf-8", object_hook=decode_types, ext_hook=ext_hook | ||||
) | ) | ||||
def exception_to_dict(exception): | |||||
tb = traceback.format_exception(None, exception, exception.__traceback__) | |||||
return { | |||||
"exception": { | |||||
"type": type(exception).__name__, | |||||
"args": exception.args, | |||||
"message": str(exception), | |||||
"traceback": tb, | |||||
} | |||||
} |
that's redundant now (complete document is something like {"swhtype": "exception", "value": {"exception": { ... } } }). Could you remove the "exception":?
It may need some tweaking in the exception handling