Files

36 lines
1.5 KiB
Python
Raw Permalink Normal View History

import json
2026-04-19 23:18:20 +02:00
import logging
2026-04-20 10:42:35 +02:00
from fastapi import APIRouter, Depends, Request, status
from fastapi.responses import PlainTextResponse, Response
from pydantic import ValidationError
from sqlalchemy.orm import Session
from app.dependencies import get_db
from app.schemas.location import LocationRecordRequest
from app.services.location import record_location
router = APIRouter(tags=["location"])
2026-04-19 23:18:20 +02:00
logger = logging.getLogger(__name__)
BAD_REQUEST_MESSAGE = "bad request"
@router.post("/location/record")
async def create_location_record(request: Request, db: Session = Depends(get_db)) -> Response:
try:
raw_payload = await request.body()
data = json.loads(raw_payload)
payload = LocationRecordRequest.model_validate(data)
2026-04-19 23:18:20 +02:00
record_location(db, payload)
except json.JSONDecodeError as exc:
2026-04-19 23:18:20 +02:00
logger.warning("Rejected location request due to invalid JSON: %s", exc)
2026-04-20 10:42:35 +02:00
return PlainTextResponse(BAD_REQUEST_MESSAGE, status_code=status.HTTP_400_BAD_REQUEST)
except ValidationError as exc:
2026-04-19 23:18:20 +02:00
logger.warning("Rejected location request due to payload validation failure: %s", exc)
2026-04-20 10:42:35 +02:00
return PlainTextResponse(BAD_REQUEST_MESSAGE, status_code=status.HTTP_400_BAD_REQUEST)
2026-04-19 23:18:20 +02:00
except ValueError as exc:
logger.warning("Rejected location request due to invalid numeric input: %s", exc)
2026-04-20 10:42:35 +02:00
return PlainTextResponse(BAD_REQUEST_MESSAGE, status_code=status.HTTP_400_BAD_REQUEST)
2026-04-20 10:42:35 +02:00
return Response(status_code=status.HTTP_200_OK)