|
| 1 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +# or more contributor license agreements. See the NOTICE file |
| 3 | +# distributed with this work for additional information |
| 4 | +# regarding copyright ownership. The ASF licenses this file |
| 5 | +# to you under the Apache License, Version 2.0 (the |
| 6 | +# "License"); you may not use this file except in compliance |
| 7 | +# with the License. You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, |
| 12 | +# software distributed under the License is distributed on an |
| 13 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +# KIND, either express or implied. See the License for the |
| 15 | +# specific language governing permissions and limitations |
| 16 | +# under the License. |
| 17 | + |
| 18 | +from __future__ import annotations |
| 19 | + |
| 20 | +from collections import abc |
| 21 | +from datetime import datetime |
| 22 | + |
| 23 | +from pydantic import BaseModel, computed_field, field_validator |
| 24 | + |
| 25 | +from airflow.api_fastapi.common.types import TimeDeltaWithValidation |
| 26 | +from airflow.serialization.serialized_objects import encode_priority_weight_strategy |
| 27 | +from airflow.task.priority_strategy import PriorityWeightStrategy |
| 28 | + |
| 29 | + |
| 30 | +class TaskResponse(BaseModel): |
| 31 | + """Task serializer for responses.""" |
| 32 | + |
| 33 | + task_id: str | None |
| 34 | + task_display_name: str | None |
| 35 | + owner: str | None |
| 36 | + start_date: datetime | None |
| 37 | + end_date: datetime | None |
| 38 | + trigger_rule: str | None |
| 39 | + depends_on_past: bool |
| 40 | + wait_for_downstream: bool |
| 41 | + retries: float | None |
| 42 | + queue: str | None |
| 43 | + pool: str | None |
| 44 | + pool_slots: float | None |
| 45 | + execution_timeout: TimeDeltaWithValidation | None |
| 46 | + retry_delay: TimeDeltaWithValidation | None |
| 47 | + retry_exponential_backoff: bool |
| 48 | + priority_weight: float | None |
| 49 | + weight_rule: str | None |
| 50 | + ui_color: str | None |
| 51 | + ui_fgcolor: str | None |
| 52 | + template_fields: list[str] | None |
| 53 | + downstream_task_ids: list[str] | None |
| 54 | + doc_md: str | None |
| 55 | + operator_name: str | None |
| 56 | + params: abc.MutableMapping | None |
| 57 | + class_ref: dict | None |
| 58 | + is_mapped: bool | None |
| 59 | + |
| 60 | + @field_validator("weight_rule", mode="before") |
| 61 | + @classmethod |
| 62 | + def validate_weight_rule(cls, wr: str | PriorityWeightStrategy | None) -> str | None: |
| 63 | + """Validate the weight_rule property.""" |
| 64 | + if wr is None: |
| 65 | + return None |
| 66 | + if isinstance(wr, str): |
| 67 | + return wr |
| 68 | + return encode_priority_weight_strategy(wr) |
| 69 | + |
| 70 | + @field_validator("params", mode="before") |
| 71 | + @classmethod |
| 72 | + def get_params(cls, params: abc.MutableMapping | None) -> dict | None: |
| 73 | + """Convert params attribute to dict representation.""" |
| 74 | + if params is None: |
| 75 | + return None |
| 76 | + return {param_name: param_val.dump() for param_name, param_val in params.items()} |
| 77 | + |
| 78 | + # Mypy issue https://github.com/python/mypy/issues/1362 |
| 79 | + @computed_field # type: ignore[misc] |
| 80 | + @property |
| 81 | + def extra_links(self) -> list[str]: |
| 82 | + """Extract and return extra_links.""" |
| 83 | + return getattr(self, "operator_extra_links", []) |
0 commit comments