karl/app/api/v1.py
2025-10-31 00:01:45 +01:00

40 lines
1.3 KiB
Python

from automapper import mapper, exceptions
from fastapi import APIRouter, Depends
from fastapi_utils.cbv import cbv
from starlette.responses import JSONResponse, Response
from app.api.models import Request
from app.core.core import WebhookProcessor
from app.core.injects import AutowireSupport
from app.events import SimpleEventBus
from app.model.webhook import WebhookEvent
router = APIRouter()
@router.get("/", summary="Main API")
async def root():
return {"message": "Witaj w API v1"}
@cbv(router)
class APIv1:
webhook_service: WebhookProcessor = Depends(AutowireSupport.webhook_processor)
event_bus: SimpleEventBus = Depends(AutowireSupport.event_bus)
logger = __import__('logging').getLogger(__name__)
def __init__(self):
try: # TODO: rejestracja w innym miejscu: klasa jest przeładowywana co żądanie
mapper.add(Request, WebhookEvent)
except exceptions.DuplicatedRegistrationError:
pass
@router.get("/health", summary="Health check")
async def health(self) -> JSONResponse:
# TODO: JSON serialize
return JSONResponse({"status": self.webhook_service.health.healthy})
@router.post("/ci", summary="CI Webhook")
async def ci(self, request: Request):
self.event_bus.publish(mapper.map(request))
return Response(status_code=201)