You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by GitBox <gi...@apache.org> on 2022/02/08 15:36:35 UTC

[GitHub] [airflow] sungchun12 commented on a change in pull request #20998: Add dbt Cloud provider

sungchun12 commented on a change in pull request #20998:
URL: https://github.com/apache/airflow/pull/20998#discussion_r800740861



##########
File path: airflow/providers/dbt/cloud/hooks/dbt.py
##########
@@ -0,0 +1,487 @@
+# 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.
+
+import json
+import sys
+import time
+from enum import Enum
+from functools import wraps
+from inspect import signature
+from typing import Any, Callable, Dict, List, Optional, Sequence, Set, Tuple, Union
+
+from requests import PreparedRequest, Session
+from requests.auth import AuthBase
+from requests.models import Response
+
+from airflow.exceptions import AirflowException
+from airflow.models import Connection
+from airflow.providers.http.hooks.http import HttpHook
+from airflow.typing_compat import TypedDict
+
+if sys.version_info >= (3, 8):
+    from functools import cached_property
+else:
+    from cached_property import cached_property
+
+
+def fallback_to_default_account(func: Callable) -> Callable:
+    """
+    Decorator which provides a fallback value for ``account_id``. If the ``account_id`` is None or not passed
+    to the decorated function, the value will be taken from the configured dbt Cloud Airflow Connection.
+    """
+    sig = signature(func)
+
+    @wraps(func)
+    def wrapper(*args, **kwargs) -> Callable:
+        bound_args = sig.bind(*args, **kwargs)
+
+        # Check if ``account_id`` was not included in the function signature or, if it is, the value is not
+        # provided.
+        if bound_args.arguments.get("account_id") is None:
+            self = args[0]
+            default_account_id = self.conn.login
+            if not default_account_id:
+                raise AirflowException("Could not determine the dbt Cloud account.")
+
+            bound_args.arguments["account_id"] = int(default_account_id)
+
+        return func(*bound_args.args, **bound_args.kwargs)
+
+    return wrapper
+
+
+class TokenAuth(AuthBase):
+    """Helper class for Auth when executing requests."""
+
+    def __init__(self, token: str) -> None:
+        self.token = token
+
+    def __call__(self, request: PreparedRequest) -> PreparedRequest:
+        request.headers["Content-Type"] = "application/json"
+        request.headers["Authorization"] = f"Token {self.token}"
+

Review comment:
       Add in a user agent to the header, and dynamically pass in the version of the airflow dbt Cloud provider to track usage on the dbt Cloud side. 
   
   ```suggestion
           request.headers["User-Agent"] = f"airflow-dbt-cloud-provider-v{X.Y.Z}"
   ```




-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org