diff --git a/docs/reference/core/harp.config.adapters.hypercorn.rst b/docs/reference/core/harp.config.adapters.hypercorn.rst new file mode 100644 index 00000000..bcb86401 --- /dev/null +++ b/docs/reference/core/harp.config.adapters.hypercorn.rst @@ -0,0 +1,7 @@ +harp.config.adapters.hypercorn module +===================================== + +.. automodule:: harp.config.adapters.hypercorn + :members: + :undoc-members: + :show-inheritance: diff --git a/docs/reference/core/harp.config.adapters.rst b/docs/reference/core/harp.config.adapters.rst new file mode 100644 index 00000000..dd062525 --- /dev/null +++ b/docs/reference/core/harp.config.adapters.rst @@ -0,0 +1,18 @@ +harp.config.adapters package +============================ + +Submodules +---------- + +.. toctree:: + :maxdepth: 4 + + harp.config.adapters.hypercorn + +Module contents +--------------- + +.. automodule:: harp.config.adapters + :members: + :undoc-members: + :show-inheritance: diff --git a/docs/reference/core/harp.config.http_adapters.base.rst b/docs/reference/core/harp.config.http_adapters.base.rst deleted file mode 100644 index 4f1dca38..00000000 --- a/docs/reference/core/harp.config.http_adapters.base.rst +++ /dev/null @@ -1,7 +0,0 @@ -harp.config.http\_adapters.base module -====================================== - -.. automodule:: harp.config.http_adapters.base - :members: - :undoc-members: - :show-inheritance: diff --git a/docs/reference/core/harp.config.http_adapters.hypercorn_adapter.rst b/docs/reference/core/harp.config.http_adapters.hypercorn_adapter.rst deleted file mode 100644 index 76718898..00000000 --- a/docs/reference/core/harp.config.http_adapters.hypercorn_adapter.rst +++ /dev/null @@ -1,7 +0,0 @@ -harp.config.http\_adapters.hypercorn\_adapter module -==================================================== - -.. automodule:: harp.config.http_adapters.hypercorn_adapter - :members: - :undoc-members: - :show-inheritance: diff --git a/docs/reference/core/harp.config.http_adapters.rst b/docs/reference/core/harp.config.http_adapters.rst deleted file mode 100644 index 29193779..00000000 --- a/docs/reference/core/harp.config.http_adapters.rst +++ /dev/null @@ -1,19 +0,0 @@ -harp.config.http\_adapters package -================================== - -Submodules ----------- - -.. toctree:: - :maxdepth: 4 - - harp.config.http_adapters.base - harp.config.http_adapters.hypercorn_adapter - -Module contents ---------------- - -.. automodule:: harp.config.http_adapters - :members: - :undoc-members: - :show-inheritance: diff --git a/docs/reference/core/harp.config.rst b/docs/reference/core/harp.config.rst index 502efb7c..111efef2 100644 --- a/docs/reference/core/harp.config.rst +++ b/docs/reference/core/harp.config.rst @@ -7,8 +7,8 @@ Subpackages .. toctree:: :maxdepth: 4 + harp.config.adapters harp.config.factories - harp.config.http_adapters harp.config.settings Submodules diff --git a/harp/models/blobs.py b/harp/models/blobs.py index 3f6930fd..5c71785e 100644 --- a/harp/models/blobs.py +++ b/harp/models/blobs.py @@ -2,6 +2,8 @@ import hashlib import json +import orjson + from harp.utils.bytes import ensure_bytes, ensure_str from .base import Entity @@ -35,6 +37,19 @@ def __bool__(self): def prettify(self): if self.content_type == "application/json": - return json.dumps(json.loads(self.data), indent=4) + try: + data = orjson.loads(self.data) + except orjson.JSONDecodeError: + try: + data = json.loads(self.data) + except json.JSONDecodeError as exc: + raise ValueError("Could not decode json data.") from exc + try: + return orjson.dumps(data, option=orjson.OPT_INDENT_2) + except orjson.JSONEncodeError: + try: + return json.dumps(data, indent=4) + except json.JSONDecodeError as exc: + raise ValueError("Could not encode json data.") from exc raise NotImplementedError(f"Cannot prettify blob of type {self.content_type}") diff --git a/harp_apps/dashboard/controllers/blobs.py b/harp_apps/dashboard/controllers/blobs.py index fac1937c..c69c55ff 100644 --- a/harp_apps/dashboard/controllers/blobs.py +++ b/harp_apps/dashboard/controllers/blobs.py @@ -17,6 +17,9 @@ async def get(self, id): content_type = blob.content_type or "application/octet-stream" - if content_type == "application/json": - return HttpResponse(blob.prettify(), content_type=content_type) - return HttpResponse(blob.data, content_type=content_type) + try: + data = blob.prettify() + except (ValueError, NotImplementedError): + data = blob.data + + return HttpResponse(data, content_type=content_type)