36 lines
1.4 KiB
Python
36 lines
1.4 KiB
Python
import json
|
|
import logging
|
|
|
|
from fastapi import APIRouter, Depends, Request
|
|
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"])
|
|
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)
|
|
record_location(db, payload)
|
|
except json.JSONDecodeError as exc:
|
|
logger.warning("Rejected location request due to invalid JSON: %s", exc)
|
|
return PlainTextResponse(BAD_REQUEST_MESSAGE, status_code=400)
|
|
except ValidationError as exc:
|
|
logger.warning("Rejected location request due to payload validation failure: %s", exc)
|
|
return PlainTextResponse(BAD_REQUEST_MESSAGE, status_code=400)
|
|
except ValueError as exc:
|
|
logger.warning("Rejected location request due to invalid numeric input: %s", exc)
|
|
return PlainTextResponse(BAD_REQUEST_MESSAGE, status_code=400)
|
|
|
|
return Response(status_code=200)
|