halfapi/halfapi/lib/responses.py
Maxime Alves LIRMM@home c99e636d6e [0.5.13]
Squashed commit of the following:

commit 4552d85cc49fda572e54aa9c8054031554bfcb3a
Author: Maxime Alves LIRMM@home <maxime.alves@lirmm.fr>
Date:   Thu Oct 7 13:57:37 2021 +0200

    [0.5.13]

commit 38032acfac559155b31c12cf12673c81b7cfdf20
Author: Maxime Alves LIRMM@home <maxime.alves@lirmm.fr>
Date:   Thu Oct 7 13:57:12 2021 +0200

    add 503 error code

commit 6f516e844b0f3786aa571d1ac8d575247ff7b7fe
Author: Maxime Alves LIRMM@home <maxime.alves@lirmm.fr>
Date:   Thu Oct 7 13:26:15 2021 +0200

    [ci] add run halfapi --version
2021-10-07 13:58:03 +02:00

113 lines
2.8 KiB
Python

#!/usr/bin/env python3
# builtins
""" Response module
Contains some base response classes
Classes :
- HJSONResponse
- InternalServerErrorResponse
- NotFoundResponse
- NotImplementedResponse
- ORJSONResponse
- PlainTextResponse
- ServiceUnavailableResponse
- UnauthorizedResponse
"""
import decimal
import typing
import orjson
# asgi framework
from starlette.responses import PlainTextResponse, Response, JSONResponse
from .jwt_middleware import JWTUser, Nobody
__all__ = [
'HJSONResponse',
'InternalServerErrorResponse',
'NotFoundResponse',
'NotImplementedResponse',
'ORJSONResponse',
'PlainTextResponse',
'ServiceUnavailableResponse',
'UnauthorizedResponse']
class InternalServerErrorResponse(Response):
""" The 500 Internal Server Error default Response
"""
def __init__(self, *args, **kwargs):
super().__init__(status_code=500)
class NotFoundResponse(Response):
""" The 404 Not Found default Response
"""
def __init__(self, *args, **kwargs):
super().__init__(status_code=404)
class NotImplementedResponse(Response):
""" The 501 Not Implemented default Response
"""
def __init__(self, *args, **kwargs):
super().__init__(status_code=501)
class ServiceUnavailableResponse(Response):
""" The 503 Service Unavailable default Response
"""
def __init__(self, *args, **kwargs):
super().__init__(status_code=503)
class UnauthorizedResponse(Response):
""" The 401 Not Found default Response
"""
def __init__(self, *args, **kwargs):
super().__init__(status_code = 401)
class ORJSONResponse(JSONResponse):
""" The response that encodes data into JSON
"""
def __init__(self, content, default=None, **kwargs):
self.default = default if default is not None else ORJSONResponse.default_cast
super().__init__(content, **kwargs)
def render(self, content: typing.Any) -> bytes:
return orjson.dumps(content,
option=orjson.OPT_NON_STR_KEYS,
default=self.default)
@staticmethod
def default_cast(typ):
""" Cast the data in JSON-serializable type
"""
str_types = {
decimal.Decimal
}
list_types = {
set
}
jsonable_types = {
JWTUser, Nobody
}
if type(typ) in str_types:
return str(typ)
if type(typ) in list_types:
return list(typ)
if type(typ) in jsonable_types:
return typ.json
raise TypeError(f'Type {type(typ)} is not handled by ORJSONResponse')
class HJSONResponse(ORJSONResponse):
""" The response that encodes generator data into JSON
"""
def render(self, content: typing.Generator):
return super().render(list(content))