[responses] cast special types in ORJSONResponse
This commit is contained in:
parent
f8e546007c
commit
d21ee175e9
|
@ -1,5 +1,5 @@
|
|||
#!/usr/bin/env python3
|
||||
__version__ = '0.3.1'
|
||||
__version__ = '0.4.1'
|
||||
|
||||
def version():
|
||||
return f'HalfAPI version:{__version__}'
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
#!/usr/bin/env python3
|
||||
# builtins
|
||||
import decimal
|
||||
import typing as typ
|
||||
import orjson
|
||||
|
||||
|
@ -47,7 +48,7 @@ class UnauthorizedResponse(Response):
|
|||
|
||||
class ORJSONResponse(JSONResponse):
|
||||
def __init__(self, content, default=None, **kwargs):
|
||||
self.default = default
|
||||
self.default = default if default is not None else ORJSONResponse.default_cast
|
||||
super().__init__(content, **kwargs)
|
||||
|
||||
def render(self, content: typ.Any) -> bytes:
|
||||
|
@ -55,6 +56,17 @@ class ORJSONResponse(JSONResponse):
|
|||
option=orjson.OPT_NON_STR_KEYS,
|
||||
default=self.default)
|
||||
|
||||
@staticmethod
|
||||
def default_cast(x):
|
||||
types = {
|
||||
decimal.Decimal
|
||||
}
|
||||
|
||||
if type(x) not in types:
|
||||
raise TypeError('Type is not handled in ORJSONResponse')
|
||||
|
||||
return str(x)
|
||||
|
||||
|
||||
class HJSONResponse(ORJSONResponse):
|
||||
def render(self, content: typ.Generator):
|
||||
|
|
Loading…
Reference in New Issue