-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.py
More file actions
53 lines (37 loc) · 1.44 KB
/
Copy pathutils.py
File metadata and controls
53 lines (37 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
"""Utility functions for the RCTab API."""
import functools
import logging
from contextlib import contextmanager
from typing import Any, Callable, Coroutine, Generator, List
from asyncpg import Record
from fastapi import HTTPException
from rctab.crud.models import database
logger = logging.getLogger(__name__)
def db_select(func: Callable) -> Callable:
"""Decorate a function that returns a SELECT statement.
Optionally, execute the function and raise a 404 if no data is returned.
"""
@contextmanager
def wrapping_logic(statement: Any) -> Generator:
logger.debug("Function: %s", func.__name__)
del statement
yield
@functools.wraps(func)
def _db_select(
*args: Any, execute: bool = True, raise_404: bool = True, **kwargs: Any
) -> Coroutine:
"""Select and raise a 404 if no data is returned."""
statement = func(*args, **kwargs)
if execute:
async def tmp() -> List[Record]:
with wrapping_logic(statement):
received = await database.fetch_all(statement)
if len(received) < 1 and raise_404:
raise HTTPException(
status_code=404, detail="Could not find the data requested"
)
return received
return tmp()
with wrapping_logic(statement):
return statement
return _db_select