Coverage for src/fastoai/__init__.py: 78%

1 import importlib.metadata

2 from contextlib import asynccontextmanager

3

4 from fastapi import FastAPI, status

5 from fastapi.responses import JSONResponse

6 from sqlalchemy.exc import NoResultFound

7

8 try:

9 __version__ = importlib.metadata.version(__name__)

10 except importlib.metadata.PackageNotFoundError:

11 __version__ = "1.0.0"

12

13 from .routers import router

14

15

16@asynccontextmanager

17 async def lifespan(app: FastAPI):

18 yield

19

20

21 app = FastAPI(title="FastOAI", version=__version__, lifespan=lifespan)

22

23

24@app.exception_handler(NoResultFound)

25 async def no_result_found_exception_handler(_, exc: NoResultFound):

26 return JSONResponse(

27 status_code=status.HTTP_404_NOT_FOUND,

28 content={"detail": str(exc)},

29 )

30

31

32 app.include_router(router)