feat: add raw cdr ingestion endpoint
This commit is contained in:
0
src/core/api/__init__.py
Normal file
0
src/core/api/__init__.py
Normal file
22
src/core/api/main.py
Normal file
22
src/core/api/main.py
Normal file
@@ -0,0 +1,22 @@
|
||||
from fastapi import FastAPI
|
||||
|
||||
from core.api.routes import router
|
||||
|
||||
|
||||
app = FastAPI(
|
||||
title="Telecom CDR Ingestion API",
|
||||
version="0.1.0",
|
||||
)
|
||||
|
||||
|
||||
app.include_router(
|
||||
router,
|
||||
prefix="/api/v1",
|
||||
)
|
||||
|
||||
|
||||
@app.get("/health")
|
||||
async def health() -> dict[str, str]:
|
||||
return {
|
||||
"status": "ok",
|
||||
}
|
||||
58
src/core/api/routes.py
Normal file
58
src/core/api/routes.py
Normal file
@@ -0,0 +1,58 @@
|
||||
import json
|
||||
|
||||
from fastapi import APIRouter, Request, status
|
||||
from fastapi.responses import JSONResponse
|
||||
|
||||
from core.domain.errors import CDRProcessingError
|
||||
from core.domain.validation import parse_raw_cdr
|
||||
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
@router.post(
|
||||
"/cdr/raw",
|
||||
status_code=status.HTTP_202_ACCEPTED,
|
||||
)
|
||||
async def ingest_raw_cdr(request: Request) -> JSONResponse:
|
||||
try:
|
||||
payload = await request.json()
|
||||
|
||||
except (json.JSONDecodeError, UnicodeDecodeError) as exc:
|
||||
return JSONResponse(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
content={
|
||||
"error": "invalid_json",
|
||||
"detail": str(exc),
|
||||
},
|
||||
)
|
||||
|
||||
if not isinstance(payload, dict):
|
||||
return JSONResponse(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
content={
|
||||
"error": "invalid_payload",
|
||||
"detail": "JSON payload must be an object.",
|
||||
},
|
||||
)
|
||||
|
||||
try:
|
||||
raw_cdr = parse_raw_cdr(payload)
|
||||
|
||||
except CDRProcessingError as exc:
|
||||
return JSONResponse(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
content={
|
||||
"error": exc.reason.value,
|
||||
"detail": exc.detail,
|
||||
"failed_field": exc.failed_field,
|
||||
},
|
||||
)
|
||||
|
||||
return JSONResponse(
|
||||
status_code=status.HTTP_202_ACCEPTED,
|
||||
content={
|
||||
"status": "accepted",
|
||||
"raw_id": raw_cdr.raw_id,
|
||||
},
|
||||
)
|
||||
Reference in New Issue
Block a user