|
| 1 | +#!/usr/bin/env python |
| 2 | +"""SPDX-FileCopyrightText: (c) 2026 by mundialis GmbH & Co. KG. |
| 3 | +
|
| 4 | +SPDX-License-Identifier: GPL-3.0-or-later |
| 5 | +
|
| 6 | +Process Execution class |
| 7 | +""" |
| 8 | + |
| 9 | +__license__ = "GPL-3.0-or-later" |
| 10 | +__author__ = "Carmen Tawalika" |
| 11 | +__copyright__ = "Copyright 2026 mundialis GmbH & Co. KG" |
| 12 | +__maintainer__ = "mundialis GmbH & Co. KG" |
| 13 | + |
| 14 | +from flask import jsonify, make_response |
| 15 | +from flask_restful_swagger_2 import Resource, request, swagger |
| 16 | +from requests.exceptions import ConnectionError # noqa: A004 |
| 17 | + |
| 18 | +from actinia_ogc_api_processes_plugin.apidocs import process_execution |
| 19 | +from actinia_ogc_api_processes_plugin.authentication import require_basic_auth |
| 20 | +from actinia_ogc_api_processes_plugin.core.actinia_common import ( |
| 21 | + safe_parse_actinia_job, |
| 22 | +) |
| 23 | +from actinia_ogc_api_processes_plugin.core.process_execution import ( |
| 24 | + generate_new_joblinks, |
| 25 | + post_process_execution, |
| 26 | +) |
| 27 | +from actinia_ogc_api_processes_plugin.model.response_models import ( |
| 28 | + SimpleStatusCodeResponseModel, |
| 29 | +) |
| 30 | +from actinia_ogc_api_processes_plugin.resources.logging import log |
| 31 | + |
| 32 | + |
| 33 | +class ProcessExecution(Resource): |
| 34 | + """ProcessExecution handling.""" |
| 35 | + |
| 36 | + @require_basic_auth() |
| 37 | + @swagger.doc(process_execution.describe_process_execution_post_docs) |
| 38 | + def post(self, process_id): |
| 39 | + """ProcessExecution post method. |
| 40 | +
|
| 41 | + Execute a process for the given process_id. |
| 42 | + """ |
| 43 | + try: |
| 44 | + postbody = request.json |
| 45 | + resp = post_process_execution(process_id, postbody) |
| 46 | + if resp.status_code == 200: |
| 47 | + job_id, status_info = safe_parse_actinia_job(resp.json()) |
| 48 | + if job_id not in status_info.get("links"): |
| 49 | + status_info["links"] = generate_new_joblinks(job_id) |
| 50 | + return make_response(status_info, 201) |
| 51 | + elif resp.status_code == 401: |
| 52 | + log.error("ERROR: Unauthorized Access") |
| 53 | + log.debug(f"actinia response: {resp.text}") |
| 54 | + res = jsonify( |
| 55 | + SimpleStatusCodeResponseModel( |
| 56 | + status=401, |
| 57 | + message="ERROR: Unauthorized Access", |
| 58 | + ), |
| 59 | + ) |
| 60 | + return make_response(res, 401) |
| 61 | + elif resp.status_code == 404: |
| 62 | + log.error("ERROR: No such process") |
| 63 | + log.debug(f"actinia response: {resp.text}") |
| 64 | + # If operation is executed using an invalid process identifier, |
| 65 | + # the response SHALL be HTTP status code 404. |
| 66 | + # The content of that response SHALL be based upon |
| 67 | + # the OpenAPI 3.0 schema exception.yaml. |
| 68 | + # https://schemas.opengis.net/ogcapi/processes/part1/1.0/openapi/schemas/exception.yaml |
| 69 | + # The type of the exception SHALL be: |
| 70 | + # “http://www.opengis.net/def/exceptions/ogcapi-processes-1/1.0/no-such-process”. |
| 71 | + res = jsonify( |
| 72 | + { |
| 73 | + "type": "http://www.opengis.net/def/exceptions/" |
| 74 | + "ogcapi-processes-1/1.0/no-such-process", |
| 75 | + "title": "No Such Process", |
| 76 | + "status": 404, |
| 77 | + "detail": f"Process '{process_id}' not found", |
| 78 | + }, |
| 79 | + ) |
| 80 | + return make_response(res, 404) |
| 81 | + else: |
| 82 | + log.error("ERROR: Internal Server Error") |
| 83 | + log.debug(f"actinia status code: {resp.status_code}") |
| 84 | + log.debug(f"actinia response: {resp.text}") |
| 85 | + res = jsonify( |
| 86 | + SimpleStatusCodeResponseModel( |
| 87 | + status=500, |
| 88 | + message="ERROR: Internal Server Error", |
| 89 | + ), |
| 90 | + ) |
| 91 | + return make_response(res, 500) |
| 92 | + except ConnectionError as e: |
| 93 | + log.error(f"Connection ERROR: {e}") |
| 94 | + res = jsonify( |
| 95 | + SimpleStatusCodeResponseModel( |
| 96 | + status=503, |
| 97 | + message=f"Connection ERROR: {e}", |
| 98 | + ), |
| 99 | + ) |
| 100 | + return make_response(res, 503) |
0 commit comments