From 372e6494d6e62bd5baf898284e4c36ce58bbb920 Mon Sep 17 00:00:00 2001 From: floeschau Date: Mon, 8 Jun 2026 12:29:35 +0200 Subject: [PATCH 01/22] Skip token exchange for OGC API if token was issued by iam.terradue.com --- .../implementations/ogc_api_process.py | 27 ++++++++++++++++--- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/app/platforms/implementations/ogc_api_process.py b/app/platforms/implementations/ogc_api_process.py index 546f74f..1a15d08 100644 --- a/app/platforms/implementations/ogc_api_process.py +++ b/app/platforms/implementations/ogc_api_process.py @@ -3,6 +3,8 @@ from app.auth import exchange_token, get_current_user_claims from fastapi import Response from loguru import logger +import jwt +from urllib.parse import urlparse from app.platforms.base import BaseProcessingPlatform from app.platforms.dispatcher import register_platform @@ -160,6 +162,23 @@ async def _create_api_client_instance( return ApiClientWrapper(configuration, **additional_args) + async def _get_token_for_api(self, user_token: str, url: str) -> str: + payload = jwt.decode(user_token, options={"verify_signature": False}) + + # Extract the 'iss' (issuer) claim safely + issuer = payload.get("iss") + parsed_uri = urlparse(issuer) + + # Return token if it is a Terradue/Geohazards-TEP token + # (issued by iam.terradue.com) + if parsed_uri.netloc == "iam.terradue.com": + logger.debug(f"Skipping token exchange (token issued by {parsed_uri.netloc})") + return user_token + + # Otherwise perform token exchange (using APEx token as input) + return await exchange_token(user_token, url) + + async def execute_job( self, user_token: str, @@ -174,7 +193,7 @@ async def execute_job( # Exchanging token logger.debug("Exchanging user token for OGC API Process execution...") - exchanged_token = await exchange_token( + exchanged_token = await self._get_token_for_api( user_token=user_token, url=details.endpoint ) @@ -298,7 +317,7 @@ async def get_job_status( logger.debug(f"Fetching job status for OGC API job with ID {job_id}") logger.debug("Exchanging user token for OGC API Process execution...") - exchanged_token = await exchange_token( + exchanged_token = await self._get_token_for_api( user_token=user_token, url=details.endpoint ) @@ -317,7 +336,7 @@ async def get_job_results( logger.debug(f"Fetching job result for opfenEO job with ID {job_id}") logger.debug("Exchanging user token for OGC API Process execution...") - exchanged_token = await exchange_token( + exchanged_token = await self._get_token_for_api( user_token=user_token, url=details.endpoint ) @@ -429,7 +448,7 @@ async def get_service_parameters( ) logger.debug("Exchanging user token for OGC API Process execution...") - exchanged_token = await exchange_token( + exchanged_token = await self._get_token_for_api( user_token=user_token, url=details.endpoint ) From ba9966c037dfd8a65057c545c5f9cac649766a7f Mon Sep 17 00:00:00 2001 From: floeschau Date: Wed, 24 Jun 2026 08:43:05 +0200 Subject: [PATCH 02/22] Changes for test deployment (Terradue) --- .../833e4a41c2ad_generation_of_tables.py | 6 +- app/auth.py | 1 + app/database/models/processing_job.py | 8 +-- docker-compose-t2.yml | 60 +++++++++++++++++++ env-t2 | 18 ++++++ 5 files changed, 86 insertions(+), 7 deletions(-) create mode 100644 docker-compose-t2.yml create mode 100644 env-t2 diff --git a/alembic/versions/833e4a41c2ad_generation_of_tables.py b/alembic/versions/833e4a41c2ad_generation_of_tables.py index 189823e..72f521f 100644 --- a/alembic/versions/833e4a41c2ad_generation_of_tables.py +++ b/alembic/versions/833e4a41c2ad_generation_of_tables.py @@ -27,7 +27,7 @@ def upgrade() -> None: sa.Column('label', sa.Enum('OPENEO', 'OGC_API_PROCESS', name='processtypeenum'), nullable=False), sa.Column('status', sa.Enum('CREATED', 'QUEUED', 'RUNNING', 'FINISHED', 'CANCELED', 'FAILED', 'UNKNOWN', name='processingstatusenum'), nullable=False), sa.Column('user_id', sa.String(length=255), nullable=False), - sa.Column('service', mysql.LONGTEXT(), nullable=False), + sa.Column('service', sa.Text, nullable=False), sa.Column('created', sa.DateTime(), nullable=False), sa.Column('updated', sa.DateTime(), nullable=False), sa.PrimaryKeyConstraint('id') @@ -46,8 +46,8 @@ def upgrade() -> None: sa.Column('status', sa.Enum('CREATED', 'QUEUED', 'RUNNING', 'FINISHED', 'CANCELED', 'FAILED', 'UNKNOWN', name='processingstatusenum'), nullable=False), sa.Column('user_id', sa.String(length=255), nullable=False), sa.Column('platform_job_id', sa.String(length=255), nullable=True), - sa.Column('parameters', mysql.LONGTEXT(), nullable=False), - sa.Column('service', mysql.LONGTEXT(), nullable=False), + sa.Column('parameters', sa.Text, nullable=False), + sa.Column('service', sa.Text, nullable=False), sa.Column('created', sa.DateTime(), nullable=False), sa.Column('updated', sa.DateTime(), nullable=False), sa.Column('upscaling_task_id', sa.Integer(), nullable=True), diff --git a/app/auth.py b/app/auth.py index 4dd88d6..c0ddeb7 100644 --- a/app/auth.py +++ b/app/auth.py @@ -39,6 +39,7 @@ def _decode_token(token: str): signing_key, algorithms=[ALGORITHM], issuer=KEYCLOAK_BASE_URL, + options={"verify_aud": False} ) return payload except Exception: diff --git a/app/database/models/processing_job.py b/app/database/models/processing_job.py index 3611b8e..20d699d 100644 --- a/app/database/models/processing_job.py +++ b/app/database/models/processing_job.py @@ -3,8 +3,8 @@ from typing import List, Optional from loguru import logger -from sqlalchemy import DateTime, Enum, ForeignKey, Integer, String -from sqlalchemy.dialects.mysql import LONGTEXT +from sqlalchemy import DateTime, Enum, ForeignKey, Integer, String, Text +#from sqlalchemy.dialects.mysql import LONGTEXT from sqlalchemy.orm import Mapped, Session, mapped_column from app.database.db import Base @@ -26,8 +26,8 @@ class ProcessingJobRecord(Base): ) user_id: Mapped[str] = mapped_column(String(255), index=True) platform_job_id: Mapped[Optional[str]] = mapped_column(String(255), index=True) - parameters: Mapped[str] = mapped_column(LONGTEXT()) - service: Mapped[str] = mapped_column(LONGTEXT()) + parameters: Mapped[str] = mapped_column(Text()) + service: Mapped[str] = mapped_column(Text()) created: Mapped[datetime.datetime] = mapped_column( DateTime, default=datetime.datetime.utcnow, index=True ) diff --git a/docker-compose-t2.yml b/docker-compose-t2.yml new file mode 100644 index 0000000..f18a3b9 --- /dev/null +++ b/docker-compose-t2.yml @@ -0,0 +1,60 @@ +version: "3.9" + +volumes: + apex-db-data: + +services: + db: + image: postgres:15 + restart: unless-stopped + environment: + POSTGRES_USER: apex_user + POSTGRES_PASSWORD: apex_pass + POSTGRES_DB: apex_db + ports: + - "5432:5432" + volumes: + - apex-db-data:/var/lib/postgresql/data + healthcheck: + test: ["CMD-SHELL", "pg_isready -U apex_user -d apex_db"] + interval: 5s + timeout: 5s + retries: 10 + + app: + build: + context: . + dockerfile: dockerfile + image: apex-dispatch-api:latest + restart: unless-stopped + environment: + APP_ENV: development + KEYCLOAK_HOST: ${KEYCLOAK_HOST} + KEYCLOAK_REALM: ${KEYCLOAK_REALM} + KEYCLOAK_CLIENT_ID: ${KEYCLOAK_CLIENT_ID} + KEYCLOAK_CLIENT_SECRET: ${KEYCLOAK_CLIENT_SECRET} + DATABASE_URL: postgresql+psycopg2://apex_user:apex_pass@db:5432/apex_db + BACKENDS: '{"https://processing.geohazards-tep.eu": {"auth_method": "USER_CREDENTIALS", "token_provider": "gep", "token_prefix": ""}}' + depends_on: + db: + condition: service_healthy + ports: + - "${APP_PORT:-8000}:${APP_PORT:-8000}" + healthcheck: + # adjust path if your health endpoint differs + test: ["CMD-SHELL", "curl -f http://localhost:${APP_PORT:-8000}/health || exit 1"] + interval: 5s + timeout: 3s + retries: 12 + + migrate: + image: apex-dispatch-api:latest + environment: + DATABASE_URL: postgresql+psycopg2://apex_user:apex_pass@db:5432/apex_db + depends_on: + db: + condition: service_healthy + # run the migration only after the app is reachable, then exit + entrypoint: ["/bin/sh", "-c"] + command: > + "alembic upgrade head" diff --git a/env-t2 b/env-t2 new file mode 100644 index 0000000..e71ae3a --- /dev/null +++ b/env-t2 @@ -0,0 +1,18 @@ +# General Settings +APP_NAME="APEx Dispatch API" +APP_DESCRIPTION="APEx Dispatch Service API to run jobs and upscaling tasks" +APP_ENV=development + +CORS_ALLOWED_ORIGINS=http://localhost:5173 + +# Database Settings +DATABASE_URL=sqlite:///test.db + +# Keycloak Settings +KEYCLOAK_HOST="https://iam.terradue.com" +KEYCLOAK_REALM=master +KEYCLOAK_CLIENT_ID=apex-client-id +KEYCLOAK_CLIENT_SECRET=apex-client-secret + + +BACKENDS='"https://openeo.backend2.com": {"auth_method": "USER_CREDENTIALS", "token_provider": "backend", "token_prefix": "oidc/backend"}}' From ec9010775f99c59c1c0f61817344e3f13f680bed Mon Sep 17 00:00:00 2001 From: floeschau Date: Wed, 24 Jun 2026 08:52:02 +0200 Subject: [PATCH 03/22] Changes for test deployment (Terradue) (2) --- env-t2 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/env-t2 b/env-t2 index e71ae3a..ebc284c 100644 --- a/env-t2 +++ b/env-t2 @@ -6,7 +6,7 @@ APP_ENV=development CORS_ALLOWED_ORIGINS=http://localhost:5173 # Database Settings -DATABASE_URL=sqlite:///test.db +DATABASE_URL=postgresql+psycopg2://apex_user:apex_pass@db:5432/apex_db # Keycloak Settings KEYCLOAK_HOST="https://iam.terradue.com" From 88ead06be671a8dd50ab646848dc29f2012dd23f Mon Sep 17 00:00:00 2001 From: floeschau Date: Wed, 24 Jun 2026 08:55:34 +0200 Subject: [PATCH 04/22] Changes for test deployment (Terradue) (3) --- docker-compose-t2.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docker-compose-t2.yml b/docker-compose-t2.yml index f18a3b9..4626a7d 100644 --- a/docker-compose-t2.yml +++ b/docker-compose-t2.yml @@ -33,7 +33,7 @@ services: KEYCLOAK_REALM: ${KEYCLOAK_REALM} KEYCLOAK_CLIENT_ID: ${KEYCLOAK_CLIENT_ID} KEYCLOAK_CLIENT_SECRET: ${KEYCLOAK_CLIENT_SECRET} - DATABASE_URL: postgresql+psycopg2://apex_user:apex_pass@db:5432/apex_db + DATABASE_URL: ${DATABASE_URL} BACKENDS: '{"https://processing.geohazards-tep.eu": {"auth_method": "USER_CREDENTIALS", "token_provider": "gep", "token_prefix": ""}}' depends_on: db: @@ -50,7 +50,7 @@ services: migrate: image: apex-dispatch-api:latest environment: - DATABASE_URL: postgresql+psycopg2://apex_user:apex_pass@db:5432/apex_db + DATABASE_URL: ${DATABASE_URL} depends_on: db: condition: service_healthy From a26e5962db352cebdc751714ba6b9fdb44e62859 Mon Sep 17 00:00:00 2001 From: floeschau Date: Tue, 7 Jul 2026 15:40:48 +0200 Subject: [PATCH 05/22] Enabled Cross Domain Federation/Trusted Token Delegation --- app/auth.py | 115 ++++++++++++++++++++++++++++-------------- app/config/schemas.py | 8 +++ 2 files changed, 86 insertions(+), 37 deletions(-) diff --git a/app/auth.py b/app/auth.py index c0ddeb7..14e4df7 100644 --- a/app/auth.py +++ b/app/auth.py @@ -8,6 +8,7 @@ from app.error import AuthException, DispatcherException from app.schemas.websockets import WSStatusMessage +from app.config.schemas import BackendAuthConfig from .config.settings import settings @@ -33,13 +34,19 @@ def _decode_token(token: str): try: logger.debug(f"Decoding token for user authentication: {token} with " f"issuer {KEYCLOAK_BASE_URL}") - signing_key = jwks_client.get_signing_key_from_jwt(token).key + try: + signing_key = jwks_client.get_signing_key_from_jwt(token).key + except Exception as e: + raise + logger.warning(f"Signing key error: {str(e)}") + signing_key = "" + logger.warning("Before decode") payload = jwt.decode( token, signing_key, algorithms=[ALGORITHM], issuer=KEYCLOAK_BASE_URL, - options={"verify_aud": False} + options={"verify_aud": False}, ) return payload except Exception: @@ -102,64 +109,93 @@ async def exchange_token(user_token: str, url: str) -> str: :return: The bearer token as a string. """ - provider = settings.backend_auth_config[url].token_provider - token_prefix = settings.backend_auth_config[url].token_prefix + backend_idp = settings.backend_auth_config[url] - if not provider: + if not backend_idp.token_provider: raise ValueError( f"Backend '{url}' must define 'token_provider'" ) platform_token = await _exchange_token_for_provider( - initial_token=user_token, provider=provider + initial_token=user_token, + backend_idp=backend_idp ) return ( - f"{token_prefix}/{platform_token['access_token']}" - if token_prefix + f"{backend_idp.token_prefix}/{platform_token['access_token']}" + if backend_idp.token_prefix else platform_token["access_token"] ) async def _exchange_token_for_provider( - initial_token: str, provider: str + initial_token: str, backend_idp: BackendAuthConfig ) -> Dict[str, Any]: """ Exchange a Keycloak access token for a token/audience targeted at `provider` using the Keycloak Token Exchange (grant_type=urn:ietf:params:oauth:grant-type:token-exchange). :param initial_token: token obtained from the client (Bearer token) - :param provider: target provider name or client_id. + :param backend_idp: target IDP. :return: The token response (dict) on success. :raise: Raises AuthException with an appropriate status and message on error. """ - token_url = f"{KEYCLOAK_BASE_URL}/protocol/openid-connect/token" - - # Check if the necessary settings are in place - if not settings.keycloak_client_id: - raise AuthException( - http_status=status.HTTP_500_INTERNAL_SERVER_ERROR, - message="Token exchange not configured on the server (missing client credentials).", - ) - - payload = { - "grant_type": "urn:ietf:params:oauth:grant-type:token-exchange", - "client_id": settings.keycloak_client_id, - "client_secret": settings.keycloak_client_secret, - "subject_token": initial_token, - "requested_issuer": provider, - } - + if backend_idp.token_url: + # Cross-Domain Federation/Trusted Token Delegation + # Payload will be sent to the backend IDP and receive a valid access token from it + logger.info(f"Using Cross-Domain Federation/Trusted Token Delegation with IDP {backend_idp.token_url}") + if not backend_idp.client_id: + raise AuthException( + http_status=status.HTTP_500_INTERNAL_SERVER_ERROR, + message="Token exchange not configured on the server (missing client credentials).", + ) + token_url = backend_idp.token_url + payload = { + "client_id": backend_idp.client_id, + "client_secret": backend_idp.client_secret, + "grant_type": "urn:ietf:params:oauth:grant-type:token-exchange", + "subject_token": initial_token, + "subject_token_type": "urn:ietf:params:oauth:token-type:access_token", + "subject_issuer": backend_idp.subject_issuer, + "audience": backend_idp.audience, + "requested_token_type": "urn:ietf:params:oauth:token-type:access_token", + "scope": "openid profile email" + } + + else: + # Internal-to-External Token Exchange + # Payload will be sent to the backend IDP and receive a valid access token from it + logger.info(f"Using Internal-to-External Token Exchange with IDP {backend_idp.token_provider}") + + token_url = f"{KEYCLOAK_BASE_URL}/protocol/openid-connect/token" + #token_url = "https://iam.terradue.com/realms/master/protocol/openid-connect/token" + + # Check if the necessary settings are in place + if not settings.keycloak_client_id: + raise AuthException( + http_status=status.HTTP_500_INTERNAL_SERVER_ERROR, + message="Token exchange not configured on the server (missing client credentials).", + ) + payload = { + "grant_type": "urn:ietf:params:oauth:grant-type:token-exchange", + "client_id": settings.keycloak_client_id, + "client_secret": settings.keycloak_client_secret, + "subject_token": initial_token, + "requested_issuer": backend_idp.token_provider, + } + + provider_str = backend_idp.token_provider or backend_idp.token_url try: async with httpx.AsyncClient(timeout=10.0) as client: resp = await client.post(token_url, data=payload) except httpx.RequestError as exc: - logger.error(f"Token exchange network error for provider={provider}: {exc}") + logger.error( + f"Token exchange network error for provider={provider_str}: {exc}") raise AuthException( http_status=status.HTTP_502_BAD_GATEWAY, message=( - f"Could not authenticate with {provider}. Please contact APEx support or reach out " + f"Could not authenticate with {provider_str}. Please contact APEx support or reach out " "through the APEx User Forum." ), ) @@ -174,7 +210,7 @@ async def _exchange_token_for_provider( raise AuthException( http_status=status.HTTP_502_BAD_GATEWAY, message=( - f"Could not authenticate with {provider}. Please contact APEx support or reach out " + f"Could not authenticate with {provider_str}. Please contact APEx support or reach out " "through the APEx User Forum." ), ) @@ -183,7 +219,7 @@ async def _exchange_token_for_provider( # Keycloak returns error and error_description fields for token errors err = body.get("error_description") or body.get("error") or resp.text logger.error( - f"Token exchange failed for provider={provider}, status={resp.status_code}, error={err}" + f"Token exchange failed for provider={provider_str}, status={resp.status_code}, error={err}" ) # Map common upstream statuses to meaningful client statuses client_status = ( @@ -192,15 +228,20 @@ async def _exchange_token_for_provider( else status.HTTP_502_BAD_GATEWAY ) - raise AuthException( - http_status=client_status, - message=( - f"Please link your account with {provider} in your " + if backend_idp.token_provider: + message = ( + f"Please link your account with {provider_str} in your " f"Account Dashboard" if body.get("error", "") == "not_linked" - else f"Could not authenticate with {provider}: {err}" - ), + else f"Could not authenticate with {provider_str}: {err}" + ) + else: + message = f"Could not authenticate with {provider_str}: {err}" + + raise AuthException( + http_status=client_status, + message=message ) # Successful exchange, return token response (access_token, expires_in, etc.) diff --git a/app/config/schemas.py b/app/config/schemas.py index d19bd11..f64d470 100644 --- a/app/config/schemas.py +++ b/app/config/schemas.py @@ -13,3 +13,11 @@ class BackendAuthConfig(BaseModel): client_credentials: Optional[str] = None token_provider: Optional[str] = None token_prefix: Optional[str] = None + + # Values to be set in case of + # Cross-Domain Federation/Trusted Token Delegation + token_url: Optional[str] = None + client_id: Optional[str] = None + client_secret: Optional[str] = None + subject_issuer: Optional[str] = None + audience: Optional[str] = None From 8b2686a47e8603584d0463384f76c4504e95d3ef Mon Sep 17 00:00:00 2001 From: floeschau Date: Wed, 21 Oct 2026 16:58:31 +0200 Subject: [PATCH 06/22] feat: add FeatureCollection to result --- .../implementations/ogc_api_process.py | 133 ++++++++++-------- 1 file changed, 75 insertions(+), 58 deletions(-) diff --git a/app/platforms/implementations/ogc_api_process.py b/app/platforms/implementations/ogc_api_process.py index 913842a..2d3b2a0 100644 --- a/app/platforms/implementations/ogc_api_process.py +++ b/app/platforms/implementations/ogc_api_process.py @@ -16,6 +16,8 @@ from app.schemas.unit_job import ServiceDetails from httpx import get as http_get, Response as HTTPXResponse from stac_pydantic.collection import Collection, Extent, SpatialExtent, TimeInterval +from stac_pydantic.item_collection import ItemCollection +from stac_pydantic.item import Item from stac_pydantic.links import Links from stac_pydantic.version import STAC_VERSION from ogc_api_processes_client.api_client_wrapper import ApiClientWrapper @@ -330,8 +332,8 @@ def _extract_download_link_from_asset(self, asset: dict) -> str | None: str | None: The download link if available, otherwise None. """ refs = [ - asset.get("href"), - asset.get("alternate", {}).get("https", {}).get("href"), + asset.href, + asset.to_dict().get("alternate", {}).get("https", {}).get("href"), ] for href in refs: if href and href.startswith("https://"): @@ -418,7 +420,7 @@ def _update_assets_hrefs(self, assets: dict, user_token: str) -> dict: "original HTTPS href." ) signed_url = href - updated_asset["href"] = signed_url + updated_asset.href = signed_url updated_assets[asset_name] = updated_asset return updated_assets @@ -426,21 +428,18 @@ def _update_assets_hrefs(self, assets: dict, user_token: str) -> dict: def _build_collection_from_features( self, features: list, - assets: dict, result_name: str, user_token: str, details: ServiceDetails, internal_job_id: str, ) -> Collection: """ - Build a STAC Collection from a list of GeoJSON features and their - aggregated assets. The spatial extent is derived from the feature - bounding boxes and the temporal extent from the feature datetime - properties. + Build a STAC Collection from a list of GeoJSON features. + The spatial extent is derived from the feature bounding boxes + and the temporal extent from the feature datetime properties. Args: features: GeoJSON feature list. - assets: Aggregated asset dict collected from those features. result_name: Identifier used as the collection ID. user_token: Token used to sign asset hrefs. details: Service details containing namespace and application information. @@ -454,7 +453,7 @@ def _build_collection_from_features( max_x, max_y = float("-inf"), float("-inf") found_bbox = False for feature in features: - bbox = feature.get("bbox") + bbox = feature.bbox if isinstance(bbox, (list, tuple)) and len(bbox) >= 4: min_x = min(min_x, float(bbox[0])) min_y = min(min_y, float(bbox[1])) @@ -468,23 +467,19 @@ def _build_collection_from_features( # Temporal extent — min/max of all datetime-like properties datetimes: list[str] = [] for feature in features: - props = feature.get("properties") or {} - for dt_key in ("datetime", "start_datetime", "end_datetime"): - dt_val = props.get(dt_key) + props = feature.properties + for dt_val in (props.datetime, props.start_datetime, props.end_datetime): if isinstance(dt_val, str): datetimes.append(dt_val) temporal_interval: list[list] = ( [[min(datetimes), max(datetimes)]] if datetimes else [[None, None]] ) - updated_assets = self._update_assets_hrefs(assets, user_token) - logger.debug( f"Building STAC Collection '{result_name}' from {len(features)} feature(s) " - f"with {len(updated_assets)} asset(s)." ) - return Collection( + collection = Collection( id=f"{details.namespace}-{internal_job_id}", stac_version=STAC_VERSION, title=f"Results for {details.application}", @@ -499,10 +494,21 @@ def _build_collection_from_features( spatial=SpatialExtent(bbox=[spatial_bbox]), temporal=TimeInterval(interval=temporal_interval), ), - assets=updated_assets, + assets={}, ) + collection.features = features + + return collection + - def _extract_assets_from_feature_collection( + def _remove_stac_links(self, links: Links): + stac_links = [link for link in links.root if link.rel in ["self", "item", "items", "collection", "parent", "root"] or link.rel.endswith("queryables")] + for link in stac_links: + links.root.remove(link) + return links.root + + + def _process_feature_collection( self, feature_collection: dict, *, @@ -511,50 +517,60 @@ def _extract_assets_from_feature_collection( details: ServiceDetails, internal_job_id: str, ) -> Collection: - assets: dict = {} - features = feature_collection.get("features", []) logger.debug(f"Feature collection: {json.dumps(feature_collection, indent=2)}") + + features_dict = feature_collection.get("features", []) + features = [Item.model_validate(f) for f in features_dict] + logger.debug(f"Feature collection: {json.dumps(feature_collection, indent=2)}") + + collection = None + for feature in features: - feature_assets = feature.get("assets") - if isinstance(feature_assets, dict): - assets.update(feature_assets) - continue + collection_link = next((link for link in feature.links.root if link.rel == "collection"), None) + self._remove_stac_links(feature.links) + feature.links = Links(self._remove_stac_links(feature.links)) + feature.assets = self._update_assets_hrefs(feature.assets, user_token) # Some providers expose assets through an item link # instead of inlining them in the feature. - for link in feature.get("links", []): - if "collection" == link.get("rel") and link.get("href"): - collection_link: str = link.get("href") - logger.debug( - f"GeoJSON FeatureCollection results: '{result_name}' " - f"points to a valid collection URL: {collection_link}" - ) + if collection is None and collection_link is not None: + logger.debug( + f"GeoJSON FeatureCollection results: '{result_name}' " + f"points to a valid collection URL: {collection_link.href}" + ) + + response: HTTPXResponse = http_get( + collection_link.href, + follow_redirects=True, + headers={"Authorization": f"Bearer {user_token}"}, + ) + response.raise_for_status() + collection_data = response.json() + collection = Collection.model_validate(collection_data) + collection.assets = self._update_assets_hrefs( + collection.assets, user_token + ) + logger.debug( + f"Extracted collection '{collection.id}' " + f"with assets: {list((collection.assets or {}).keys())}" + ) + self._remove_stac_links(collection.links) + collection.links = Links(self._remove_stac_links(collection.links)) + + + if collection is None: + collection = self._build_collection_from_features( + features, + result_name, + user_token, + details, + internal_job_id + ) + else: + collection.features = features + + return collection - response: HTTPXResponse = http_get( - collection_link, - follow_redirects=True, - headers={"Authorization": f"Bearer {user_token}"}, - ) - response.raise_for_status() - collection_data = response.json() - collection_data["assets"] = self._update_assets_hrefs( - collection_data.get("assets", {}), user_token - ) - collection = Collection.model_validate(collection_data) - logger.debug( - f"Extracted collection '{collection.id}' " - f"with assets: {list((collection.assets or {}).keys())}" - ) - return collection - - return self._build_collection_from_features( - features, - assets, - result_name, - user_token, - details, - internal_job_id - ) async def get_job_status( self, user_token: str, job_id: str, details: ServiceDetails @@ -575,6 +591,7 @@ async def get_job_status( status_info: StatusInfo = api_client.get_status(job_id=internal_job_id) return self._map_ogcapi_status(status_info.status) + async def get_job_results( self, user_token: str, job_id: str, details: ServiceDetails ) -> Collection: @@ -643,7 +660,7 @@ async def get_job_results( feature_collection = ( qualified_value.value.oneof_schema_2_validator or {} ) - return self._extract_assets_from_feature_collection( + return self._process_feature_collection( feature_collection, result_name=result_name, user_token=exchanged_token or user_token, From 80d1e274d61cf0a44331cd02632bfcd492352bac Mon Sep 17 00:00:00 2001 From: floeschau Date: Thu, 22 Oct 2026 17:33:38 +0200 Subject: [PATCH 07/22] feat: enable ItemCollection als results response payload --- app/platforms/base.py | 5 +- .../implementations/ogc_api_process.py | 49 ++++++------------- app/routers/unit_jobs.py | 5 +- 3 files changed, 21 insertions(+), 38 deletions(-) diff --git a/app/platforms/base.py b/app/platforms/base.py index 5e0d664..546e3b4 100644 --- a/app/platforms/base.py +++ b/app/platforms/base.py @@ -7,7 +7,8 @@ from app.schemas.parameters import Parameter from app.schemas.unit_job import ServiceDetails -from stac_pydantic import Collection +from stac_pydantic.collection import Collection +from stac_pydantic.item_collection import ItemCollection class BaseProcessingPlatform(ABC): @@ -75,7 +76,7 @@ async def get_job_status( @abstractmethod async def get_job_results( self, user_token: str, job_id: str, details: ServiceDetails - ) -> Collection: + ) -> Collection | ItemCollection: """ Retrieve the job results of a processing job that is running on the platform. diff --git a/app/platforms/implementations/ogc_api_process.py b/app/platforms/implementations/ogc_api_process.py index 2d3b2a0..6c086b5 100644 --- a/app/platforms/implementations/ogc_api_process.py +++ b/app/platforms/implementations/ogc_api_process.py @@ -425,14 +425,14 @@ def _update_assets_hrefs(self, assets: dict, user_token: str) -> dict: return updated_assets - def _build_collection_from_features( + def _build_item_collection_from_features( self, features: list, result_name: str, user_token: str, details: ServiceDetails, internal_job_id: str, - ) -> Collection: + ) -> ItemCollection: """ Build a STAC Collection from a list of GeoJSON features. The spatial extent is derived from the feature bounding boxes @@ -479,26 +479,10 @@ def _build_collection_from_features( f"Building STAC Collection '{result_name}' from {len(features)} feature(s) " ) - collection = Collection( - id=f"{details.namespace}-{internal_job_id}", - stac_version=STAC_VERSION, - title=f"Results for {details.application}", - description=( - f"OGC API process result items for job '{internal_job_id}' " - f"of application '{details.application}'." - ), - type="Collection", - license="proprietary", - links=Links([]), - extent=Extent( - spatial=SpatialExtent(bbox=[spatial_bbox]), - temporal=TimeInterval(interval=temporal_interval), - ), - assets={}, + return ItemCollection( + bbox=[spatial_bbox], + features=features ) - collection.features = features - - return collection def _remove_stac_links(self, links: Links): @@ -516,7 +500,7 @@ def _process_feature_collection( user_token: str, details: ServiceDetails, internal_job_id: str, - ) -> Collection: + ) -> ItemCollection: logger.debug(f"Feature collection: {json.dumps(feature_collection, indent=2)}") features_dict = feature_collection.get("features", []) @@ -558,18 +542,15 @@ def _process_feature_collection( collection.links = Links(self._remove_stac_links(collection.links)) - if collection is None: - collection = self._build_collection_from_features( - features, - result_name, - user_token, - details, - internal_job_id - ) - else: - collection.features = features + item_collection = self._build_item_collection_from_features( + features, + result_name, + user_token, + details, + internal_job_id + ) - return collection + return item_collection async def get_job_status( @@ -594,7 +575,7 @@ async def get_job_status( async def get_job_results( self, user_token: str, job_id: str, details: ServiceDetails - ) -> Collection: + ) -> Collection | ItemCollection: logger.debug(f"Fetching job result for opfenEO job with ID {job_id}") logger.debug("Exchanging user token for OGC API Process execution...") diff --git a/app/routers/unit_jobs.py b/app/routers/unit_jobs.py index 5770746..884793e 100644 --- a/app/routers/unit_jobs.py +++ b/app/routers/unit_jobs.py @@ -26,7 +26,8 @@ get_processing_job_results, ) -from stac_pydantic import Collection +from stac_pydantic.collection import Collection +from stac_pydantic.item_collection import ItemCollection # from app.auth import get_current_user @@ -212,7 +213,7 @@ async def get_job( ) async def get_job_results( job_id: int, db: Session = Depends(get_db), token: str = Depends(oauth2_scheme) -) -> Collection | None: +) -> Collection | ItemCollection | None: try: result = await get_processing_job_results(token, db, job_id) if not result: From 9537ef943ae0aa553a96bd31cf7f7da8e1896381 Mon Sep 17 00:00:00 2001 From: floeschau Date: Fri, 23 Oct 2026 17:58:48 +0200 Subject: [PATCH 08/22] feat: enable ItemCollection as results response payload (2) --- .../implementations/ogc_api_process.py | 91 ++++++++++--------- 1 file changed, 47 insertions(+), 44 deletions(-) diff --git a/app/platforms/implementations/ogc_api_process.py b/app/platforms/implementations/ogc_api_process.py index 6c086b5..dbe1af2 100644 --- a/app/platforms/implementations/ogc_api_process.py +++ b/app/platforms/implementations/ogc_api_process.py @@ -1,25 +1,13 @@ import json import re -from typing import List +from urllib.parse import urlparse +import jwt import requests -from app.auth import exchange_token, get_current_user_claims from fastapi import Response +from httpx import Response as HTTPXResponse +from httpx import get as http_get from loguru import logger -import jwt -from urllib.parse import urlparse - -from app.platforms.base import BaseProcessingPlatform -from app.platforms.dispatcher import register_platform -from app.schemas.enum import OutputFormatEnum, ProcessTypeEnum, ProcessingStatusEnum -from app.schemas.parameters import ParamTypeEnum, Parameter -from app.schemas.unit_job import ServiceDetails -from httpx import get as http_get, Response as HTTPXResponse -from stac_pydantic.collection import Collection, Extent, SpatialExtent, TimeInterval -from stac_pydantic.item_collection import ItemCollection -from stac_pydantic.item import Item -from stac_pydantic.links import Links -from stac_pydantic.version import STAC_VERSION from ogc_api_processes_client.api_client_wrapper import ApiClientWrapper from ogc_api_processes_client.configuration import Configuration from ogc_api_processes_client.models.inline_or_ref_data import InlineOrRefData @@ -28,7 +16,18 @@ from ogc_api_processes_client.models.qualified_input_value import QualifiedInputValue from ogc_api_processes_client.models.status_code import StatusCode from ogc_api_processes_client.models.status_info import StatusInfo -from typing import Dict +from stac_pydantic.collection import Collection, Extent, SpatialExtent, TimeInterval +from stac_pydantic.item import Item +from stac_pydantic.item_collection import ItemCollection +from stac_pydantic.links import Links +from stac_pydantic.version import STAC_VERSION + +from app.auth import exchange_token, get_current_user_claims +from app.platforms.base import BaseProcessingPlatform +from app.platforms.dispatcher import register_platform +from app.schemas.enum import OutputFormatEnum, ProcessingStatusEnum, ProcessTypeEnum +from app.schemas.parameters import Parameter, ParamTypeEnum +from app.schemas.unit_job import ServiceDetails STAC_COLLECTION_SCHEMA = ( "https://schemas.stacspec.org/v1.0.0/collection-spec/json-schema/collection.json" @@ -179,13 +178,14 @@ async def _get_token_for_api(self, user_token: str, url: str) -> str: # Return token if it is a Terradue/Geohazards-TEP token # (issued by iam.terradue.com) if parsed_uri.netloc == "iam.terradue.com": - logger.debug(f"Skipping token exchange (token issued by {parsed_uri.netloc})") + logger.debug( + f"Skipping token exchange (token issued by {parsed_uri.netloc})" + ) return user_token # Otherwise perform token exchange (using APEx token as input) return await exchange_token(user_token, url) - async def execute_job( self, user_token: str, @@ -471,27 +471,36 @@ def _build_item_collection_from_features( for dt_val in (props.datetime, props.start_datetime, props.end_datetime): if isinstance(dt_val, str): datetimes.append(dt_val) - temporal_interval: list[list] = ( - [[min(datetimes), max(datetimes)]] if datetimes else [[None, None]] - ) + + logger.debug(f"Features: {features}") logger.debug( f"Building STAC Collection '{result_name}' from {len(features)} feature(s) " ) + logger.debug(f"bbox = {spatial_bbox}") + return ItemCollection( - bbox=[spatial_bbox], - features=features + bbox=spatial_bbox, + type="FeatureCollection", + links=Links([]), + features=features, ) - - def _remove_stac_links(self, links: Links): - stac_links = [link for link in links.root if link.rel in ["self", "item", "items", "collection", "parent", "root"] or link.rel.endswith("queryables")] + def _remove_stac_links(self, links: Links, keep: list[str] | None = None): + if not keep: + keep = [] + stac_links = [ + link + for link in links.root + if link.rel in ["self", "item", "items", "collection", "parent", "root"] + and not link.rel in keep + or link.rel.endswith("queryables") + ] for link in stac_links: links.root.remove(link) return links.root - def _process_feature_collection( self, feature_collection: dict, @@ -505,14 +514,16 @@ def _process_feature_collection( features_dict = feature_collection.get("features", []) features = [Item.model_validate(f) for f in features_dict] - logger.debug(f"Feature collection: {json.dumps(feature_collection, indent=2)}") - + collection = None for feature in features: - collection_link = next((link for link in feature.links.root if link.rel == "collection"), None) - self._remove_stac_links(feature.links) - feature.links = Links(self._remove_stac_links(feature.links)) + collection_link = next( + (link for link in feature.links.root if link.rel == "collection"), None + ) + feature.links = Links( + self._remove_stac_links(feature.links, keep=["collection"]) + ) feature.assets = self._update_assets_hrefs(feature.assets, user_token) # Some providers expose assets through an item link @@ -538,21 +549,14 @@ def _process_feature_collection( f"Extracted collection '{collection.id}' " f"with assets: {list((collection.assets or {}).keys())}" ) - self._remove_stac_links(collection.links) collection.links = Links(self._remove_stac_links(collection.links)) - item_collection = self._build_item_collection_from_features( - features, - result_name, - user_token, - details, - internal_job_id + features, result_name, user_token, details, internal_job_id ) return item_collection - async def get_job_status( self, user_token: str, job_id: str, details: ServiceDetails ) -> ProcessingStatusEnum: @@ -572,7 +576,6 @@ async def get_job_status( status_info: StatusInfo = api_client.get_status(job_id=internal_job_id) return self._map_ogcapi_status(status_info.status) - async def get_job_results( self, user_token: str, job_id: str, details: ServiceDetails ) -> Collection | ItemCollection: @@ -589,7 +592,7 @@ async def get_job_results( details.endpoint, namespace, exchanged_token ) - result: Dict[str, InlineOrRefData] = api_client.get_result( + result: dict[str, InlineOrRefData] = api_client.get_result( job_id=internal_job_id ) @@ -676,7 +679,7 @@ async def get_job_results( async def get_service_parameters( self, user_token: str, details: ServiceDetails - ) -> List[Parameter]: + ) -> list[Parameter]: parameters = [] logger.debug( From 14b50e2ddf36f10790285d5f662fa45a97dd1b7b Mon Sep 17 00:00:00 2001 From: floeschau Date: Thu, 29 Oct 2026 16:42:49 +0100 Subject: [PATCH 09/22] fix: remove collection from features in FeatureCollection --- app/platforms/implementations/ogc_api_process.py | 1 + 1 file changed, 1 insertion(+) diff --git a/app/platforms/implementations/ogc_api_process.py b/app/platforms/implementations/ogc_api_process.py index dbe1af2..61bb10e 100644 --- a/app/platforms/implementations/ogc_api_process.py +++ b/app/platforms/implementations/ogc_api_process.py @@ -525,6 +525,7 @@ def _process_feature_collection( self._remove_stac_links(feature.links, keep=["collection"]) ) feature.assets = self._update_assets_hrefs(feature.assets, user_token) + feature.collection = None # Some providers expose assets through an item link # instead of inlining them in the feature. From f8ccf8371b9c7320c0306864bb47f2737f94e4ba Mon Sep 17 00:00:00 2001 From: floeschau Date: Thu, 29 Oct 2026 16:43:54 +0100 Subject: [PATCH 10/22] test: adjust test for FeatureCollection OGC API response and ItemCollection result --- .../test_ogc_api_process_platform.py | 52 ++++++++++++++++--- 1 file changed, 46 insertions(+), 6 deletions(-) diff --git a/tests/platforms/test_ogc_api_process_platform.py b/tests/platforms/test_ogc_api_process_platform.py index 9d9cbea..51d5efe 100644 --- a/tests/platforms/test_ogc_api_process_platform.py +++ b/tests/platforms/test_ogc_api_process_platform.py @@ -138,6 +138,44 @@ def build_collection_payload(collection_id="collection-1", title="Test Collectio } +def build_feature_collection_payload(item_id="item-1"): + return { + "type": "FeatureCollection", + "features": [ + { + "bbox": [12.145, 41.996, 12.458, 42.176], + "type": "Feature", + "geometry": { + "type": "Polygon", + "coordinates": [ + [ + [12.145, 41.996], + [12.145, 42.176], + [12.458, 42.176], + [12.458, 41.996], + [12.145, 41.996] + ] + ] + }, + "properties": { + "datetime": None, + "start_datetime": "2024-11-13T17:06:07Z", + "end_datetime": "2024-11-13T17:06:32Z", + "title": f"Title for {item_id}", + }, + "id": item_id, + "stac_version": "1.1.0", + "assets": {}, + "links": [], + "stac_extensions": [], + "collection": "opensartoolkit-v2-2-1-dsb8v-zwdj6w" + } + ], + "links": [], + "bbox": [12.145, 41.996, 12.458, 42.176] + } + + @pytest.mark.parametrize( ("schema", "input_id", "expected_type"), [ @@ -632,22 +670,24 @@ async def test_get_job_status_maps_client_status( @pytest.mark.asyncio +@patch.object(OGCAPIProcessPlatform, "_get_token_for_api", new_callable=AsyncMock) @patch( "app.platforms.implementations.ogc_api_process.exchange_token", new_callable=AsyncMock, ) @patch.object(OGCAPIProcessPlatform, "_create_api_client_instance", new_callable=AsyncMock) async def test_get_job_results_returns_stac_collection( - mock_create_api_client, mock_exchange_token, platform + mock_create_api_client, mock_exchange_token, mock_get_token_for_api, platform ): + mock_get_token_for_api.return_value = "exchanged-token" mock_exchange_token.return_value = "exchanged-token" api_client = MagicMock() api_client.get_result.return_value = { - "result": SimpleNamespace( + "output": SimpleNamespace( actual_instance=SimpleNamespace( - var_schema=SimpleNamespace(actual_instance=STAC_COLLECTION_SCHEMA), + var_schema=SimpleNamespace(actual_instance=GEOJSON_FEATURECOLLECTION_SCHEMA), value=SimpleNamespace( - actual_instance=build_collection_payload("collection-from-stac") + oneof_schema_2_validator=build_feature_collection_payload("item-from-stac") ), ) ) @@ -664,8 +704,8 @@ async def test_get_job_results_returns_stac_collection( ), ) - assert result.id == "collection-from-stac" - assert result.title == "Test Collection" + assert result.features[0].id == "item-from-stac" + assert result.features[0].properties.title == "Title for item-from-stac" @pytest.mark.asyncio From 3fd8f4a6b8cdc4b680624a6e3ab7fcc483b99f31 Mon Sep 17 00:00:00 2001 From: bramjanssen Date: Tue, 17 Nov 2026 09:52:33 +0100 Subject: [PATCH 11/22] chore: code cleanup and test fixes --- app/platforms/base.py | 2 +- .../implementations/ogc_api_process.py | 40 +---- app/routers/unit_jobs.py | 2 +- app/services/processing.py | 4 +- .../test_ogc_api_process_platform.py | 164 ++++++++++-------- tests/routers/test_unit_jobs.py | 3 +- 6 files changed, 107 insertions(+), 108 deletions(-) diff --git a/app/platforms/base.py b/app/platforms/base.py index 546e3b4..5fbf58c 100644 --- a/app/platforms/base.py +++ b/app/platforms/base.py @@ -76,7 +76,7 @@ async def get_job_status( @abstractmethod async def get_job_results( self, user_token: str, job_id: str, details: ServiceDetails - ) -> Collection | ItemCollection: + ) -> ItemCollection: """ Retrieve the job results of a processing job that is running on the platform. diff --git a/app/platforms/implementations/ogc_api_process.py b/app/platforms/implementations/ogc_api_process.py index 9c5d0e9..331c546 100644 --- a/app/platforms/implementations/ogc_api_process.py +++ b/app/platforms/implementations/ogc_api_process.py @@ -317,8 +317,8 @@ def _extract_download_link_from_asset(self, asset: dict) -> str | None: str | None: The download link if available, otherwise None. """ refs = [ - asset.href, - asset.to_dict().get("alternate", {}).get("https", {}).get("href"), + asset.get("href", None), + asset.get("alternate", {}).get("https", {}).get("href"), ] for href in refs: if href and href.startswith("https://"): @@ -405,7 +405,7 @@ def _update_assets_hrefs(self, assets: dict, user_token: str) -> dict: "original HTTPS href." ) signed_url = href - updated_asset.href = signed_url + updated_asset["href"] = signed_url updated_assets[asset_name] = updated_asset return updated_assets @@ -414,9 +414,6 @@ def _build_item_collection_from_features( self, features: list, result_name: str, - user_token: str, - details: ServiceDetails, - internal_job_id: str, ) -> ItemCollection: """ Build a STAC Collection from a list of GeoJSON features. @@ -492,8 +489,6 @@ def _process_feature_collection( *, result_name: str, user_token: str, - details: ServiceDetails, - internal_job_id: str, ) -> ItemCollection: logger.debug(f"Feature collection: {json.dumps(feature_collection, indent=2)}") @@ -529,7 +524,7 @@ def _process_feature_collection( collection_data = response.json() collection = Collection.model_validate(collection_data) collection.assets = self._update_assets_hrefs( - collection.assets, user_token + collection.assets or {}, user_token ) logger.debug( f"Extracted collection '{collection.id}' " @@ -538,7 +533,7 @@ def _process_feature_collection( collection.links = Links(self._remove_stac_links(collection.links)) item_collection = self._build_item_collection_from_features( - features, result_name, user_token, details, internal_job_id + features, result_name ) return item_collection @@ -564,7 +559,7 @@ async def get_job_status( async def get_job_results( self, user_token: str, job_id: str, details: ServiceDetails - ) -> Collection | ItemCollection: + ) -> ItemCollection: logger.debug(f"Fetching job result for opfenEO job with ID {job_id}") logger.debug("Exchanging user token for OGC API Process execution...") @@ -633,9 +628,7 @@ async def get_job_results( return self._process_feature_collection( feature_collection, result_name=result_name, - user_token=exchanged_token or user_token, - details=details, - internal_job_id=internal_job_id, + user_token=exchanged_token or user_token ) else: logger.warning( @@ -644,24 +637,7 @@ async def get_job_results( ) # result not found, send back an empty collection - - return Collection( - id=f"{details.namespace}-{internal_job_id}", - stac_version=STAC_VERSION, - title=f"Results for {details.application}", - description=( - f"OGC API process result items for job '{internal_job_id}' " - f"of application '{details.application}'." - ), - type="Collection", - license="proprietary", - links=Links([]), - extent=Extent( - spatial=SpatialExtent(bbox=[(-180.0, -90.0, 180.0, 90.0)]), - temporal=TimeInterval(interval=[[None, None]]), - ), - assets={}, - ) + return self._build_item_collection_from_features([], "No results found") async def get_service_parameters( self, user_token: str, details: ServiceDetails diff --git a/app/routers/unit_jobs.py b/app/routers/unit_jobs.py index 884793e..0896c9c 100644 --- a/app/routers/unit_jobs.py +++ b/app/routers/unit_jobs.py @@ -213,7 +213,7 @@ async def get_job( ) async def get_job_results( job_id: int, db: Session = Depends(get_db), token: str = Depends(oauth2_scheme) -) -> Collection | ItemCollection | None: +) -> ItemCollection | None: try: result = await get_processing_job_results(token, db, job_id) if not result: diff --git a/app/services/processing.py b/app/services/processing.py index 39b4f87..bc13976 100644 --- a/app/services/processing.py +++ b/app/services/processing.py @@ -4,7 +4,7 @@ from fastapi import Response from loguru import logger from sqlalchemy.orm import Session -from stac_pydantic import Collection +from stac_pydantic.item_collection import ItemCollection from app.auth import get_current_user_id from app.database.models.processing_job import ( @@ -114,7 +114,7 @@ async def get_processing_job_results( token: str, database: Session, job_id: int, -) -> Collection | None: +) -> ItemCollection | None: user = get_current_user_id(token) record = get_job_by_user_id(database, job_id, user) if not record: diff --git a/tests/platforms/test_ogc_api_process_platform.py b/tests/platforms/test_ogc_api_process_platform.py index a21099e..62c7657 100644 --- a/tests/platforms/test_ogc_api_process_platform.py +++ b/tests/platforms/test_ogc_api_process_platform.py @@ -20,9 +20,7 @@ def _install_ogc_api_processes_client_stub(): api_client_wrapper_module = types.ModuleType( "ogc_api_processes_client.api_client_wrapper" ) - configuration_module = types.ModuleType( - "ogc_api_processes_client.configuration" - ) + configuration_module = types.ModuleType("ogc_api_processes_client.configuration") models_module = types.ModuleType("ogc_api_processes_client.models") inline_or_ref_data_module = types.ModuleType( "ogc_api_processes_client.models.inline_or_ref_data" @@ -138,7 +136,12 @@ def build_collection_payload(collection_id="collection-1", title="Test Collectio "description": "Test STAC collection", "type": "Collection", "license": "proprietary", - "links": [], + "links": [ + { + "rel": "collection", + "href": "https://example.com/collections/1", + } + ], "extent": { "spatial": {"bbox": [[-180.0, -90.0, 180.0, 90.0]]}, "temporal": {"interval": [[None, None]]}, @@ -161,9 +164,9 @@ def build_feature_collection_payload(item_id="item-1"): [12.145, 42.176], [12.458, 42.176], [12.458, 41.996], - [12.145, 41.996] + [12.145, 41.996], ] - ] + ], }, "properties": { "datetime": None, @@ -174,13 +177,18 @@ def build_feature_collection_payload(item_id="item-1"): "id": item_id, "stac_version": "1.1.0", "assets": {}, - "links": [], + "links": [ + { + "rel": "collection", + "href": "https://example.com/collections/1", + } + ], "stac_extensions": [], - "collection": "opensartoolkit-v2-2-1-dsb8v-zwdj6w" + "collection": "opensartoolkit-v2-2-1-dsb8v-zwdj6w", } ], "links": [], - "bbox": [12.145, 41.996, 12.458, 42.176] + "bbox": [12.145, 41.996, 12.458, 42.176], } @@ -210,32 +218,28 @@ def build_feature_collection_payload(item_id="item-1"): ({"type": "integer"}, "limit", ParamTypeEnum.INTEGER), ({"type": "double"}, "scale", ParamTypeEnum.DOUBLE), ( - {"type": "object", "required": ["coordinates", "type", "bbox"], - "properties": { - "type": { - "actual_instance": { - "actual_instance": { - "enum": ["Polygon"] - } - } - } - } - }, + { + "type": "object", + "required": ["coordinates", "type", "bbox"], + "properties": { + "type": { + "actual_instance": {"actual_instance": {"enum": ["Polygon"]}} + } + }, + }, "aoi", ParamTypeEnum.POLYGON, ), ( - {"type": "object", "required": ["coordinates", "type", "bbox"], - "properties": { - "type": { - "actual_instance": { - "actual_instance": { - "enum": ["Point"] - } - } - } - } - }, + { + "type": "object", + "required": ["coordinates", "type", "bbox"], + "properties": { + "type": { + "actual_instance": {"actual_instance": {"enum": ["Point"]}} + } + }, + }, "poi", ParamTypeEnum.POINT, ), @@ -285,17 +289,13 @@ def test_transform_bbox_parameter(platform, value, expected): "invalid", { "type": "Polygon", - "coordinates": [ - [[4, 50], [5, 50], [5, 51], [4, 51], [4, 50]] - ], + "coordinates": [[[4, 50], [5, 50], [5, 51], [4, 51], [4, 50]]], }, { "type": "Feature", "geometry": { "type": "Polygon", - "coordinates": [ - [[4, 50], [5, 50], [5, 51], [4, 51], [4, 50]] - ], + "coordinates": [[[4, 50], [5, 50], [5, 51], [4, 51], [4, 50]]], }, }, ], @@ -373,9 +373,7 @@ def test_update_assets_hrefs_keeps_original_href_when_signing_fails(platform): assets = { "result": { "href": "s3://bucket/result.tif", - "alternate": { - "https": {"href": "https://download.example.com/resource"} - }, + "alternate": {"https": {"href": "https://download.example.com/resource"}}, } } @@ -481,7 +479,9 @@ async def test_create_api_client_instance_with_token_and_namespace( ) @patch("app.platforms.implementations.ogc_api_process.get_current_user_claims") @patch.object(OGCAPIProcessPlatform, "_transform_parameters", new_callable=AsyncMock) -@patch.object(OGCAPIProcessPlatform, "_create_api_client_instance", new_callable=AsyncMock) +@patch.object( + OGCAPIProcessPlatform, "_create_api_client_instance", new_callable=AsyncMock +) async def test_execute_job_returns_namespaced_job_id( mock_create_api_client, mock_transform_parameters, @@ -540,7 +540,9 @@ async def test_execute_job_returns_namespaced_job_id( ) @patch("app.platforms.implementations.ogc_api_process.get_current_user_claims") @patch.object(OGCAPIProcessPlatform, "_transform_parameters", new_callable=AsyncMock) -@patch.object(OGCAPIProcessPlatform, "_create_api_client_instance", new_callable=AsyncMock) +@patch.object( + OGCAPIProcessPlatform, "_create_api_client_instance", new_callable=AsyncMock +) async def test_execute_job_returns_plain_job_id_without_namespace( mock_create_api_client, mock_transform_parameters, @@ -591,7 +593,9 @@ async def test_execute_job_returns_plain_job_id_without_namespace( ) @patch("app.platforms.implementations.ogc_api_process.get_current_user_claims") @patch.object(OGCAPIProcessPlatform, "_transform_parameters", new_callable=AsyncMock) -@patch.object(OGCAPIProcessPlatform, "_create_api_client_instance", new_callable=AsyncMock) +@patch.object( + OGCAPIProcessPlatform, "_create_api_client_instance", new_callable=AsyncMock +) async def test_execute_job_omits_missing_optional_user_fields( mock_create_api_client, mock_transform_parameters, @@ -651,7 +655,9 @@ async def test_execute_synchronous_job_not_implemented(platform): "app.platforms.implementations.ogc_api_process.exchange_token", new_callable=AsyncMock, ) -@patch.object(OGCAPIProcessPlatform, "_create_api_client_instance", new_callable=AsyncMock) +@patch.object( + OGCAPIProcessPlatform, "_create_api_client_instance", new_callable=AsyncMock +) async def test_get_job_status_maps_client_status( mock_create_api_client, mock_exchange_token, platform ): @@ -678,25 +684,34 @@ async def test_get_job_status_maps_client_status( @pytest.mark.asyncio +@patch("app.platforms.implementations.ogc_api_process.http_get") @patch.object(OGCAPIProcessPlatform, "_get_token_for_api", new_callable=AsyncMock) @patch( "app.platforms.implementations.ogc_api_process.exchange_token", new_callable=AsyncMock, ) -@patch.object(OGCAPIProcessPlatform, "_create_api_client_instance", new_callable=AsyncMock) +@patch.object( + OGCAPIProcessPlatform, "_create_api_client_instance", new_callable=AsyncMock +) async def test_get_job_results_returns_stac_collection( - mock_create_api_client, mock_exchange_token, mock_get_token_for_api, platform + mock_create_api_client, + mock_exchange_token, + mock_get_token_for_api, + mock_http_get, + platform, ): mock_get_token_for_api.return_value = "exchanged-token" mock_exchange_token.return_value = "exchanged-token" + mock_http_get.return_value.json.return_value = build_collection_payload() + feature_collection = build_feature_collection_payload("item-from-stac") api_client = MagicMock() api_client.get_result.return_value = { "output": SimpleNamespace( actual_instance=SimpleNamespace( - var_schema=SimpleNamespace(actual_instance=GEOJSON_FEATURECOLLECTION_SCHEMA), - value=SimpleNamespace( - oneof_schema_2_validator=build_feature_collection_payload("item-from-stac") + var_schema=SimpleNamespace( + actual_instance=GEOJSON_FEATURECOLLECTION_SCHEMA ), + value=SimpleNamespace(oneof_schema_2_validator=feature_collection), ) ) } @@ -714,6 +729,12 @@ async def test_get_job_results_returns_stac_collection( assert result.features[0].id == "item-from-stac" assert result.features[0].properties.title == "Title for item-from-stac" + mock_http_get.assert_called_once_with( + "https://example.com/collections/1", + follow_redirects=True, + headers={"Authorization": "Bearer exchanged-token"}, + ) + mock_http_get.return_value.raise_for_status.assert_called_once_with() @pytest.mark.asyncio @@ -722,11 +743,14 @@ async def test_get_job_results_returns_stac_collection( "app.platforms.implementations.ogc_api_process.exchange_token", new_callable=AsyncMock, ) -@patch.object(OGCAPIProcessPlatform, "_create_api_client_instance", new_callable=AsyncMock) +@patch.object( + OGCAPIProcessPlatform, "_create_api_client_instance", new_callable=AsyncMock +) async def test_get_job_results_follows_geojson_collection_link( mock_create_api_client, mock_exchange_token, mock_http_get, platform ): mock_exchange_token.return_value = "exchanged-token" + feature_collection = build_feature_collection_payload("item-from-geojson") api_client = MagicMock() api_client.get_result.return_value = { "result": SimpleNamespace( @@ -734,20 +758,7 @@ async def test_get_job_results_follows_geojson_collection_link( var_schema=SimpleNamespace( actual_instance=GEOJSON_FEATURECOLLECTION_SCHEMA ), - value=SimpleNamespace( - oneof_schema_2_validator={ - "features": [ - { - "links": [ - { - "rel": "collection", - "href": "https://example.com/collections/1", - } - ] - } - ] - } - ), + value=SimpleNamespace(oneof_schema_2_validator=feature_collection), ) ) } @@ -767,7 +778,11 @@ async def test_get_job_results_follows_geojson_collection_link( ), ) - assert result.id == "collection-from-geojson" + assert result.type == "FeatureCollection" + assert len(result.features) == 1 + assert result.features[0].id == "item-from-geojson" + assert result.features[0].collection is None + assert result.features[0].links.root[0].rel == "collection" mock_http_get.assert_called_once_with( "https://example.com/collections/1", follow_redirects=True, @@ -781,7 +796,9 @@ async def test_get_job_results_follows_geojson_collection_link( "app.platforms.implementations.ogc_api_process.exchange_token", new_callable=AsyncMock, ) -@patch.object(OGCAPIProcessPlatform, "_create_api_client_instance", new_callable=AsyncMock) +@patch.object( + OGCAPIProcessPlatform, "_create_api_client_instance", new_callable=AsyncMock +) async def test_get_job_results_returns_empty_collection_when_no_supported_result( mock_create_api_client, mock_exchange_token, platform ): @@ -802,9 +819,10 @@ async def test_get_job_results_returns_empty_collection_when_no_supported_result ), ) - assert result.id == "ns-job-123" - assert result.title == "Results for buffer" - assert result.license == "proprietary" + assert result.type == "FeatureCollection" + assert result.features == [] + assert result.links.root == [] + assert result.bbox == (-180.0, -90.0, 180.0, 90.0) @pytest.mark.asyncio @@ -812,7 +830,9 @@ async def test_get_job_results_returns_empty_collection_when_no_supported_result "app.platforms.implementations.ogc_api_process.exchange_token", new_callable=AsyncMock, ) -@patch.object(OGCAPIProcessPlatform, "_create_api_client_instance", new_callable=AsyncMock) +@patch.object( + OGCAPIProcessPlatform, "_create_api_client_instance", new_callable=AsyncMock +) async def test_get_service_parameters_maps_geojson_and_options( mock_create_api_client, mock_exchange_token, platform ): @@ -880,7 +900,9 @@ async def test_get_service_parameters_maps_geojson_and_options( "app.platforms.implementations.ogc_api_process.exchange_token", new_callable=AsyncMock, ) -@patch.object(OGCAPIProcessPlatform, "_create_api_client_instance", new_callable=AsyncMock) +@patch.object( + OGCAPIProcessPlatform, "_create_api_client_instance", new_callable=AsyncMock +) async def test_get_service_parameters_maps_all_supported_types( mock_create_api_client, mock_exchange_token, platform ): diff --git a/tests/routers/test_unit_jobs.py b/tests/routers/test_unit_jobs.py index ef38fb3..0378fe5 100644 --- a/tests/routers/test_unit_jobs.py +++ b/tests/routers/test_unit_jobs.py @@ -2,6 +2,7 @@ from unittest.mock import patch from fastapi import status +from stac_pydantic import ItemCollection from app.error import InternalException @@ -103,9 +104,9 @@ def test_unit_jobs_get_job_internal_error(mock_get_processing_job, client): def test_unit_jobs_get_job_results_200( mock_get_processing_job_results, client, - fake_result, ): + fake_result = ItemCollection(type="FeatureCollection", features=[], links=[]) mock_get_processing_job_results.return_value = fake_result r = client.get("/unit_jobs/1/results") From c063bbe0f7f79e390f15a36b0513cf8dfbc3fad5 Mon Sep 17 00:00:00 2001 From: bramjanssen Date: Tue, 17 Nov 2026 09:54:29 +0100 Subject: [PATCH 12/22] test: fixed validation --- tests/platforms/test_ogc_api_process_platform.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/platforms/test_ogc_api_process_platform.py b/tests/platforms/test_ogc_api_process_platform.py index 62c7657..379a833 100644 --- a/tests/platforms/test_ogc_api_process_platform.py +++ b/tests/platforms/test_ogc_api_process_platform.py @@ -6,6 +6,7 @@ import jwt import pytest import requests +from stac_pydantic import ItemCollection TEST_USER_TOKEN = jwt.encode( @@ -693,7 +694,7 @@ async def test_get_job_status_maps_client_status( @patch.object( OGCAPIProcessPlatform, "_create_api_client_instance", new_callable=AsyncMock ) -async def test_get_job_results_returns_stac_collection( +async def test_get_job_results_returns_item_collection( mock_create_api_client, mock_exchange_token, mock_get_token_for_api, @@ -727,6 +728,8 @@ async def test_get_job_results_returns_stac_collection( ), ) + assert isinstance(result, ItemCollection) + assert result.type == "FeatureCollection" assert result.features[0].id == "item-from-stac" assert result.features[0].properties.title == "Title for item-from-stac" mock_http_get.assert_called_once_with( From 25a61a97a06426be075310691b4a4d444e7f0e41 Mon Sep 17 00:00:00 2001 From: bramjanssen Date: Tue, 17 Nov 2026 11:10:32 +0100 Subject: [PATCH 13/22] feat: implemented changes to align openEO results to itemcollection --- app/platforms/implementations/openeo.py | 14 +++++-- tests/platforms/test_openeo_platform.py | 50 ++++++++++++++++++++++--- tests/routers/test_unit_jobs.py | 3 ++ 3 files changed, 58 insertions(+), 9 deletions(-) diff --git a/app/platforms/implementations/openeo.py b/app/platforms/implementations/openeo.py index 2d35e8d..90e6a47 100644 --- a/app/platforms/implementations/openeo.py +++ b/app/platforms/implementations/openeo.py @@ -9,7 +9,7 @@ import requests from dotenv import load_dotenv from loguru import logger -from stac_pydantic import Collection +from stac_pydantic import Collection, Item, ItemCollection from app.auth import exchange_token from app.config.schemas import AuthMethod @@ -187,10 +187,16 @@ async def _get_job_status_once( async def _get_job_results_once( self, user_token: str, job_id: str, details: ServiceDetails - ) -> Collection: + ) -> ItemCollection: connection = await self._setup_connection(user_token, details.endpoint) job = connection.job(job_id) - return Collection(**job.get_results().get_metadata()) + collection = Collection.model_validate(job.get_results().get_metadata()) + features = [ + Item.model_validate(connection.get(link.href, expected_status=200).json()) + for link in collection.links.root + if link.rel == "item" + ] + return ItemCollection(type="FeatureCollection", features=features, links=[]) def _get_client_credentials(self, url: str) -> tuple[str, str, str]: """ @@ -387,7 +393,7 @@ async def get_job_status( async def get_job_results( self, user_token: str, job_id: str, details: ServiceDetails - ) -> Collection: + ) -> ItemCollection: try: logger.debug(f"Fetching job result for openEO job with ID {job_id}") return await self._get_job_results_once(user_token, job_id, details) diff --git a/tests/platforms/test_openeo_platform.py b/tests/platforms/test_openeo_platform.py index a63202a..0b505f4 100644 --- a/tests/platforms/test_openeo_platform.py +++ b/tests/platforms/test_openeo_platform.py @@ -16,7 +16,7 @@ from app.schemas.enum import OutputFormatEnum, ProcessingStatusEnum from app.schemas.parameters import ParamTypeEnum, Parameter from app.schemas.unit_job import ServiceDetails -from stac_pydantic import Collection +from stac_pydantic import Collection, ItemCollection from openeo.rest import OpenEoApiError @@ -336,14 +336,53 @@ async def test_get_job_status_404_with_other_code_returns_unknown( @pytest.mark.asyncio +@pytest.mark.parametrize("result_kind", ["empty", "assets", "items", "items_and_assets"]) @patch.object(OpenEOPlatform, "_setup_connection") -async def test_get_job_results_success(mock_connection, platform, fake_result): - mock_connection.return_value = DummyOpenEOClient(result=fake_result) +async def test_get_job_results_success( + mock_connection, platform, fake_result, result_kind +): + metadata = fake_result.model_dump() + assets = {"result": {"href": "https://example.com/result.tif"}} + item = { + "type": "Feature", + "id": "result-item", + "geometry": None, + "properties": {"datetime": "2025-01-01T00:00:00Z"}, + "assets": assets, + "links": [], + } + connection = MagicMock() + connection.job.return_value.get_results.return_value.get_metadata.return_value = metadata + if result_kind in ("assets", "items_and_assets"): + metadata["assets"] = { + "collection-only": {"href": "https://example.com/collection-only.tif"} + } + if result_kind in ("items", "items_and_assets"): + metadata["links"] = [ + {"rel": "item", "href": "https://example.com/items/result-item"} + ] + connection.get.return_value.json.return_value = item + mock_connection.return_value = connection details = ServiceDetails(endpoint="foo", application="bar") result = await platform.get_job_results("foobar", "job123", details) - assert result == fake_result + assert isinstance(result, ItemCollection) + assert result.type == "FeatureCollection" + if result_kind in ("empty", "assets"): + assert result.features == [] + else: + assert len(result.features) == 1 + assert result.features[0].model_dump(mode="json", exclude_none=True)[ + "assets" + ] == assets + assert result.features[0].id == "result-item" + if result_kind in ("items", "items_and_assets"): + connection.get.assert_called_once_with( + "https://example.com/items/result-item", expected_status=200 + ) + else: + connection.get.assert_not_called() @pytest.mark.asyncio @@ -818,7 +857,8 @@ async def test_get_job_results_retries_after_auth_error( result = await platform.get_job_results("fake_token", "job-1", service_details) - assert result == fake_result + assert isinstance(result, ItemCollection) + assert result.type == "FeatureCollection" mock_refresh_connection.assert_awaited_once_with( "fake_token", service_details.endpoint ) diff --git a/tests/routers/test_unit_jobs.py b/tests/routers/test_unit_jobs.py index 0378fe5..830dfd5 100644 --- a/tests/routers/test_unit_jobs.py +++ b/tests/routers/test_unit_jobs.py @@ -111,6 +111,9 @@ def test_unit_jobs_get_job_results_200( r = client.get("/unit_jobs/1/results") assert r.status_code == 200 + assert r.json()["type"] == "FeatureCollection" + assert "features" in r.json() + assert isinstance(ItemCollection.model_validate(r.json()), ItemCollection) assert json.dumps(r.json(), indent=1) == fake_result.model_dump_json( indent=1, exclude_unset=False ) From 88f848ed41ac1b7e47d45c3c28aba969559c7e39 Mon Sep 17 00:00:00 2001 From: bramjanssen Date: Tue, 17 Nov 2026 11:42:36 +0100 Subject: [PATCH 14/22] chore: fixed linting --- app/platforms/implementations/openeo.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/platforms/implementations/openeo.py b/app/platforms/implementations/openeo.py index 90e6a47..ff52d6a 100644 --- a/app/platforms/implementations/openeo.py +++ b/app/platforms/implementations/openeo.py @@ -196,7 +196,7 @@ async def _get_job_results_once( for link in collection.links.root if link.rel == "item" ] - return ItemCollection(type="FeatureCollection", features=features, links=[]) + return ItemCollection(type="FeatureCollection", features=features) def _get_client_credentials(self, url: str) -> tuple[str, str, str]: """ From 7991bd52cff2dce335dc5ae974a79196305f5341 Mon Sep 17 00:00:00 2001 From: bramjanssen Date: Tue, 17 Nov 2026 11:43:18 +0100 Subject: [PATCH 15/22] ogc: aligned result handling --- .../implementations/ogc_api_process.py | 22 ++++++++++- .../test_ogc_api_process_platform.py | 38 +++++++++++++++++-- 2 files changed, 56 insertions(+), 4 deletions(-) diff --git a/app/platforms/implementations/ogc_api_process.py b/app/platforms/implementations/ogc_api_process.py index 331c546..a48555a 100644 --- a/app/platforms/implementations/ogc_api_process.py +++ b/app/platforms/implementations/ogc_api_process.py @@ -1,5 +1,6 @@ import json import re +from asyncio import to_thread from urllib.parse import urlparse import jwt @@ -615,9 +616,28 @@ async def get_job_results( if STAC_COLLECTION_SCHEMA == schema_reference: logger.success(f"STAC Collection found in results: '{result_name}'") - return Collection.model_validate( + collection = Collection.model_validate( qualified_value.value.actual_instance ) + features = [] + for link in collection.links.root: + if link.rel != "item": + continue + response = await to_thread( + http_get, + link.href, + follow_redirects=True, + headers={ + "Authorization": f"Bearer {exchanged_token or user_token}" + }, + ) + response.raise_for_status() + features.append(response.json()) + return self._process_feature_collection( + {"features": features}, + result_name=result_name, + user_token=exchanged_token or user_token, + ) elif GEOJSON_FEATURECOLLECTION_SCHEMA == schema_reference: logger.success( f"GeoJSON FeatureCollection found in results: '{result_name}'" diff --git a/tests/platforms/test_ogc_api_process_platform.py b/tests/platforms/test_ogc_api_process_platform.py index 379a833..81b13e2 100644 --- a/tests/platforms/test_ogc_api_process_platform.py +++ b/tests/platforms/test_ogc_api_process_platform.py @@ -685,6 +685,9 @@ async def test_get_job_status_maps_client_status( @pytest.mark.asyncio +@pytest.mark.parametrize( + "schema_reference", [GEOJSON_FEATURECOLLECTION_SCHEMA, STAC_COLLECTION_SCHEMA] +) @patch("app.platforms.implementations.ogc_api_process.http_get") @patch.object(OGCAPIProcessPlatform, "_get_token_for_api", new_callable=AsyncMock) @patch( @@ -700,19 +703,34 @@ async def test_get_job_results_returns_item_collection( mock_get_token_for_api, mock_http_get, platform, + schema_reference, ): mock_get_token_for_api.return_value = "exchanged-token" mock_exchange_token.return_value = "exchanged-token" mock_http_get.return_value.json.return_value = build_collection_payload() feature_collection = build_feature_collection_payload("item-from-stac") + collection = build_collection_payload() + collection["links"] = [ + {"rel": "item", "href": "https://example.com/items/item-from-stac"} + ] + collection["assets"] = { + "ignored": {"href": "https://example.com/collection-only.tif"} + } + if schema_reference == STAC_COLLECTION_SCHEMA: + item_response = MagicMock() + item_response.json.return_value = feature_collection["features"][0] + mock_http_get.side_effect = [item_response, mock_http_get.return_value] api_client = MagicMock() api_client.get_result.return_value = { "output": SimpleNamespace( actual_instance=SimpleNamespace( var_schema=SimpleNamespace( - actual_instance=GEOJSON_FEATURECOLLECTION_SCHEMA + actual_instance=schema_reference + ), + value=SimpleNamespace( + actual_instance=collection, + oneof_schema_2_validator=feature_collection, ), - value=SimpleNamespace(oneof_schema_2_validator=feature_collection), ) ) } @@ -730,9 +748,23 @@ async def test_get_job_results_returns_item_collection( assert isinstance(result, ItemCollection) assert result.type == "FeatureCollection" + assert len(result.features) == 1 assert result.features[0].id == "item-from-stac" assert result.features[0].properties.title == "Title for item-from-stac" - mock_http_get.assert_called_once_with( + assert result.features[0].assets == {} + if schema_reference == STAC_COLLECTION_SCHEMA: + assert mock_http_get.call_count == 2 + assert mock_http_get.call_args_list[0].args == ( + "https://example.com/items/item-from-stac", + ) + assert mock_http_get.call_args_list[0].kwargs == { + "follow_redirects": True, + "headers": {"Authorization": "Bearer exchanged-token"}, + } + item_response.raise_for_status.assert_called_once_with() + else: + mock_http_get.assert_called_once() + mock_http_get.assert_called_with( "https://example.com/collections/1", follow_redirects=True, headers={"Authorization": "Bearer exchanged-token"}, From 3c9dbdb2a8ce8c622ecaf55e59c8729d56f8f475 Mon Sep 17 00:00:00 2001 From: bramjanssen Date: Tue, 17 Nov 2026 11:50:54 +0100 Subject: [PATCH 16/22] chore: code cleanup for flake8 --- app/auth.py | 1 - app/platforms/base.py | 1 - app/platforms/implementations/ogc_api_process.py | 10 +++------- app/routers/unit_jobs.py | 1 - 4 files changed, 3 insertions(+), 10 deletions(-) diff --git a/app/auth.py b/app/auth.py index 3b00bc5..5fedd1f 100644 --- a/app/auth.py +++ b/app/auth.py @@ -160,7 +160,6 @@ async def _exchange_token_for_provider( "scope": "openid profile email", } - provider_str = backend_idp.token_provider or backend_idp.token_url try: async with httpx.AsyncClient(timeout=BROKER_TOKEN_TIMEOUT_SECONDS) as client: resp = await client.post(backend_idp.token_url, data=payload) diff --git a/app/platforms/base.py b/app/platforms/base.py index 5fbf58c..a0aa127 100644 --- a/app/platforms/base.py +++ b/app/platforms/base.py @@ -7,7 +7,6 @@ from app.schemas.parameters import Parameter from app.schemas.unit_job import ServiceDetails -from stac_pydantic.collection import Collection from stac_pydantic.item_collection import ItemCollection diff --git a/app/platforms/implementations/ogc_api_process.py b/app/platforms/implementations/ogc_api_process.py index a48555a..1e4b750 100644 --- a/app/platforms/implementations/ogc_api_process.py +++ b/app/platforms/implementations/ogc_api_process.py @@ -1,9 +1,7 @@ import json import re from asyncio import to_thread -from urllib.parse import urlparse -import jwt import requests from fastapi import Response from httpx import Response as HTTPXResponse @@ -17,11 +15,10 @@ from ogc_api_processes_client.models.qualified_input_value import QualifiedInputValue from ogc_api_processes_client.models.status_code import StatusCode from ogc_api_processes_client.models.status_info import StatusInfo -from stac_pydantic.collection import Collection, Extent, SpatialExtent, TimeInterval +from stac_pydantic.collection import Collection from stac_pydantic.item import Item from stac_pydantic.item_collection import ItemCollection from stac_pydantic.links import Links -from stac_pydantic.version import STAC_VERSION from app.auth import exchange_token, get_current_user_claims from app.platforms.base import BaseProcessingPlatform @@ -477,7 +474,7 @@ def _remove_stac_links(self, links: Links, keep: list[str] | None = None): link for link in links.root if link.rel in ["self", "item", "items", "collection", "parent", "root"] - and not link.rel in keep + and link.rel not in keep or link.rel.endswith("queryables") ] for link in stac_links: @@ -536,7 +533,6 @@ def _process_feature_collection( item_collection = self._build_item_collection_from_features( features, result_name ) - return item_collection async def get_job_status( @@ -648,7 +644,7 @@ async def get_job_results( return self._process_feature_collection( feature_collection, result_name=result_name, - user_token=exchanged_token or user_token + user_token=exchanged_token or user_token, ) else: logger.warning( diff --git a/app/routers/unit_jobs.py b/app/routers/unit_jobs.py index 0896c9c..efb6a5b 100644 --- a/app/routers/unit_jobs.py +++ b/app/routers/unit_jobs.py @@ -26,7 +26,6 @@ get_processing_job_results, ) -from stac_pydantic.collection import Collection from stac_pydantic.item_collection import ItemCollection # from app.auth import get_current_user From 11ab5942133bfd6b5aa3ce05bf08af008522518a Mon Sep 17 00:00:00 2001 From: bramjanssen Date: Tue, 17 Nov 2026 11:53:37 +0100 Subject: [PATCH 17/22] chore: removed env file --- .gitignore | 3 +++ env-t2 | 18 ------------------ 2 files changed, 3 insertions(+), 18 deletions(-) delete mode 100644 env-t2 diff --git a/.gitignore b/.gitignore index 23c4682..10b63f9 100644 --- a/.gitignore +++ b/.gitignore @@ -196,3 +196,6 @@ cython_debug/ .vscode/settings.json .codex + + +env* diff --git a/env-t2 b/env-t2 deleted file mode 100644 index ebc284c..0000000 --- a/env-t2 +++ /dev/null @@ -1,18 +0,0 @@ -# General Settings -APP_NAME="APEx Dispatch API" -APP_DESCRIPTION="APEx Dispatch Service API to run jobs and upscaling tasks" -APP_ENV=development - -CORS_ALLOWED_ORIGINS=http://localhost:5173 - -# Database Settings -DATABASE_URL=postgresql+psycopg2://apex_user:apex_pass@db:5432/apex_db - -# Keycloak Settings -KEYCLOAK_HOST="https://iam.terradue.com" -KEYCLOAK_REALM=master -KEYCLOAK_CLIENT_ID=apex-client-id -KEYCLOAK_CLIENT_SECRET=apex-client-secret - - -BACKENDS='"https://openeo.backend2.com": {"auth_method": "USER_CREDENTIALS", "token_provider": "backend", "token_prefix": "oidc/backend"}}' From 41e25da3122e812a03ad4b4cf53e169e142d346d Mon Sep 17 00:00:00 2001 From: floeschau Date: Thu, 19 Nov 2026 14:08:02 +0100 Subject: [PATCH 18/22] fix: revert asset to Pydantic object --- app/platforms/implementations/ogc_api_process.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/platforms/implementations/ogc_api_process.py b/app/platforms/implementations/ogc_api_process.py index 1e4b750..e1d47bc 100644 --- a/app/platforms/implementations/ogc_api_process.py +++ b/app/platforms/implementations/ogc_api_process.py @@ -315,8 +315,8 @@ def _extract_download_link_from_asset(self, asset: dict) -> str | None: str | None: The download link if available, otherwise None. """ refs = [ - asset.get("href", None), - asset.get("alternate", {}).get("https", {}).get("href"), + asset.href, + asset.to_dict().get("alternate", {}).get("https", {}).get("href"), ] for href in refs: if href and href.startswith("https://"): @@ -403,7 +403,7 @@ def _update_assets_hrefs(self, assets: dict, user_token: str) -> dict: "original HTTPS href." ) signed_url = href - updated_asset["href"] = signed_url + updated_asset.href = signed_url updated_assets[asset_name] = updated_asset return updated_assets From b719ad77fe0c88c67dab4604aaf1ed54733c1356 Mon Sep 17 00:00:00 2001 From: floeschau Date: Thu, 19 Nov 2026 15:08:50 +0100 Subject: [PATCH 19/22] fix: set correct type annotation for Asset --- app/platforms/implementations/ogc_api_process.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/app/platforms/implementations/ogc_api_process.py b/app/platforms/implementations/ogc_api_process.py index e1d47bc..002ab9a 100644 --- a/app/platforms/implementations/ogc_api_process.py +++ b/app/platforms/implementations/ogc_api_process.py @@ -19,6 +19,7 @@ from stac_pydantic.item import Item from stac_pydantic.item_collection import ItemCollection from stac_pydantic.links import Links +from stac_pydantic.shared import Asset from app.auth import exchange_token, get_current_user_claims from app.platforms.base import BaseProcessingPlatform @@ -301,7 +302,7 @@ def _map_ogcapi_status(self, ogcapi_status: StatusCode) -> ProcessingStatusEnum: logger.warning(f"Mapping of unknown OGC API status: {ogcapi_status}") return ProcessingStatusEnum.UNKNOWN - def _extract_download_link_from_asset(self, asset: dict) -> str | None: + def _extract_download_link_from_asset(self, asset: Asset) -> str | None: """ Extracts the download link from an asset dictionary. Checks if the `href` field is present and contains an HTTPS URL. If this is not @@ -378,7 +379,7 @@ def _generate_signed_url(self, href: str, user_token: str) -> str | None: ) return None - def _update_assets_hrefs(self, assets: dict, user_token: str) -> dict: + def _update_assets_hrefs(self, assets: dict[str, Asset], user_token: str) -> dict: """ Update the hrefs of the assets to be HTTPS URLs. If the current href is an S3 URL, the code will look into `alternate` links to From fb1fb4179a48d5738d2ffc5f652a6a262889cf9b Mon Sep 17 00:00:00 2001 From: floeschau Date: Thu, 19 Nov 2026 15:19:56 +0100 Subject: [PATCH 20/22] fix: set correctly typed asset in test --- tests/platforms/test_ogc_api_process_platform.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/platforms/test_ogc_api_process_platform.py b/tests/platforms/test_ogc_api_process_platform.py index 81b13e2..1af7830 100644 --- a/tests/platforms/test_ogc_api_process_platform.py +++ b/tests/platforms/test_ogc_api_process_platform.py @@ -6,7 +6,8 @@ import jwt import pytest import requests -from stac_pydantic import ItemCollection +from stac_pydantic.shared import Asset +from stac_pydantic.item_collection import ItemCollection TEST_USER_TOKEN = jwt.encode( @@ -372,7 +373,7 @@ def test_generate_signed_url_returns_none_on_request_exception( def test_update_assets_hrefs_keeps_original_href_when_signing_fails(platform): assets = { - "result": { + "result": Asset( "href": "s3://bucket/result.tif", "alternate": {"https": {"href": "https://download.example.com/resource"}}, } From d8dc7917e52255cf7624f5a3ff168fe50a6488e2 Mon Sep 17 00:00:00 2001 From: floeschau Date: Thu, 19 Nov 2026 15:25:27 +0100 Subject: [PATCH 21/22] chore: format --- tests/platforms/test_ogc_api_process_platform.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/tests/platforms/test_ogc_api_process_platform.py b/tests/platforms/test_ogc_api_process_platform.py index 1af7830..0aa42bd 100644 --- a/tests/platforms/test_ogc_api_process_platform.py +++ b/tests/platforms/test_ogc_api_process_platform.py @@ -373,10 +373,12 @@ def test_generate_signed_url_returns_none_on_request_exception( def test_update_assets_hrefs_keeps_original_href_when_signing_fails(platform): assets = { - "result": Asset( - "href": "s3://bucket/result.tif", - "alternate": {"https": {"href": "https://download.example.com/resource"}}, - } + "result": Asset.model_validate( + { + "href": "s3://bucket/result.tif", + "alternate": {"https": {"href": "https://download.example.com/resource"}}, + } + ) } with patch.object(platform, "_generate_signed_url", return_value=None): From 0aaf522653c9756d8ebc2b2dd98b087b8edc455e Mon Sep 17 00:00:00 2001 From: floeschau Date: Thu, 19 Nov 2026 15:29:00 +0100 Subject: [PATCH 22/22] fix: set correctly typed asset in test (2) --- tests/platforms/test_ogc_api_process_platform.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/platforms/test_ogc_api_process_platform.py b/tests/platforms/test_ogc_api_process_platform.py index 0aa42bd..6099368 100644 --- a/tests/platforms/test_ogc_api_process_platform.py +++ b/tests/platforms/test_ogc_api_process_platform.py @@ -384,7 +384,7 @@ def test_update_assets_hrefs_keeps_original_href_when_signing_fails(platform): with patch.object(platform, "_generate_signed_url", return_value=None): updated = platform._update_assets_hrefs(assets, "token") - assert updated["result"]["href"] == "https://download.example.com/resource" + assert updated["result"].href == "https://download.example.com/resource" @pytest.mark.parametrize(