amoghrajesh commented on code in PR #67041: URL: https://github.com/apache/airflow/pull/67041#discussion_r3271438557
########## airflow-core/src/airflow/api_fastapi/core_api/routes/public/asset_state.py: ########## @@ -0,0 +1,138 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +from __future__ import annotations + +from fastapi import Depends, HTTPException, status +from sqlalchemy import select + +from airflow._shared.state import AssetScope +from airflow.api_fastapi.common.db.common import SessionDep, paginated_select +from airflow.api_fastapi.common.parameters import QueryLimit, QueryOffset +from airflow.api_fastapi.common.router import AirflowRouter +from airflow.api_fastapi.core_api.datamodels.asset_state import ( + AssetStateBody, + AssetStateCollectionResponse, + AssetStateResponse, +) +from airflow.api_fastapi.core_api.openapi.exceptions import create_openapi_http_exception_doc +from airflow.api_fastapi.core_api.security import requires_access_asset +from airflow.models.asset_state import AssetStateModel +from airflow.state.metastore import MetastoreStateBackend + +asset_state_router = AirflowRouter( + tags=["Asset State"], + prefix="/assets/{asset_id}/states", +) + + +@asset_state_router.get( + "", + responses=create_openapi_http_exception_doc([status.HTTP_404_NOT_FOUND]), + dependencies=[Depends(requires_access_asset(method="GET"))], +) +def list_asset_states( + asset_id: int, + limit: QueryLimit, + offset: QueryOffset, + session: SessionDep, +) -> AssetStateCollectionResponse: + """List all state entries for an asset.""" + base = select( + AssetStateModel.key, + AssetStateModel.value, + AssetStateModel.updated_at, + ).where(AssetStateModel.asset_id == asset_id) + paginated, total_entries = paginated_select( + statement=base, filters=[], order_by=None, offset=offset, limit=limit, session=session + ) + rows = session.execute(paginated).all() + entries = [AssetStateResponse(key=r.key, value=r.value, updated_at=r.updated_at) for r in rows] + return AssetStateCollectionResponse(asset_states=entries, total_entries=total_entries) + + +@asset_state_router.get( + "/{key}", + responses=create_openapi_http_exception_doc([status.HTTP_404_NOT_FOUND]), + dependencies=[Depends(requires_access_asset(method="GET"))], +) +def get_asset_state( + asset_id: int, + key: str, + session: SessionDep, +) -> AssetStateResponse: + """Get a single asset state entry.""" + row = session.execute( + select( + AssetStateModel.key, + AssetStateModel.value, + AssetStateModel.updated_at, + ).where( + AssetStateModel.asset_id == asset_id, + AssetStateModel.key == key, + ) + ).one_or_none() + if row is None: + raise HTTPException( + status_code=status.HTTP_404_NOT_FOUND, + detail=f"Asset state key {key!r} not found", + ) + return AssetStateResponse(key=row.key, value=row.value, updated_at=row.updated_at) + + +@asset_state_router.put( + "/{key}", + status_code=status.HTTP_204_NO_CONTENT, + responses=create_openapi_http_exception_doc([status.HTTP_404_NOT_FOUND]), + dependencies=[Depends(requires_access_asset(method="PUT"))], +) +def set_asset_state( + asset_id: int, + key: str, + body: AssetStateBody, + session: SessionDep, +) -> None: + """Set an asset state value. Creates or overwrites the key.""" + MetastoreStateBackend().set(AssetScope(asset_id=asset_id), key, body.value, session=session) Review Comment: Adding a `_get_asset_or_404` dependency that does a lightweight `SELECT id FROM asset WHERE id = :asset_id` and raises 404 if not found. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
