"""Application Handlers Layer. Requests are redirected to handlers, which are responsible for getting information from the URL and passing it down to the logic layer. The way each layer talks to each other is through Response objects which defines the type status of the data and the data itself. Please note: the Orchard uses the term handlers over views as convention for clarity See: oto.response for more details. """ from typing import List, Optional from fastapi import APIRouter, HTTPException from fastapi.responses import JSONResponse from dataexport.conf import config from dataexport.dtos import Job from dataexport.dtos import JobQuery from dataexport.logic import create_job from dataexport.logic import get_all_jobs api_router = APIRouter() # DATA EXPORT CONTROLLERS @api_router.get(config.HEALTH_CHECK, status_code=200) def health_check() -> JSONResponse: return JSONResponse(content={"status": "ok"}) @api_router.get("/test/{name}", status_code=200) def test_endpoint(name: str, num: Optional[int] = None) -> JSONResponse: return JSONResponse(content={"hello": name, "num": num}) @api_router.get("/job", status_code=200, response_model=List[Job]) def all_jobs(): """Fetch all jobs.""" jobs = get_all_jobs() return jobs @api_router.post("/job", status_code=202, response_model=Job) async def new_job(query: JobQuery): """Create a new job.""" try: job: Job = await create_job(query) except ValueError as err: raise HTTPException(status_code=422, detail=str(err)) if not job or not job.query_string: raise HTTPException(status_code=422, detail="Empty query") return job