Skip to content

Commit 35a3974

Browse files
authored
Add annotations to examples/error_handling (#948)
1 parent 306c6a6 commit 35a3974

File tree

4 files changed

+239
-107
lines changed

4 files changed

+239
-107
lines changed

examples/error_handling/handle_keyword_policy_violations.py

Lines changed: 47 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,25 @@
2727

2828
import argparse
2929
import sys
30+
from typing import Any, List, Optional, Tuple
3031

3132
from google.ads.googleads.client import GoogleAdsClient
3233
from google.ads.googleads.errors import GoogleAdsException
33-
34-
35-
def main(client, customer_id, ad_group_id, keyword_text):
34+
from google.ads.googleads.v20.services.services.ad_group_criterion_service import (
35+
AdGroupCriterionServiceClient,
36+
)
37+
from google.ads.googleads.v20.services.types.ad_group_criterion_service import (
38+
AdGroupCriterionOperation,
39+
)
40+
from google.ads.googleads.v20.common.types.policy import PolicyViolationKey
41+
42+
43+
def main(
44+
client: GoogleAdsClient,
45+
customer_id: str,
46+
ad_group_id: str,
47+
keyword_text: str,
48+
) -> None:
3649
"""Demonstrates how to request an exemption for keyword policy violations.
3750
3851
Args:
@@ -42,8 +55,12 @@ def main(client, customer_id, ad_group_id, keyword_text):
4255
keyword_text: The keyword text to add.
4356
"""
4457

45-
ad_group_criterion_service = client.get_service("AdGroupCriterionService")
58+
ad_group_criterion_service: AdGroupCriterionServiceClient = (
59+
client.get_service("AdGroupCriterionService")
60+
)
4661

62+
googleads_exception: Optional[GoogleAdsException]
63+
ad_group_criterion_operation: AdGroupCriterionOperation
4764
(
4865
googleads_exception,
4966
ad_group_criterion_operation,
@@ -60,8 +77,8 @@ def main(client, customer_id, ad_group_id, keyword_text):
6077
# your keyword contains many policy violations, but not all of them are
6178
# exemptible, the request will not be sent.
6279
if googleads_exception is not None:
63-
exempt_policy_violation_keys = fetch_exempt_policy_violation_keys(
64-
googleads_exception
80+
exempt_policy_violation_keys: List[PolicyViolationKey] = (
81+
fetch_exempt_policy_violation_keys(googleads_exception)
6582
)
6683
request_exemption(
6784
customer_id,
@@ -83,8 +100,12 @@ def main(client, customer_id, ad_group_id, keyword_text):
83100

84101

85102
def create_keyword_criterion(
86-
client, ad_group_criterion_service, customer_id, ad_group_id, keyword_text
87-
):
103+
client: GoogleAdsClient,
104+
ad_group_criterion_service: AdGroupCriterionServiceClient,
105+
customer_id: str,
106+
ad_group_id: str,
107+
keyword_text: str,
108+
) -> Tuple[Optional[GoogleAdsException], AdGroupCriterionOperation]:
88109
"""Attempts to add a keyword criterion to an ad group.
89110
90111
Args:
@@ -99,8 +120,10 @@ def create_keyword_criterion(
99120
successful) and the modified operation.
100121
"""
101122
# Constructs an ad group criterion using the keyword text provided.
102-
ad_group_criterion_operation = client.get_type("AdGroupCriterionOperation")
103-
ad_group_criterion = ad_group_criterion_operation.create
123+
ad_group_criterion_operation: AdGroupCriterionOperation = client.get_type(
124+
"AdGroupCriterionOperation"
125+
)
126+
ad_group_criterion: Any = ad_group_criterion_operation.create
104127
ad_group_criterion.ad_group = client.get_service(
105128
"AdGroupService"
106129
).ad_group_path(customer_id, ad_group_id)
@@ -112,7 +135,7 @@ def create_keyword_criterion(
112135

113136
try:
114137
# Try sending a mutate request to add the keyword.
115-
response = ad_group_criterion_service.mutate_ad_group_criteria(
138+
response: Any = ad_group_criterion_service.mutate_ad_group_criteria(
116139
customer_id=customer_id, operations=[ad_group_criterion_operation]
117140
)
118141
except GoogleAdsException as googleads_exception:
@@ -129,7 +152,9 @@ def create_keyword_criterion(
129152

130153

131154
# [START handle_keyword_policy_violations]
132-
def fetch_exempt_policy_violation_keys(googleads_exception):
155+
def fetch_exempt_policy_violation_keys(
156+
googleads_exception: GoogleAdsException,
157+
) -> List[PolicyViolationKey]:
133158
"""Collects all policy violation keys that can be exempted.
134159
135160
Args:
@@ -138,7 +163,7 @@ def fetch_exempt_policy_violation_keys(googleads_exception):
138163
Returns:
139164
A list of policy violation keys.
140165
"""
141-
exempt_policy_violation_keys = []
166+
exempt_policy_violation_keys: List[PolicyViolationKey] = []
142167

143168
print("Google Ads failure details:")
144169
for error in googleads_exception.failure.errors:
@@ -191,11 +216,11 @@ def fetch_exempt_policy_violation_keys(googleads_exception):
191216

192217
# [START handle_keyword_policy_violations_1]
193218
def request_exemption(
194-
customer_id,
195-
ad_group_criterion_service,
196-
ad_group_criterion_operation,
197-
exempt_policy_violation_keys,
198-
):
219+
customer_id: str,
220+
ad_group_criterion_service: AdGroupCriterionServiceClient,
221+
ad_group_criterion_operation: AdGroupCriterionOperation,
222+
exempt_policy_violation_keys: List[PolicyViolationKey],
223+
) -> None:
199224
"""Sends exemption requests for creating a keyword.
200225
201226
Args:
@@ -212,7 +237,7 @@ def request_exemption(
212237
ad_group_criterion_operation.exempt_policy_violation_keys.extend(
213238
exempt_policy_violation_keys
214239
)
215-
response = ad_group_criterion_service.mutate_ad_group_criteria(
240+
response: Any = ad_group_criterion_service.mutate_ad_group_criteria(
216241
customer_id=customer_id, operations=[ad_group_criterion_operation]
217242
)
218243
print(
@@ -256,7 +281,9 @@ def request_exemption(
256281

257282
# GoogleAdsClient will read the google-ads.yaml configuration file in the
258283
# home directory if none is specified.
259-
googleads_client = GoogleAdsClient.load_from_storage(version="v20")
284+
googleads_client: GoogleAdsClient = GoogleAdsClient.load_from_storage(
285+
version="v20"
286+
)
260287

261288
main(
262289
googleads_client, args.customer_id, args.ad_group_id, args.keyword_text

examples/error_handling/handle_partial_failure.py

Lines changed: 52 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,24 @@
1818
import argparse
1919
import sys
2020
import uuid
21+
from typing import Any, List
2122

2223
from google.ads.googleads.client import GoogleAdsClient
2324
from google.ads.googleads.errors import GoogleAdsException
24-
25-
26-
def main(client, customer_id, campaign_id):
25+
from google.ads.googleads.v20.services.services.ad_group_service import (
26+
AdGroupServiceClient,
27+
)
28+
from google.ads.googleads.v20.services.services.campaign_service import (
29+
CampaignServiceClient,
30+
)
31+
from google.ads.googleads.v20.services.types.ad_group_service import (
32+
AdGroupOperation,
33+
MutateAdGroupsResponse,
34+
MutateAdGroupsRequest,
35+
)
36+
37+
38+
def main(client: GoogleAdsClient, customer_id: str, campaign_id: str) -> None:
2739
"""Runs the example code, which demonstrates how to handle partial failures.
2840
2941
The example creates three Ad Groups, two of which intentionally fail in
@@ -36,7 +48,9 @@ def main(client, customer_id, campaign_id):
3648
campaign_id: The ID for a campaign to create Ad Groups under.
3749
"""
3850
try:
39-
ad_group_response = create_ad_groups(client, customer_id, campaign_id)
51+
ad_group_response: MutateAdGroupsResponse = create_ad_groups(
52+
client, customer_id, campaign_id
53+
)
4054
except GoogleAdsException as ex:
4155
print(
4256
f'Request with ID "{ex.request_id}" failed with status '
@@ -53,7 +67,9 @@ def main(client, customer_id, campaign_id):
5367

5468

5569
# [START handle_partial_failure]
56-
def create_ad_groups(client, customer_id, campaign_id):
70+
def create_ad_groups(
71+
client: GoogleAdsClient, customer_id: str, campaign_id: str
72+
) -> MutateAdGroupsResponse:
5773
"""Creates three Ad Groups, two of which intentionally generate errors.
5874
5975
Args:
@@ -63,35 +79,41 @@ def create_ad_groups(client, customer_id, campaign_id):
6379
6480
Returns: A MutateAdGroupsResponse message instance.
6581
"""
66-
ad_group_service = client.get_service("AdGroupService")
67-
campaign_service = client.get_service("CampaignService")
68-
resource_name = campaign_service.campaign_path(customer_id, campaign_id)
82+
ad_group_service: AdGroupServiceClient = client.get_service(
83+
"AdGroupService"
84+
)
85+
campaign_service: CampaignServiceClient = client.get_service(
86+
"CampaignService"
87+
)
88+
resource_name: str = campaign_service.campaign_path(
89+
customer_id, campaign_id
90+
)
6991

70-
invalid_resource_name = campaign_service.campaign_path(customer_id, 0)
71-
ad_group_operations = []
92+
invalid_resource_name: str = campaign_service.campaign_path(customer_id, 0)
93+
ad_group_operations: List[AdGroupOperation] = []
7294

7395
# This AdGroup should be created successfully - assuming the campaign in
7496
# the params exists.
75-
ad_group_op1 = client.get_type("AdGroupOperation")
97+
ad_group_op1: AdGroupOperation = client.get_type("AdGroupOperation")
7698
ad_group_op1.create.name = f"Valid AdGroup: {uuid.uuid4()}"
7799
ad_group_op1.create.campaign = resource_name
78100
ad_group_operations.append(ad_group_op1)
79101

80102
# This AdGroup will always fail - campaign ID 0 in resource names is
81103
# never valid.
82-
ad_group_op2 = client.get_type("AdGroupOperation")
104+
ad_group_op2: AdGroupOperation = client.get_type("AdGroupOperation")
83105
ad_group_op2.create.name = f"Broken AdGroup: {uuid.uuid4()}"
84106
ad_group_op2.create.campaign = invalid_resource_name
85107
ad_group_operations.append(ad_group_op2)
86108

87109
# This AdGroup will always fail - duplicate ad group names are not allowed.
88-
ad_group_op3 = client.get_type("AdGroupOperation")
110+
ad_group_op3: AdGroupOperation = client.get_type("AdGroupOperation")
89111
ad_group_op3.create.name = ad_group_op1.create.name
90112
ad_group_op3.create.campaign = resource_name
91113
ad_group_operations.append(ad_group_op3)
92114

93115
# Issue a mutate request, setting partial_failure=True.
94-
request = client.get_type("MutateAdGroupsRequest")
116+
request: MutateAdGroupsRequest = client.get_type("MutateAdGroupsRequest")
95117
request.customer_id = customer_id
96118
request.operations = ad_group_operations
97119
request.partial_failure = True
@@ -100,7 +122,7 @@ def create_ad_groups(client, customer_id, campaign_id):
100122

101123

102124
# [START handle_partial_failure_1]
103-
def is_partial_failure_error_present(response):
125+
def is_partial_failure_error_present(response: MutateAdGroupsResponse) -> bool:
104126
"""Checks whether a response message has a partial failure error.
105127
106128
In Python the partial_failure_error attr is always present on a response
@@ -115,14 +137,16 @@ def is_partial_failure_error_present(response):
115137
Returns: A boolean, whether or not the response message has a partial
116138
failure error.
117139
"""
118-
partial_failure = getattr(response, "partial_failure_error", None)
119-
code = getattr(partial_failure, "code", None)
140+
partial_failure: Any = getattr(response, "partial_failure_error", None)
141+
code: int = int(getattr(partial_failure, "code", 0)) # Default to 0 if None
120142
return code != 0
121143
# [END handle_partial_failure_1]
122144

123145

124146
# [START handle_partial_failure_2]
125-
def print_results(client, response):
147+
def print_results(
148+
client: GoogleAdsClient, response: MutateAdGroupsResponse
149+
) -> None:
126150
"""Prints partial failure errors and success messages from a response.
127151
128152
This function shows how to retrieve partial_failure errors from a response
@@ -163,17 +187,19 @@ def print_results(client, response):
163187
if is_partial_failure_error_present(response):
164188
print("Partial failures occurred. Details will be shown below.\n")
165189
# Prints the details of the partial failure errors.
166-
partial_failure = getattr(response, "partial_failure_error", None)
190+
partial_failure: Any = getattr(response, "partial_failure_error", None)
167191
# partial_failure_error.details is a repeated field and iterable
168-
error_details = getattr(partial_failure, "details", [])
192+
error_details: List[Any] = getattr(partial_failure, "details", [])
169193

170194
for error_detail in error_details:
171195
# Retrieve an instance of the GoogleAdsFailure class from the client
172-
failure_message = client.get_type("GoogleAdsFailure")
196+
failure_message: Any = client.get_type("GoogleAdsFailure")
173197
# Parse the string into a GoogleAdsFailure message instance.
174198
# To access class-only methods on the message we retrieve its type.
175-
GoogleAdsFailure = type(failure_message)
176-
failure_object = GoogleAdsFailure.deserialize(error_detail.value)
199+
GoogleAdsFailure: Any = type(failure_message)
200+
failure_object: Any = GoogleAdsFailure.deserialize(
201+
error_detail.value
202+
)
177203

178204
for error in failure_object.errors:
179205
# Construct and print a string that details which element in
@@ -222,6 +248,8 @@ def print_results(client, response):
222248

223249
# GoogleAdsClient will read the google-ads.yaml configuration file in the
224250
# home directory if none is specified.
225-
googleads_client = GoogleAdsClient.load_from_storage(version="v20")
251+
googleads_client: GoogleAdsClient = GoogleAdsClient.load_from_storage(
252+
version="v20"
253+
)
226254

227255
main(googleads_client, args.customer_id, args.campaign_id)

0 commit comments

Comments
 (0)