Skip to content

Commit

Permalink
Changes:
Browse files Browse the repository at this point in the history
- fix circular import
  • Loading branch information
devkral committed Oct 21, 2024
1 parent d8cb7b2 commit 9dcecf0
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 12 deletions.
1 change: 0 additions & 1 deletion esmerald/responses/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,6 @@ def __init__(
),
] = None,
) -> None:

super().__init__(
content=content,
status_code=status_code,
Expand Down
14 changes: 5 additions & 9 deletions esmerald/responses/encoders.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
from typing import Any

from lilya.responses import JSONResponse as JSONResponse
import orjson

from esmerald.responses.json import BaseJSONResponse

try:
import orjson
from orjson import OPT_OMIT_MICROSECONDS, OPT_SERIALIZE_NUMPY
except ImportError: # pragma: no cover
orjson = None

try:
import ujson
except ImportError: # pragma: no cover
Expand All @@ -24,11 +18,10 @@ class ORJSONResponse(BaseJSONResponse):
"""

def make_response(self, content: Any) -> bytes:
assert orjson is not None, "You must install the encoders or orjson to use ORJSONResponse"
return orjson.dumps(
content,
default=self.transform,
option=OPT_SERIALIZE_NUMPY | OPT_OMIT_MICROSECONDS,
option=orjson.OPT_SERIALIZE_NUMPY | orjson.OPT_OMIT_MICROSECONDS,
)


Expand All @@ -42,3 +35,6 @@ class UJSONResponse(BaseJSONResponse):
def make_response(self, content: Any) -> bytes:
assert ujson is not None, "You must install the encoders or ujson to use UJSONResponse"
return ujson.dumps(content, ensure_ascii=False).encode("utf-8")


__all__ = ["ORJSONResponse", "UJSONResponse"]
6 changes: 5 additions & 1 deletion esmerald/responses/json.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from typing import Any, Dict, cast

from lilya.responses import JSONResponse

from esmerald.encoders import json_encoder
from esmerald.responses import JSONResponse as JSONResponse # noqa


class BaseJSONResponse(JSONResponse):
Expand All @@ -16,3 +17,6 @@ def transform(value: Any) -> Dict[str, Any]: # pragma: no cover
a dict().
"""
return cast(Dict[str, Any], json_encoder(value))


__all__ = ["BaseJSONResponse"]
2 changes: 1 addition & 1 deletion esmerald/routing/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
from esmerald.injector import Inject
from esmerald.permissions.utils import continue_or_raise_permission_exception
from esmerald.requests import Request
from esmerald.responses import JSONResponse, Response
from esmerald.responses.base import JSONResponse, Response
from esmerald.routing.apis.base import View
from esmerald.transformers.model import (
TransformerModel,
Expand Down

0 comments on commit 9dcecf0

Please sign in to comment.