[New Feature] Rest API implementation to showcase the OpenDBM features

This commit is contained in:
Rudy Haryanto
2022-10-04 03:07:29 +07:00
parent a574bc6870
commit 92e08860a8
41 changed files with 2576 additions and 0 deletions

View File

@@ -0,0 +1,51 @@
from fastapi import Request
from starlette.responses import JSONResponse
class AppExceptionCase(Exception):
def __init__(self, status_code: int, context: dict):
self.exception_case = self.__class__.__name__
self.status_code = status_code
self.context = context
def __str__(self):
return (
f"<AppException {self.exception_case} - "
+ f"status_code={self.status_code} - context={self.context}>"
)
async def app_exception_handler(request: Request, exc: AppExceptionCase):
return JSONResponse(
status_code=exc.status_code,
content={
"app_exception": exc.exception_case,
"context": exc.context,
},
)
class AppException(object):
class FooCreateItem(AppExceptionCase):
def __init__(self, context: dict = None):
"""
Item creation failed
"""
status_code = 500
AppExceptionCase.__init__(self, status_code, context)
class FooGetItem(AppExceptionCase):
def __init__(self, context: dict = None):
"""
Item not found
"""
status_code = 404
AppExceptionCase.__init__(self, status_code, context)
class FooItemRequiresAuth(AppExceptionCase):
def __init__(self, context: dict = None):
"""
Item is not public and requires auth
"""
status_code = 401
AppExceptionCase.__init__(self, status_code, context)