Update app/api/routes.py
Browse files- app/api/routes.py +57 -3
app/api/routes.py
CHANGED
|
@@ -1,8 +1,12 @@
|
|
| 1 |
-
|
|
|
|
| 2 |
|
| 3 |
-
from
|
|
|
|
|
|
|
| 4 |
from app.schemas.categories import CategorizeRequest, CategoryPrediction
|
| 5 |
from app.services.autocategorizer import AutoCategoryService
|
|
|
|
| 6 |
|
| 7 |
router = APIRouter()
|
| 8 |
|
|
@@ -11,5 +15,55 @@ router = APIRouter()
|
|
| 11 |
async def categorize_transaction(
|
| 12 |
payload: CategorizeRequest,
|
| 13 |
service: AutoCategoryService = Depends(get_category_service),
|
|
|
|
| 14 |
) -> CategoryPrediction:
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import time
|
| 2 |
+
from typing import Any
|
| 3 |
|
| 4 |
+
from fastapi import APIRouter, Depends, HTTPException
|
| 5 |
+
|
| 6 |
+
from app.dependencies import get_api_logger, get_category_service
|
| 7 |
from app.schemas.categories import CategorizeRequest, CategoryPrediction
|
| 8 |
from app.services.autocategorizer import AutoCategoryService
|
| 9 |
+
from app.services.api_logger import ApiLogger
|
| 10 |
|
| 11 |
router = APIRouter()
|
| 12 |
|
|
|
|
| 15 |
async def categorize_transaction(
|
| 16 |
payload: CategorizeRequest,
|
| 17 |
service: AutoCategoryService = Depends(get_category_service),
|
| 18 |
+
api_logger: ApiLogger = Depends(get_api_logger),
|
| 19 |
) -> CategoryPrediction:
|
| 20 |
+
started_at = time.monotonic()
|
| 21 |
+
try:
|
| 22 |
+
result = await service.categorize(payload.notes)
|
| 23 |
+
await api_logger.log_categorization(
|
| 24 |
+
name="Auto Expense Categorization",
|
| 25 |
+
status="success",
|
| 26 |
+
response_time=time.monotonic() - started_at,
|
| 27 |
+
user_id=payload.user_id,
|
| 28 |
+
)
|
| 29 |
+
return result
|
| 30 |
+
except HTTPException as exc:
|
| 31 |
+
await api_logger.log_categorization(
|
| 32 |
+
name="Auto Expense Categorization",
|
| 33 |
+
status="fail",
|
| 34 |
+
response_time=time.monotonic() - started_at,
|
| 35 |
+
user_id=payload.user_id,
|
| 36 |
+
error_message=_normalize_error_detail(exc.detail),
|
| 37 |
+
)
|
| 38 |
+
raise
|
| 39 |
+
except Exception as exc:
|
| 40 |
+
await api_logger.log_categorization(
|
| 41 |
+
name="Auto Expense Categorization",
|
| 42 |
+
status="fail",
|
| 43 |
+
response_time=time.monotonic() - started_at,
|
| 44 |
+
user_id=payload.user_id,
|
| 45 |
+
error_message=str(exc),
|
| 46 |
+
)
|
| 47 |
+
raise
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
def _normalize_error_detail(detail: Any) -> str:
|
| 51 |
+
if isinstance(detail, str):
|
| 52 |
+
return detail
|
| 53 |
+
return str(detail)
|
| 54 |
+
|
| 55 |
+
# from fastapi import APIRouter, Depends
|
| 56 |
+
|
| 57 |
+
# from app.dependencies import get_category_service
|
| 58 |
+
# from app.schemas.categories import CategorizeRequest, CategoryPrediction
|
| 59 |
+
# from app.services.autocategorizer import AutoCategoryService
|
| 60 |
+
|
| 61 |
+
# router = APIRouter()
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
# @router.post("/categorize", response_model=CategoryPrediction, summary="Categorize a transaction note")
|
| 65 |
+
# async def categorize_transaction(
|
| 66 |
+
# payload: CategorizeRequest,
|
| 67 |
+
# service: AutoCategoryService = Depends(get_category_service),
|
| 68 |
+
# ) -> CategoryPrediction:
|
| 69 |
+
# return await service.categorize(payload.notes)
|