Skip to content

Commit

Permalink
Use api version only in GoogleAdsHook not operators (#15266)
Browse files Browse the repository at this point in the history
Add parameter api_version to both GoogleAdsToGcsOperator & GoogleAdsListAccountsOperator.
Remove repeated line self.gcp_conn_id = gcp_conn_id.
Change the default api_version to v5 since v3 is deprecated.
Add api_version to GoogleAdsHook's docstring.
  • Loading branch information
Jacob Kim committed May 21, 2021
1 parent 85b2ccb commit aa4713e
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 8 deletions.
11 changes: 7 additions & 4 deletions airflow/providers/google/ads/hooks/ads.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
# under the License.
"""This module contains Google Ad hook."""
from tempfile import NamedTemporaryFile
from typing import IO, Any, Dict, Generator, List
from typing import IO, Any, Dict, Generator, List, Optional

try:
from functools import cached_property
Expand Down Expand Up @@ -69,22 +69,25 @@ class GoogleAdsHook(BaseHook):
:type gcp_conn_id: str
:param google_ads_conn_id: The connection ID with the details of Google Ads config.yaml file.
:type google_ads_conn_id: str
:param api_version: The Google Ads API version to use.
:type api_version: str
:return: list of Google Ads Row object(s)
:rtype: list[GoogleAdsRow]
"""

default_api_version = "v5"

def __init__(
self,
api_version: Optional[str],
gcp_conn_id: str = "google_cloud_default",
google_ads_conn_id: str = "google_ads_default",
api_version: str = "v3",
) -> None:
super().__init__()
self.api_version = api_version or self.default_api_version
self.gcp_conn_id = gcp_conn_id
self.google_ads_conn_id = google_ads_conn_id
self.gcp_conn_id = gcp_conn_id
self.api_version = api_version
self.google_ads_config: Dict[str, Any] = {}

@cached_property
Expand Down
10 changes: 9 additions & 1 deletion airflow/providers/google/ads/operators/ads.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ class GoogleAdsListAccountsOperator(BaseOperator):
Service Account Token Creator IAM role to the directly preceding identity, with first
account from the list granting this role to the originating account (templated).
:type impersonation_chain: Union[str, Sequence[str]]
:param api_version: Optional Google Ads API version to use.
:type api_version: Optional[str]
"""

template_fields = (
Expand All @@ -78,6 +80,7 @@ def __init__(
google_ads_conn_id: str = "google_ads_default",
gzip: bool = False,
impersonation_chain: Optional[Union[str, Sequence[str]]] = None,
api_version: Optional[str] = None,
**kwargs,
) -> None:
super().__init__(**kwargs)
Expand All @@ -87,11 +90,16 @@ def __init__(
self.google_ads_conn_id = google_ads_conn_id
self.gzip = gzip
self.impersonation_chain = impersonation_chain
self.api_version = api_version

def execute(self, context: dict) -> str:
uri = f"gs://{self.bucket}/{self.object_name}"

ads_hook = GoogleAdsHook(gcp_conn_id=self.gcp_conn_id, google_ads_conn_id=self.google_ads_conn_id)
ads_hook = GoogleAdsHook(
gcp_conn_id=self.gcp_conn_id,
google_ads_conn_id=self.google_ads_conn_id,
api_version=self.api_version,
)

gcs_hook = GCSHook(gcp_conn_id=self.gcp_conn_id, impersonation_chain=self.impersonation_chain)
with NamedTemporaryFile("w+") as temp_file:
Expand Down
10 changes: 9 additions & 1 deletion airflow/providers/google/ads/transfers/ads_to_gcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ class GoogleAdsToGcsOperator(BaseOperator):
Service Account Token Creator IAM role to the directly preceding identity, with first
account from the list granting this role to the originating account (templated).
:type impersonation_chain: Union[str, Sequence[str]]
:param api_version: Optional Google Ads API version to use.
:type api_version: Optional[str]
"""

template_fields = (
Expand All @@ -90,6 +92,7 @@ def __init__(
page_size: int = 10000,
gzip: bool = False,
impersonation_chain: Optional[Union[str, Sequence[str]]] = None,
api_version: Optional[str] = None,
**kwargs,
) -> None:
super().__init__(**kwargs)
Expand All @@ -103,9 +106,14 @@ def __init__(
self.page_size = page_size
self.gzip = gzip
self.impersonation_chain = impersonation_chain
self.api_version = api_version

def execute(self, context: dict) -> None:
service = GoogleAdsHook(gcp_conn_id=self.gcp_conn_id, google_ads_conn_id=self.google_ads_conn_id)
service = GoogleAdsHook(
gcp_conn_id=self.gcp_conn_id,
google_ads_conn_id=self.google_ads_conn_id,
api_version=self.api_version,
)
rows = service.search(client_ids=self.client_ids, query=self.query, page_size=self.page_size)

try:
Expand Down
8 changes: 7 additions & 1 deletion tests/providers/google/ads/operators/test_ads.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@

gcp_conn_id = "gcp_conn_id"
google_ads_conn_id = "google_ads_conn_id"
api_version = "v5"


class TestGoogleAdsListAccountsOperator:
Expand All @@ -58,10 +59,15 @@ def test_execute(self, mocks_csv_writer, mock_tempfile, mock_gcs_hook, mock_ads_
bucket=BUCKET,
task_id="run_operator",
impersonation_chain=IMPERSONATION_CHAIN,
api_version=api_version,
)
op.execute({})

mock_ads_hook.assert_called_once_with(gcp_conn_id=gcp_conn_id, google_ads_conn_id=google_ads_conn_id)
mock_ads_hook.assert_called_once_with(
gcp_conn_id=gcp_conn_id,
google_ads_conn_id=google_ads_conn_id,
api_version=api_version,
)
mock_gcs_hook.assert_called_once_with(
gcp_conn_id=gcp_conn_id,
impersonation_chain=IMPERSONATION_CHAIN,
Expand Down
8 changes: 7 additions & 1 deletion tests/providers/google/ads/transfers/test_ads_to_gcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
GCS_OBJ_PATH,
IMPERSONATION_CHAIN,
QUERY,
api_version,
gcp_conn_id,
google_ads_conn_id,
)
Expand All @@ -44,9 +45,14 @@ def test_execute(self, mock_gcs_hook, mock_ads_hook):
bucket=BUCKET,
task_id="run_operator",
impersonation_chain=IMPERSONATION_CHAIN,
api_version=api_version,
)
op.execute({})
mock_ads_hook.assert_called_once_with(gcp_conn_id=gcp_conn_id, google_ads_conn_id=google_ads_conn_id)
mock_ads_hook.assert_called_once_with(
gcp_conn_id=gcp_conn_id,
google_ads_conn_id=google_ads_conn_id,
api_version=api_version,
)
mock_ads_hook.return_value.search.assert_called_once_with(
client_ids=CLIENT_IDS, query=QUERY, page_size=10000
)
Expand Down

0 comments on commit aa4713e

Please sign in to comment.