import json
# -------------------------------------------------- #
# Response wrapping/unwrapping exceptions definition #
# -------------------------------------------------- #
[docs]class ResponseUnwrappingException(Exception): pass
[docs]class ResponseWrappingException(Exception): pass
# --------------------------------------- #
# Response wrapping/unwrapping definition #
# --------------------------------------- #
[docs]class ResponseWrapper(object):
"""Class implementing Response wrapper (wrapping and unwrapping responses)"""
[docs] @staticmethod
def unwrap_response(response):
"""Unwraps the response (deserialize from JSON-string)"""
try:
return json.loads(response) if isinstance(response, str) else response
except Exception as e:
raise ResponseUnwrappingException(e)
[docs] @staticmethod
def wrap_response(response):
"""Wraps the response (serialize to JSON-string)"""
try:
return json.dumps(response) if not isinstance(response, str) else response
except Exception as e:
raise ResponseWrappingException(e)
# ----------------------------- #
# HTTPError wrapping definition #
# ----------------------------- #
[docs]class HttpErrorWrapper(object):
"""Class implementing HTTPError wrapper (wrapping errors)"""
def __init__(self, e):
self.e = e
def __str__(self):
return f"{self.e.__class__.__name__}: {self.e}"