Skip to content

Commit

Permalink
feat: fix prettifycation of json blobs that are not json
Browse files Browse the repository at this point in the history
  • Loading branch information
hartym committed Mar 17, 2024
1 parent 2a476f9 commit f22ea37
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 38 deletions.
7 changes: 7 additions & 0 deletions docs/reference/core/harp.config.adapters.hypercorn.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
harp.config.adapters.hypercorn module
=====================================

.. automodule:: harp.config.adapters.hypercorn
:members:
:undoc-members:
:show-inheritance:
18 changes: 18 additions & 0 deletions docs/reference/core/harp.config.adapters.rst
Original file line number Diff line number Diff line change
@@ -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:
7 changes: 0 additions & 7 deletions docs/reference/core/harp.config.http_adapters.base.rst

This file was deleted.

This file was deleted.

19 changes: 0 additions & 19 deletions docs/reference/core/harp.config.http_adapters.rst

This file was deleted.

2 changes: 1 addition & 1 deletion docs/reference/core/harp.config.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ Subpackages
.. toctree::
:maxdepth: 4

harp.config.adapters
harp.config.factories
harp.config.http_adapters
harp.config.settings

Submodules
Expand Down
17 changes: 16 additions & 1 deletion harp/models/blobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import hashlib
import json

import orjson

from harp.utils.bytes import ensure_bytes, ensure_str

from .base import Entity
Expand Down Expand Up @@ -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}")
9 changes: 6 additions & 3 deletions harp_apps/dashboard/controllers/blobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit f22ea37

Please sign in to comment.