Skip to content

Commit aa4713e

Browse files
author
Jacob Kim
authored
Use api version only in GoogleAdsHook not operators (#15266)
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.
1 parent 85b2ccb commit aa4713e

File tree

5 files changed

+39
-8
lines changed

5 files changed

+39
-8
lines changed

airflow/providers/google/ads/hooks/ads.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
# under the License.
1818
"""This module contains Google Ad hook."""
1919
from tempfile import NamedTemporaryFile
20-
from typing import IO, Any, Dict, Generator, List
20+
from typing import IO, Any, Dict, Generator, List, Optional
2121

2222
try:
2323
from functools import cached_property
@@ -69,22 +69,25 @@ class GoogleAdsHook(BaseHook):
6969
:type gcp_conn_id: str
7070
:param google_ads_conn_id: The connection ID with the details of Google Ads config.yaml file.
7171
:type google_ads_conn_id: str
72+
:param api_version: The Google Ads API version to use.
73+
:type api_version: str
7274
7375
:return: list of Google Ads Row object(s)
7476
:rtype: list[GoogleAdsRow]
7577
"""
7678

79+
default_api_version = "v5"
80+
7781
def __init__(
7882
self,
83+
api_version: Optional[str],
7984
gcp_conn_id: str = "google_cloud_default",
8085
google_ads_conn_id: str = "google_ads_default",
81-
api_version: str = "v3",
8286
) -> None:
8387
super().__init__()
88+
self.api_version = api_version or self.default_api_version
8489
self.gcp_conn_id = gcp_conn_id
8590
self.google_ads_conn_id = google_ads_conn_id
86-
self.gcp_conn_id = gcp_conn_id
87-
self.api_version = api_version
8891
self.google_ads_config: Dict[str, Any] = {}
8992

9093
@cached_property

airflow/providers/google/ads/operators/ads.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ class GoogleAdsListAccountsOperator(BaseOperator):
6161
Service Account Token Creator IAM role to the directly preceding identity, with first
6262
account from the list granting this role to the originating account (templated).
6363
:type impersonation_chain: Union[str, Sequence[str]]
64+
:param api_version: Optional Google Ads API version to use.
65+
:type api_version: Optional[str]
6466
"""
6567

6668
template_fields = (
@@ -78,6 +80,7 @@ def __init__(
7880
google_ads_conn_id: str = "google_ads_default",
7981
gzip: bool = False,
8082
impersonation_chain: Optional[Union[str, Sequence[str]]] = None,
83+
api_version: Optional[str] = None,
8184
**kwargs,
8285
) -> None:
8386
super().__init__(**kwargs)
@@ -87,11 +90,16 @@ def __init__(
8790
self.google_ads_conn_id = google_ads_conn_id
8891
self.gzip = gzip
8992
self.impersonation_chain = impersonation_chain
93+
self.api_version = api_version
9094

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

94-
ads_hook = GoogleAdsHook(gcp_conn_id=self.gcp_conn_id, google_ads_conn_id=self.google_ads_conn_id)
98+
ads_hook = GoogleAdsHook(
99+
gcp_conn_id=self.gcp_conn_id,
100+
google_ads_conn_id=self.google_ads_conn_id,
101+
api_version=self.api_version,
102+
)
95103

96104
gcs_hook = GCSHook(gcp_conn_id=self.gcp_conn_id, impersonation_chain=self.impersonation_chain)
97105
with NamedTemporaryFile("w+") as temp_file:

airflow/providers/google/ads/transfers/ads_to_gcs.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ class GoogleAdsToGcsOperator(BaseOperator):
6666
Service Account Token Creator IAM role to the directly preceding identity, with first
6767
account from the list granting this role to the originating account (templated).
6868
:type impersonation_chain: Union[str, Sequence[str]]
69+
:param api_version: Optional Google Ads API version to use.
70+
:type api_version: Optional[str]
6971
"""
7072

7173
template_fields = (
@@ -90,6 +92,7 @@ def __init__(
9092
page_size: int = 10000,
9193
gzip: bool = False,
9294
impersonation_chain: Optional[Union[str, Sequence[str]]] = None,
95+
api_version: Optional[str] = None,
9396
**kwargs,
9497
) -> None:
9598
super().__init__(**kwargs)
@@ -103,9 +106,14 @@ def __init__(
103106
self.page_size = page_size
104107
self.gzip = gzip
105108
self.impersonation_chain = impersonation_chain
109+
self.api_version = api_version
106110

107111
def execute(self, context: dict) -> None:
108-
service = GoogleAdsHook(gcp_conn_id=self.gcp_conn_id, google_ads_conn_id=self.google_ads_conn_id)
112+
service = GoogleAdsHook(
113+
gcp_conn_id=self.gcp_conn_id,
114+
google_ads_conn_id=self.google_ads_conn_id,
115+
api_version=self.api_version,
116+
)
109117
rows = service.search(client_ids=self.client_ids, query=self.query, page_size=self.page_size)
110118

111119
try:

tests/providers/google/ads/operators/test_ads.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737

3838
gcp_conn_id = "gcp_conn_id"
3939
google_ads_conn_id = "google_ads_conn_id"
40+
api_version = "v5"
4041

4142

4243
class TestGoogleAdsListAccountsOperator:
@@ -58,10 +59,15 @@ def test_execute(self, mocks_csv_writer, mock_tempfile, mock_gcs_hook, mock_ads_
5859
bucket=BUCKET,
5960
task_id="run_operator",
6061
impersonation_chain=IMPERSONATION_CHAIN,
62+
api_version=api_version,
6163
)
6264
op.execute({})
6365

64-
mock_ads_hook.assert_called_once_with(gcp_conn_id=gcp_conn_id, google_ads_conn_id=google_ads_conn_id)
66+
mock_ads_hook.assert_called_once_with(
67+
gcp_conn_id=gcp_conn_id,
68+
google_ads_conn_id=google_ads_conn_id,
69+
api_version=api_version,
70+
)
6571
mock_gcs_hook.assert_called_once_with(
6672
gcp_conn_id=gcp_conn_id,
6773
impersonation_chain=IMPERSONATION_CHAIN,

tests/providers/google/ads/transfers/test_ads_to_gcs.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
GCS_OBJ_PATH,
2626
IMPERSONATION_CHAIN,
2727
QUERY,
28+
api_version,
2829
gcp_conn_id,
2930
google_ads_conn_id,
3031
)
@@ -44,9 +45,14 @@ def test_execute(self, mock_gcs_hook, mock_ads_hook):
4445
bucket=BUCKET,
4546
task_id="run_operator",
4647
impersonation_chain=IMPERSONATION_CHAIN,
48+
api_version=api_version,
4749
)
4850
op.execute({})
49-
mock_ads_hook.assert_called_once_with(gcp_conn_id=gcp_conn_id, google_ads_conn_id=google_ads_conn_id)
51+
mock_ads_hook.assert_called_once_with(
52+
gcp_conn_id=gcp_conn_id,
53+
google_ads_conn_id=google_ads_conn_id,
54+
api_version=api_version,
55+
)
5056
mock_ads_hook.return_value.search.assert_called_once_with(
5157
client_ids=CLIENT_IDS, query=QUERY, page_size=10000
5258
)

0 commit comments

Comments
 (0)