Implement task API with input validation; implement error handling; refactoring
This commit is contained in:
parent
19d264a03b
commit
50aff05614
24 changed files with 612 additions and 131 deletions
|
|
@ -1,6 +1,26 @@
|
|||
from typing import TypeVar
|
||||
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from tofu_api.api.tasks import TaskHandler
|
||||
from tofu_api.common.database import SQLAlchemy
|
||||
from tofu_api.repositories import TaskRepository
|
||||
|
||||
T_Dep_Callable = TypeVar('T_Dep_Callable')
|
||||
|
||||
|
||||
def cache_dependency(func: T_Dep_Callable) -> T_Dep_Callable:
|
||||
"""
|
||||
Decorator to be used in `Dependencies` to cache dependencies inside the Dependencies instance.
|
||||
"""
|
||||
dep_name = func.__name__
|
||||
|
||||
def wrapped_func(self: 'Dependencies'):
|
||||
if dep_name not in self._dependency_cache:
|
||||
self._dependency_cache[dep_name] = func(self)
|
||||
return self._dependency_cache.get(dep_name)
|
||||
|
||||
return wrapped_func
|
||||
|
||||
|
||||
class Dependencies:
|
||||
|
|
@ -15,10 +35,22 @@ class Dependencies:
|
|||
|
||||
# Database dependencies
|
||||
|
||||
@cache_dependency
|
||||
def get_sqlalchemy(self) -> SQLAlchemy:
|
||||
if SQLAlchemy not in self._dependency_cache:
|
||||
self._dependency_cache[SQLAlchemy] = SQLAlchemy()
|
||||
return self._dependency_cache[SQLAlchemy]
|
||||
return SQLAlchemy()
|
||||
|
||||
# No caching necessary here
|
||||
def get_db_session(self) -> Session:
|
||||
return self.get_sqlalchemy().session
|
||||
|
||||
# Repository classes
|
||||
|
||||
@cache_dependency
|
||||
def get_task_repository(self) -> TaskRepository:
|
||||
return TaskRepository(session=self.get_db_session())
|
||||
|
||||
# API Handler classes
|
||||
|
||||
@cache_dependency
|
||||
def get_task_handler(self) -> TaskHandler:
|
||||
return TaskHandler(task_repository=self.get_task_repository())
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue