[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,47 @@
from loguru import logger
import inspect
from utils.app_exceptions import AppExceptionCase
class ServiceResult(object):
def __init__(self, arg):
if isinstance(arg, AppExceptionCase):
self.success = False
self.exception_case = arg.exception_case
self.status_code = arg.status_code
else:
self.success = True
self.exception_case = None
self.status_code = None
self.value = arg
def __str__(self):
if self.success:
return "[Success]"
return f'[Exception] "{self.exception_case}"'
def __repr__(self):
if self.success:
return "<ServiceResult Success>"
return f"<ServiceResult AppException {self.exception_case}>"
def __enter__(self):
return self.value
def __exit__(self, *kwargs):
pass
def caller_info() -> str:
info = inspect.getframeinfo(inspect.stack()[2][0])
return f"{info.filename}:{info.function}:{info.lineno}"
def handle_result(result: ServiceResult):
if not result.success:
with result as exception:
logger.error(f"{exception} | caller={caller_info()}")
raise exception
with result as result:
return result