diff --git a/docs/en/docs/responses.md b/docs/en/docs/responses.md index 7715de79..c25cd43a 100644 --- a/docs/en/docs/responses.md +++ b/docs/en/docs/responses.md @@ -18,7 +18,7 @@ The available responses from `Esmerald` are: * `Response` * `JSON` -* `OrJSON` +* `ORJSON` * `UJSON` * `Template` * `Redirect` @@ -27,14 +27,14 @@ The available responses from `Esmerald` are: ## Important requirements -Some responses use extra dependencies, such as [UJSON](#ujson) and [OrJSON](#orjson). To use these +Some responses use extra dependencies, such as [UJSON](#ujson) and [ORJSON](#orjson). To use these responses, you need to install: ```shell $ pip install ujson orjson ``` -This will allow you to use the [OrJSON](#orjson) and [UJSON](#ujson) as well as the +This will allow you to use the [ORJSON](#orjson) and [UJSON](#ujson) as well as the [UJSONResponse](#ujsonresponse) and [ORJSONResponse](#orjsonresponse) in your projects. ### Response @@ -61,7 +61,7 @@ Check out the [API Reference for Response](./references/responses/response.md) f ### JSON The classic JSON response for 99% of the responses used nowaday. The `JSON` returns a -`JSONResponse`. +`JSONResponse` (ORJSON). ```python {!> ../../../docs_src/responses/json.py !} @@ -71,7 +71,7 @@ The classic JSON response for 99% of the responses used nowaday. The `JSON` retu Check out the [API Reference for JSON](./references/responses/json.md) for more details. -#### JSONResponse +### JSONResponse (Lilya) You can always use directly the `JSONResponse` from Lilya without using the Esmerald wrapper. @@ -79,17 +79,12 @@ You can always use directly the `JSONResponse` from Lilya without using the Esme from lilya.responses import JSONResponse as JSONResponse ``` -or alternatively - -```python -from esmerald.responses import JSONResponse -``` ## API Reference Check out the [API Reference for JSONResponse](./references/responses/json-response.md) for more details. -### OrJSON +### ORJSON Super fast JSON serialization/deserialization response. @@ -101,7 +96,7 @@ Super fast JSON serialization/deserialization response. Please read the [important requirements](#important-requirements) before using this response. !!! Check - More details about the ORJSOM can be [found here](https://github.com/ijl/orjson). + More details about the ORJSON can be [found here](https://github.com/ijl/orjson). ## API Reference @@ -115,6 +110,12 @@ You can always use directly the `ORJSONResponse` from Esmerald without using the from esmerald.responses.encoders import ORJSONResponse ``` +or alternatively (we alias JSONResponse to ORJSONResponse because it is faster) + +```python +from esmerald.responses import JSONResponse +``` + ## API Reference Check out the [API Reference for ORJSONResponse](./references/responses/orjson-response.md) for more details. diff --git a/esmerald/responses/__init__.py b/esmerald/responses/__init__.py index dfa72de5..0bdaff28 100644 --- a/esmerald/responses/__init__.py +++ b/esmerald/responses/__init__.py @@ -2,12 +2,12 @@ Error, FileResponse, HTMLResponse, - JSONResponse, LilyaResponse, PlainText, Response, StreamingResponse, ) +from .encoders import ORJSONResponse as JSONResponse from .template import TemplateResponse __all__ = [ diff --git a/esmerald/responses/base.py b/esmerald/responses/base.py index 713a1854..22297f7a 100644 --- a/esmerald/responses/base.py +++ b/esmerald/responses/base.py @@ -163,7 +163,6 @@ def __init__( ), ] = None, ) -> None: - super().__init__( content=content, status_code=status_code, diff --git a/esmerald/responses/encoders.py b/esmerald/responses/encoders.py index 1cbdcd5c..bf04f905 100644 --- a/esmerald/responses/encoders.py +++ b/esmerald/responses/encoders.py @@ -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 @@ -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, ) @@ -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"] diff --git a/esmerald/responses/json.py b/esmerald/responses/json.py index e23811a8..2b7a7df0 100644 --- a/esmerald/responses/json.py +++ b/esmerald/responses/json.py @@ -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): @@ -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"] diff --git a/esmerald/routing/base.py b/esmerald/routing/base.py index cebdd583..b2d882a8 100644 --- a/esmerald/routing/base.py +++ b/esmerald/routing/base.py @@ -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,