3030import hashlib
3131import sys
3232import uuid
33- from typing import List , Dict , Any , Optional
33+ from typing import List , Dict , Optional , Union , Iterable
34+
35+ from google .protobuf .any_pb2 import Any
36+ from google .rpc import status_pb2
3437
3538from google .ads .googleads .client import GoogleAdsClient
3639from google .ads .googleads .errors import GoogleAdsException
37- from google .ads .googleads .v20 .services .types .google_ads_service import (
40+ from google .ads .googleads .v20 .services .services .google_ads_service import (
3841 GoogleAdsServiceClient ,
42+ )
43+ from google .ads .googleads .v20 .services .services .user_list_service import (
44+ UserListServiceClient ,
45+ )
46+ from google .ads .googleads .v20 .services .services .offline_user_data_job_service import (
47+ OfflineUserDataJobServiceClient ,
48+ )
49+ from google .ads .googleads .v20 .services .types .google_ads_service import (
3950 SearchGoogleAdsStreamResponse ,
4051)
4152from google .ads .googleads .v20 .services .types .user_list_service import (
4253 UserListOperation ,
43- UserListServiceClient ,
54+ MutateUserListsResponse ,
4455)
4556from google .ads .googleads .v20 .resources .types .user_list import UserList
4657from google .ads .googleads .v20 .services .types .offline_user_data_job_service import (
58+ AddOfflineUserDataJobOperationsRequest ,
4759 OfflineUserDataJobOperation ,
48- OfflineUserDataJobServiceClient ,
4960 AddOfflineUserDataJobOperationsResponse ,
61+ CreateOfflineUserDataJobResponse ,
5062)
5163from google .ads .googleads .v20 .resources .types .offline_user_data_job import (
5264 OfflineUserDataJob ,
5365)
66+ from google .ads .googleads .v20 .common .types .criteria import (
67+ AddressInfo ,
68+ )
5469from google .ads .googleads .v20 .common .types .offline_user_data import (
5570 UserData ,
5671 UserIdentifier ,
57- AddressInfo ,
72+ )
73+ from google .ads .googleads .v20 .errors .types .errors import (
74+ GoogleAdsFailure ,
75+ GoogleAdsError ,
5876)
5977
6078
@@ -151,8 +169,10 @@ def create_customer_match_user_list(
151169 # Sets the membership life span to 30 days.
152170 user_list .membership_life_span = 30
153171
154- response : Any = user_list_service_client .mutate_user_lists (
155- customer_id = customer_id , operations = [user_list_operation ]
172+ response : MutateUserListsResponse = (
173+ user_list_service_client .mutate_user_lists (
174+ customer_id = customer_id , operations = [user_list_operation ]
175+ )
156176 )
157177 user_list_resource_name : str = response .results [0 ].resource_name
158178 print (
@@ -227,10 +247,10 @@ def add_users_to_customer_match_user_list(
227247 ]
228248
229249 # Issues a request to create an offline user data job.
230- create_offline_user_data_job_response : Any = (
231- offline_user_data_job_service_client . create_offline_user_data_job (
232- customer_id = customer_id , job = offline_user_data_job
233- )
250+ create_offline_user_data_job_response : (
251+ CreateOfflineUserDataJobResponse
252+ ) = offline_user_data_job_service_client . create_offline_user_data_job (
253+ customer_id = customer_id , job = offline_user_data_job
234254 )
235255 offline_user_data_job_resource_name = (
236256 create_offline_user_data_job_response .resource_name
@@ -249,34 +269,45 @@ def add_users_to_customer_match_user_list(
249269 # https://developers.google.com/google-ads/api/docs/remarketing/audience-types/customer-match#customer_match_considerations
250270 # and https://developers.google.com/google-ads/api/docs/best-practices/quotas#user_data
251271 # for more information on the per-request limits.
252- request : Any = client .get_type ("AddOfflineUserDataJobOperationsRequest" )
272+ request : AddOfflineUserDataJobOperationsRequest = client .get_type (
273+ "AddOfflineUserDataJobOperationsRequest"
274+ )
253275 request .resource_name = offline_user_data_job_resource_name
254276 request .operations = build_offline_user_data_job_operations (client )
255277 request .enable_partial_failure = True
256278
257279 # Issues a request to add the operations to the offline user data job.
258- response : AddOfflineUserDataJobOperationsResponse = offline_user_data_job_service_client .add_offline_user_data_job_operations (
259- request = request
280+ response : AddOfflineUserDataJobOperationsResponse = (
281+ offline_user_data_job_service_client .add_offline_user_data_job_operations (
282+ request = request
283+ )
260284 )
261285
262286 # Prints the status message if any partial failure error is returned.
263287 # Note: the details of each partial failure error are not printed here.
264288 # Refer to the error_handling/handle_partial_failure.py example to learn
265289 # more.
266290 # Extracts the partial failure from the response status.
267- partial_failure : Any = getattr (response , "partial_failure_error" , None )
291+ partial_failure : Union [status_pb2 .Status , None ] = getattr (
292+ response , "partial_failure_error" , None
293+ )
268294 if getattr (partial_failure , "code" , None ) != 0 :
269- error_details : Any = getattr (partial_failure , "details" , [])
295+ error_details : Iterable [Any , None ] = getattr (
296+ partial_failure , "details" , []
297+ )
270298 for error_detail in error_details :
271- failure_message : Any = client .get_type ("GoogleAdsFailure" )
299+ failure_message : GoogleAdsFailure = client .get_type (
300+ "GoogleAdsFailure"
301+ )
272302 # Retrieve the class definition of the GoogleAdsFailure instance
273303 # in order to use the "deserialize" class method to parse the
274304 # error_detail string into a protobuf message object.
275- failure_object : Any = type (failure_message ).deserialize (
276- error_detail .value
277- )
305+ failure_object : GoogleAdsFailure = type (
306+ failure_message
307+ ).deserialize (error_detail .value )
308+ errors : Iterable [GoogleAdsError ] = failure_object .errors
278309
279- for error in failure_object . errors :
310+ for error in errors :
280311 print (
281312 "A partial failure at index "
282313 f"{ error .location .field_path_elements [0 ].index } occurred.\n "
@@ -634,10 +665,18 @@ def normalize_and_hash(s: str, remove_all_whitespace: bool) -> str:
634665 # used for help text, so it's fine if it fails.
635666 try :
636667 consent_status_enum_names = [
637- e .name for e in GoogleAdsClient .load_from_storage (version = "v20" ).enums .ConsentStatusEnum
668+ e .name
669+ for e in GoogleAdsClient .load_from_storage (
670+ version = "v20"
671+ ).enums .ConsentStatusEnum
638672 ]
639673 except Exception :
640- consent_status_enum_names = ["UNSPECIFIED" , "UNKNOWN" , "GRANTED" , "DENIED" ]
674+ consent_status_enum_names = [
675+ "UNSPECIFIED" ,
676+ "UNKNOWN" ,
677+ "GRANTED" ,
678+ "DENIED" ,
679+ ]
641680 parser .add_argument (
642681 "-d" ,
643682 "--ad_user_data_consent" ,
0 commit comments