Skip to content

Commit a24f440

Browse files
committed
lint
1 parent 21c5953 commit a24f440

4 files changed

Lines changed: 27 additions & 8 deletions

File tree

app/api/stuff.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1+
from collections.abc import Callable
2+
from typing import Annotated, Any
3+
14
from fastapi import APIRouter, Depends, HTTPException, Request, status
2-
from app.services.logging import get_logger
5+
from pydantic import ValidationError, WrapValidator
6+
from rotoger import get_logger
37
from sqlalchemy.exc import SQLAlchemyError
48
from sqlalchemy.ext.asyncio import AsyncSession
59

@@ -22,12 +26,26 @@ async def create_random_stuff(
2226
return {"id": str(random_stuff.id)}
2327

2428

29+
failed_items: list[dict] = [] # Global or pass via context
30+
31+
def catch_invalid(v: Any, handler: Callable[[Any], Any] ) -> Any:
32+
try:
33+
return handler(v)
34+
except ValidationError:
35+
failed_items.append(v) # Intercept here!
36+
return None # Or raise if needed
37+
2538
@router.post("/add_many", status_code=status.HTTP_201_CREATED)
2639
async def create_multi_stuff(
27-
payload: list[StuffSchema], db_session: AsyncSession = Depends(get_db)
40+
payload: list[Annotated[StuffSchema, WrapValidator(catch_invalid)]], db_session: AsyncSession = Depends(get_db)
2841
):
42+
await logger.ainfo(f">>>{failed_items}")
2943
try:
30-
stuff_instances = [Stuff(**stuff.model_dump()) for stuff in payload]
44+
await logger.ainfo(f">>>{failed_items}")
45+
await logger.ainfo(f">>>{payload}")
46+
stuff_instances = [
47+
Stuff(**stuff.model_dump()) for stuff in payload if stuff is not None
48+
]
3149
db_session.add_all(stuff_instances)
3250
await db_session.commit()
3351
except SQLAlchemyError as ex:
@@ -39,6 +57,7 @@ async def create_multi_stuff(
3957
await logger.ainfo(
4058
f"{len(stuff_instances)} Stuff instances inserted into the database."
4159
)
60+
return {"inserted": len(stuff_instances)}
4261
return True
4362

4463

app/main.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from fastapi import Depends, FastAPI, Request
66
from fastapi.responses import HTMLResponse
77
from fastapi.templating import Jinja2Templates
8-
from app.services.logging import get_logger
8+
from rotoger import get_logger
99
from starlette.middleware import Middleware
1010
from starlette.middleware.gzip import GZipMiddleware
1111

@@ -21,7 +21,6 @@
2121
from app.redis import get_redis
2222
from app.services.auth import AuthBearer
2323

24-
# logger = get_logger()
2524
templates = Jinja2Templates(directory=Path(__file__).parent.parent / "templates")
2625

2726

app/schemas/stuff.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
from typing import Any, Callable
1+
from typing import Any
22
from uuid import UUID
33

4-
from pydantic import BaseModel, ConfigDict, Field, WrapValidator, ValidationError
4+
from pydantic import BaseModel, ConfigDict, Field
55

66
config = ConfigDict(from_attributes=True)
77

app/server.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from granian import Granian
22

3+
34
def startup():
45
print("Server starting up...")
56

@@ -16,4 +17,4 @@ def shutdown():
1617
)
1718
server.on_startup(startup)
1819
server.on_shutdown(shutdown)
19-
server.serve_forever()
20+
server.serve_forever()

0 commit comments

Comments
 (0)