Skip to content

Commit 2c81f0f

Browse files
committed
Fix reporting example
Change-Id: I3f5008600fa1345f8a35e8f9f0b46d1df95bfe57
1 parent c08f109 commit 2c81f0f

File tree

1 file changed

+30
-14
lines changed

1 file changed

+30
-14
lines changed

examples/reporting/parallel_report_download.py

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,31 @@
2323
from itertools import product
2424
import multiprocessing
2525
import time
26-
from typing import Any, Dict, Iterable, List, Tuple, Union
26+
from typing import Any, Dict, Iterable, List, Tuple
2727

2828
from google.ads.googleads.client import GoogleAdsClient
2929
from google.ads.googleads.errors import GoogleAdsException
30+
from google.ads.googleads.v20.errors.types import (
31+
ErrorLocation,
32+
GoogleAdsError,
33+
)
34+
from google.ads.googleads.v20.services.services.google_ads_service import (
35+
GoogleAdsServiceClient,
36+
)
37+
from google.ads.googleads.v20.services.types import (
38+
GoogleAdsRow,
39+
SearchGoogleAdsStreamResponse,
40+
)
3041

3142
# Maximum number of processes to spawn.
32-
MAX_PROCESSES = multiprocessing.cpu_count()
43+
MAX_PROCESSES: int = multiprocessing.cpu_count()
3344
# Timeout between retries in seconds.
34-
BACKOFF_FACTOR = 5
45+
BACKOFF_FACTOR: int = 5
3546
# Maximum number of retries for errors.
36-
MAX_RETRIES = 5
47+
MAX_RETRIES: int = 5
3748

3849

39-
def main(
40-
client: GoogleAdsClient, customer_ids: List[str]
41-
) -> None:
50+
def main(client: GoogleAdsClient, customer_ids: List[str]) -> None:
4251
"""The main method that creates all necessary entities for the example.
4352
4453
Args:
@@ -47,11 +56,11 @@ def main(
4756
"""
4857

4958
# Define the GAQL query strings to run for each customer ID.
50-
campaign_query = """
59+
campaign_query: str = """
5160
SELECT campaign.id, metrics.impressions, metrics.clicks
5261
FROM campaign
5362
WHERE segments.date DURING LAST_30_DAYS"""
54-
ad_group_query = """
63+
ad_group_query: str = """
5564
SELECT campaign.id, ad_group.id, metrics.impressions, metrics.clicks
5665
FROM ad_group
5766
WHERE segments.date DURING LAST_30_DAYS"""
@@ -69,6 +78,7 @@ def main(
6978
# Partition our results into successful and failed results.
7079
successes: List[Dict[str, Any]] = []
7180
failures: List[Dict[str, Any]] = []
81+
res: Tuple[bool, Dict[str, Any]]
7282
for res in results:
7383
if res[0]:
7484
successes.append(res[1])
@@ -82,24 +92,28 @@ def main(
8292
)
8393

8494
print("Successes:") if len(successes) else None
95+
success: Dict[str, Any]
8596
for success in successes:
8697
# success["results"] represents an array of result strings for one
8798
# customer ID / query combination.
88-
result_str = "\n".join(success["results"])
99+
result_str: str = "\n".join(success["results"])
89100
print(result_str)
90101

91102
print("Failures:") if len(failures) else None
103+
failure: Dict[str, Any]
92104
for failure in failures:
93-
ex = failure["exception"]
105+
ex: GoogleAdsException = failure["exception"]
94106
print(
95107
f'Request with ID "{ex.request_id}" failed with status '
96108
f'"{ex.error.code().name}" for customer_id '
97109
f'{failure["customer_id"]} and query "{failure["query"]}" and '
98110
"includes the following errors:"
99111
)
112+
error: GoogleAdsError
100113
for error in ex.failure.errors:
101114
print(f'\tError with message "{error.message}".')
102115
if error.location:
116+
field_path_element: ErrorLocation.FieldPathElement
103117
for (
104118
field_path_element
105119
) in error.location.field_path_elements:
@@ -118,20 +132,22 @@ def issue_search_request(
118132
customer_id: a client customer ID str.
119133
query: a GAQL query str.
120134
"""
121-
ga_service: Any = client.get_service("GoogleAdsService")
135+
ga_service: GoogleAdsServiceClient = client.get_service("GoogleAdsService")
122136
retry_count: int = 0
123137
# Retry until we've reached MAX_RETRIES or have successfully received a
124138
# response.
125139
while True:
126140
try:
127-
stream: Any = ga_service.search_stream(
128-
customer_id=customer_id, query=query
141+
stream: Iterable[SearchGoogleAdsStreamResponse] = (
142+
ga_service.search_stream(customer_id=customer_id, query=query)
129143
)
130144
# Returning a list of GoogleAdsRows will result in a
131145
# PicklingError, so instead we put the GoogleAdsRow data
132146
# into a list of str results and return that.
133147
result_strings: List[str] = []
148+
batch: SearchGoogleAdsStreamResponse
134149
for batch in stream:
150+
row: GoogleAdsRow
135151
for row in batch.results:
136152
ad_group_id: str = (
137153
f"Ad Group ID {row.ad_group.id} in "

0 commit comments

Comments
 (0)