|
| 1 | +from typing import Annotated |
| 2 | +from fastapi import Body, APIRouter, Depends, HTTPException, Response, status |
| 3 | +from loguru import logger |
| 4 | + |
| 5 | +from app.schemas.enum import OutputFormatEnum, ProcessTypeEnum |
| 6 | +from app.schemas.unit_job import ( |
| 7 | + BaseJobRequest, |
| 8 | + ServiceDetails, |
| 9 | +) |
| 10 | +from app.auth import oauth2_scheme |
| 11 | +from app.services.processing import ( |
| 12 | + create_synchronous_job, |
| 13 | +) |
| 14 | + |
| 15 | + |
| 16 | +# from app.auth import get_current_user |
| 17 | + |
| 18 | +router = APIRouter() |
| 19 | + |
| 20 | + |
| 21 | +@router.post( |
| 22 | + "/sync_jobs", |
| 23 | + status_code=status.HTTP_201_CREATED, |
| 24 | + tags=["Unit Jobs"], |
| 25 | + summary="Create a new processing job", |
| 26 | +) |
| 27 | +async def create_sync_job( |
| 28 | + payload: Annotated[ |
| 29 | + BaseJobRequest, |
| 30 | + Body( |
| 31 | + openapi_examples={ |
| 32 | + "openEO Example": { |
| 33 | + "summary": "Valid openEO job request", |
| 34 | + "description": "The following example demonstrates how to create a processing " |
| 35 | + "job using an openEO-based service. This example triggers the " |
| 36 | + "[`variability map`](https://github.com/ESA-APEx/apex_algorithms/blob/main/algo" |
| 37 | + "rithm_catalog/vito/variabilitymap/records/variabilitymap.json) " |
| 38 | + "process using the CDSE openEO Federation. In this case the `endpoint`" |
| 39 | + "represents the URL of the openEO backend and the `application` refers to the " |
| 40 | + "User Defined Process (UDP) that is being executed on the backend.", |
| 41 | + "value": BaseJobRequest( |
| 42 | + label=ProcessTypeEnum.OPENEO, |
| 43 | + title="Example openEO Job", |
| 44 | + service=ServiceDetails( |
| 45 | + endpoint="https://openeofed.dataspace.copernicus.eu", |
| 46 | + application="https://raw.githubusercontent.com/ESA-APEx/apex_algorithms" |
| 47 | + "/32ea3c9a6fa24fe063cb59164cd318cceb7209b0/openeo_udp/variabilitymap/" |
| 48 | + "variabilitymap.json", |
| 49 | + ), |
| 50 | + format=OutputFormatEnum.GEOTIFF, |
| 51 | + parameters={ |
| 52 | + "spatial_extent": { |
| 53 | + "type": "FeatureCollection", |
| 54 | + "features": [ |
| 55 | + { |
| 56 | + "type": "Feature", |
| 57 | + "properties": {}, |
| 58 | + "geometry": { |
| 59 | + "coordinates": [ |
| 60 | + [ |
| 61 | + [ |
| 62 | + 5.170043941798298, |
| 63 | + 51.25050990858725, |
| 64 | + ], |
| 65 | + [ |
| 66 | + 5.171035037521989, |
| 67 | + 51.24865722468999, |
| 68 | + ], |
| 69 | + [ |
| 70 | + 5.178521828188366, |
| 71 | + 51.24674578027137, |
| 72 | + ], |
| 73 | + [ |
| 74 | + 5.179084341977159, |
| 75 | + 51.24984764553983, |
| 76 | + ], |
| 77 | + [ |
| 78 | + 5.170043941798298, |
| 79 | + 51.25050990858725, |
| 80 | + ], |
| 81 | + ] |
| 82 | + ], |
| 83 | + "type": "Polygon", |
| 84 | + }, |
| 85 | + } |
| 86 | + ], |
| 87 | + }, |
| 88 | + "temporal_extent": ["2025-05-01", "2025-05-01"], |
| 89 | + }, |
| 90 | + ).model_dump(), |
| 91 | + } |
| 92 | + }, |
| 93 | + ), |
| 94 | + ], |
| 95 | + token: str = Depends(oauth2_scheme), |
| 96 | +) -> Response: |
| 97 | + """Initiate a synchronous processing job with the provided data and return the result.""" |
| 98 | + try: |
| 99 | + return await create_synchronous_job(token, payload) |
| 100 | + except HTTPException as e: |
| 101 | + raise e |
| 102 | + except Exception as e: |
| 103 | + logger.exception(f"Error creating synchronous job: {e}") |
| 104 | + raise HTTPException( |
| 105 | + status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, |
| 106 | + detail=f"An error occurred while creating the synchronous job: {e}", |
| 107 | + ) |
0 commit comments