-
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Changes: - cleanup responses - move transform speedup in mixin - improve tests and docs
- Loading branch information
Showing
8 changed files
with
158 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,3 @@ | ||
from typing import Any, Dict, cast | ||
|
||
from lilya.responses import JSONResponse | ||
|
||
from esmerald.encoders import json_encoder | ||
|
||
|
||
class BaseJSONResponse(JSONResponse): | ||
""" | ||
Making sure it parses all the values from pydantic into dictionary. | ||
""" | ||
|
||
@staticmethod | ||
def transform(value: Any) -> Dict[str, Any]: # pragma: no cover | ||
""" | ||
Makes sure that every value is checked and if it's a pydantic model then parses into | ||
a dict(). | ||
""" | ||
return cast(Dict[str, Any], json_encoder(value)) | ||
|
||
from lilya.responses import JSONResponse as BaseJSONResponse | ||
|
||
__all__ = ["BaseJSONResponse"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
from functools import partial | ||
from typing import ( | ||
Any, | ||
) | ||
|
||
import orjson | ||
from lilya.responses import ( | ||
RESPONSE_TRANSFORM_KWARGS, | ||
) | ||
|
||
|
||
class ORJSONTransformMixin: | ||
@classmethod | ||
def transform(cls, value: Any) -> Any: | ||
""" | ||
The transformation of the data being returned (simplify operation). | ||
Supports all the default encoders from Lilya and custom from Esmerald. | ||
""" | ||
transform_kwargs = RESPONSE_TRANSFORM_KWARGS.get() | ||
if transform_kwargs is None: | ||
transform_kwargs = {} | ||
else: | ||
transform_kwargs.copy() | ||
transform_kwargs.setdefault( | ||
"json_encode_fn", | ||
partial( | ||
orjson.dumps, option=orjson.OPT_SERIALIZE_NUMPY | orjson.OPT_OMIT_MICROSECONDS | ||
), | ||
) | ||
transform_kwargs.setdefault("post_transform_fn", orjson.loads) | ||
|
||
with cls.with_transform_kwargs(transform_kwargs): | ||
return super().transform(value) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters