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