jedcunningham commented on code in PR #45300: URL: https://github.com/apache/airflow/pull/45300#discussion_r1996627411
########## airflow/cli/api/client.py: ########## @@ -0,0 +1,264 @@ +# 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 + +import contextlib +import json +import os +import sys +from functools import wraps +from typing import TYPE_CHECKING, Any, Callable, TypeVar, cast + +import httpx +import keyring +import structlog +from platformdirs import user_config_path +from uuid6 import uuid7 + +from airflow.cli.api.operations import ( + AssetsOperations, + BackfillsOperations, + ConfigOperations, + ConnectionsOperations, + DagOperations, + DagRunOperations, + JobsOperations, + PoolsOperations, + ProvidersOperations, + ServerResponseError, + VariablesOperations, + VersionOperations, +) +from airflow.exceptions import AirflowNotFoundException +from airflow.typing_compat import ParamSpec +from airflow.version import version + +if TYPE_CHECKING: + # # methodtools doesn't have typestubs, so give a stub + def lru_cache(maxsize: int | None = 128): + def wrapper(f): + return f + + return wrapper +else: + from methodtools import lru_cache + +log = structlog.get_logger(logger_name=__name__) + +__all__ = [ + "Client", + "Credentials", +] + +PS = ParamSpec("PS") +RT = TypeVar("RT") + + +def add_correlation_id(request: httpx.Request): + request.headers["correlation-id"] = str(uuid7()) + + +def get_json_error(response: httpx.Response): + """Raise a ServerResponseError if we can extract error info from the error.""" + err = ServerResponseError.from_response(response) + if err: + log.warning("Server error ", extra=dict(err.response.json())) + raise err + + +def raise_on_4xx_5xx(response: httpx.Response): + return get_json_error(response) or response.raise_for_status() + + +# Credentials for the API +class Credentials: + """Credentials for the API.""" + + api_url: str | None + api_token: str | None + api_environment: str + + def __init__( + self, + api_url: str | None = None, + api_token: str | None = None, + api_environment: str = "production", Review Comment: I follow now, it's more like name for a git remote, or k8s cluster in your config. How this is presented to users in docs and help text in the cli command is going to be critical, as this is a new concept completely and isn't immediately obvious like "url" is. I think env var or flag like you have now is fine. Not a blocker, but I think a rename and having something like current context in k8s would be a nice usability improvement. Maybe in a follow up. -- 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]
