-
Notifications
You must be signed in to change notification settings - Fork 14.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
AIP-84 Get Configs / Get Config Value (#43841)
* AIP-84 Get Config * Fix section param type and text format * Add test for Get Config * Consolidate redundant for check expose config * Add `Accept` header depends for Json and Text * Refactor config, dag_source with common header * Refactor test_config with non-sensitive-only case * Fix OpenAPI schema for Headers, Get Config * Refactor test_config - add `HEADERS_NONE` and `HEADERS_ANY` cases - modularize common json response cases - add `_validate_response` common method * Fix style by ruff format
- Loading branch information
1 parent
aa7a3b2
commit 3b8f834
Showing
17 changed files
with
1,468 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# 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, Header, HTTPException, status | ||
from typing_extensions import Annotated | ||
|
||
from airflow.api_fastapi.common.types import Mimetype | ||
|
||
|
||
def header_accept_json_or_text_depends( | ||
accept: Annotated[ | ||
str, | ||
Header( | ||
json_schema_extra={ | ||
"type": "string", | ||
"enum": [Mimetype.JSON, Mimetype.TEXT, Mimetype.ANY], | ||
} | ||
), | ||
] = Mimetype.ANY, | ||
) -> Mimetype: | ||
if accept.startswith(Mimetype.ANY): | ||
return Mimetype.ANY | ||
if accept.startswith(Mimetype.JSON): | ||
return Mimetype.JSON | ||
if accept.startswith(Mimetype.TEXT): | ||
return Mimetype.TEXT | ||
raise HTTPException( | ||
status_code=status.HTTP_406_NOT_ACCEPTABLE, | ||
detail="Only application/json or text/plain is supported", | ||
) | ||
|
||
|
||
HeaderAcceptJsonOrText = Annotated[Mimetype, Depends(header_accept_json_or_text_depends)] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
# 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 pydantic import BaseModel | ||
|
||
|
||
class ConfigOption(BaseModel): | ||
"""Config option.""" | ||
|
||
key: str | ||
value: str | tuple[str, str] | ||
|
||
@property | ||
def text_format(self): | ||
if isinstance(self.value, tuple): | ||
return f"{self.key} = {self.value[0]} {self.value[1]}" | ||
return f"{self.key} = {self.value}" | ||
|
||
|
||
class ConfigSection(BaseModel): | ||
"""Config Section Schema.""" | ||
|
||
name: str | ||
options: list[ConfigOption] | ||
|
||
@property | ||
def text_format(self): | ||
""" | ||
Convert the config section to text format. | ||
Example: | ||
``` | ||
[section_name] | ||
key1 = value1 | ||
key2 = value2 | ||
``` | ||
""" | ||
return f"[{self.name}]\n" + "\n".join(option.text_format for option in self.options) + "\n" | ||
|
||
|
||
class Config(BaseModel): | ||
"""List of config sections with their options.""" | ||
|
||
sections: list[ConfigSection] | ||
|
||
@property | ||
def text_format(self): | ||
# convert all config sections to text | ||
return "\n".join(section.text_format for section in self.sections) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.