2424from uuid import uuid4
2525from typing import Any , List , Coroutine
2626
27+ from google .api_core .operation import Operation
28+
2729from google .ads .googleads .client import GoogleAdsClient
2830from google .ads .googleads .errors import GoogleAdsException
2931from google .ads .googleads .v20 .services .services .batch_job_service import (
3537 ListBatchJobResultsRequest ,
3638 ListBatchJobResultsResponse ,
3739)
38- from google .ads .googleads .v20 .services .types .common import MutateOperation
40+ from google .ads .googleads .v20 .services .types .google_ads_service import (
41+ MutateOperation ,
42+ )
3943from google .ads .googleads .v20 .resources .types .batch_job import BatchJob
4044from google .ads .googleads .v20 .services .types .campaign_budget_service import (
4145 CampaignBudgetOperation ,
4246)
43- from google .ads .googleads .v20 .services .types .campaign_service import CampaignOperation
47+ from google .ads .googleads .v20 .services .types .campaign_service import (
48+ CampaignOperation ,
49+ )
4450from google .ads .googleads .v20 .services .types .campaign_criterion_service import (
4551 CampaignCriterionOperation ,
4652)
47- from google .ads .googleads .v20 .services .types .ad_group_service import AdGroupOperation
53+ from google .ads .googleads .v20 .services .types .ad_group_service import (
54+ AdGroupOperation ,
55+ )
4856from google .ads .googleads .v20 .services .types .ad_group_criterion_service import (
4957 AdGroupCriterionOperation ,
5058)
5159from google .ads .googleads .v20 .services .types .ad_group_ad_service import (
5260 AdGroupAdOperation ,
5361)
54- from google .ads .googleads .v20 .services .types .batch_job_service import BatchJobOperation
55- from google .api_core .operation import Operation
62+ from google .ads .googleads .v20 .services .types .batch_job_service import (
63+ BatchJobOperation ,
64+ )
65+
5666
5767NUMBER_OF_CAMPAIGNS_TO_ADD : int = 2
5868NUMBER_OF_AD_GROUPS_TO_ADD : int = 2
@@ -101,14 +111,20 @@ async def main(client: GoogleAdsClient, customer_id: str) -> None:
101111 client: an initialized GoogleAdsClient instance.
102112 customer_id: a str of a customer ID.
103113 """
104- batch_job_service : BatchJobServiceClient = client .get_service ("BatchJobService" )
114+ batch_job_service : BatchJobServiceClient = client .get_service (
115+ "BatchJobService"
116+ )
105117 batch_job_operation : BatchJobOperation = create_batch_job_operation (client )
106118 resource_name : str = create_batch_job (
107119 batch_job_service , customer_id , batch_job_operation
108120 )
109- operations : List [MutateOperation ] = build_all_operations (client , customer_id )
121+ operations : List [MutateOperation ] = build_all_operations (
122+ client , customer_id
123+ )
110124 add_all_batch_job_operations (batch_job_service , operations , resource_name )
111- operations_response : Operation = run_batch_job (batch_job_service , resource_name )
125+ operations_response : Operation = run_batch_job (
126+ batch_job_service , resource_name
127+ )
112128
113129 # Create an asyncio.Event instance to control execution during the
114130 # asynchronous steps in _poll_batch_job. Note that this is not important
@@ -135,7 +151,9 @@ def create_batch_job_operation(client: GoogleAdsClient) -> BatchJobOperation:
135151 Returns: a BatchJobOperation with a BatchJob instance set in the "create"
136152 property.
137153 """
138- batch_job_operation : BatchJobOperation = client .get_type ("BatchJobOperation" )
154+ batch_job_operation : BatchJobOperation = client .get_type (
155+ "BatchJobOperation"
156+ )
139157 batch_job : BatchJob = client .get_type ("BatchJob" )
140158 client .copy_from (batch_job_operation .create , batch_job )
141159 return batch_job_operation
@@ -198,7 +216,8 @@ def add_all_batch_job_operations(
198216 )
199217
200218 print (
201- f"{ response .total_operations } mutate operations have been " "added so far."
219+ f"{ response .total_operations } mutate operations have been "
220+ "added so far."
202221 )
203222
204223 # You can use this next sequence token for calling
@@ -227,11 +246,13 @@ def build_all_operations(
227246
228247 # Creates a new campaign budget operation and adds it to the list of
229248 # mutate operations.
230- campaign_budget_op : CampaignBudgetOperation = build_campaign_budget_operation (
231- client , customer_id
249+ campaign_budget_op : CampaignBudgetOperation = (
250+ build_campaign_budget_operation ( client , customer_id )
232251 )
233252 operations .append (
234- build_mutate_operation (client , "campaign_budget_operation" , campaign_budget_op )
253+ build_mutate_operation (
254+ client , "campaign_budget_operation" , campaign_budget_op
255+ )
235256 )
236257
237258 # Creates new campaign operations and adds them to the list of
@@ -250,7 +271,9 @@ def build_all_operations(
250271 build_campaign_criterion_operations (client , campaign_operations )
251272 )
252273 operations .extend (
253- build_mutate_operation (client , "campaign_criterion_operation" , operation )
274+ build_mutate_operation (
275+ client , "campaign_criterion_operation" , operation
276+ )
254277 for operation in campaign_criterion_operations
255278 )
256279
@@ -270,14 +293,16 @@ def build_all_operations(
270293 build_ad_group_criterion_operations (client , ad_group_operations )
271294 )
272295 operations .extend (
273- build_mutate_operation (client , "ad_group_criterion_operation" , operation )
296+ build_mutate_operation (
297+ client , "ad_group_criterion_operation" , operation
298+ )
274299 for operation in ad_group_criterion_operations
275300 )
276301
277302 # Creates new ad group ad operations and adds them to the list of
278303 # mutate operations.
279- ad_group_ad_operations : List [AdGroupAdOperation ] = build_ad_group_ad_operations (
280- client , ad_group_operations
304+ ad_group_ad_operations : List [AdGroupAdOperation ] = (
305+ build_ad_group_ad_operations ( client , ad_group_operations )
281306 )
282307 operations .extend (
283308 build_mutate_operation (client , "ad_group_ad_operation" , operation )
@@ -308,14 +333,18 @@ def build_campaign_budget_operation(
308333 )
309334 campaign_budget .resource_name = resource_name
310335 campaign_budget .name = f"Interplanetary Cruise Budget #{ uuid4 ()} "
311- campaign_budget .delivery_method = client .enums .BudgetDeliveryMethodEnum .STANDARD
336+ campaign_budget .delivery_method = (
337+ client .enums .BudgetDeliveryMethodEnum .STANDARD
338+ )
312339 campaign_budget .amount_micros = 5000000
313340
314341 return campaign_budget_operation
315342
316343
317344def build_campaign_operations (
318- client : GoogleAdsClient , customer_id : str , campaign_budget_resource_name : str
345+ client : GoogleAdsClient ,
346+ customer_id : str ,
347+ campaign_budget_resource_name : str ,
319348) -> List [CampaignOperation ]:
320349 """Builds new campaign operations for the specified customer ID.
321350
@@ -328,13 +357,17 @@ def build_campaign_operations(
328357 Returns: a list of CampaignOperation instances.
329358 """
330359 return [
331- build_campaign_operation (client , customer_id , campaign_budget_resource_name )
360+ build_campaign_operation (
361+ client , customer_id , campaign_budget_resource_name
362+ )
332363 for _ in range (NUMBER_OF_CAMPAIGNS_TO_ADD )
333364 ]
334365
335366
336367def build_campaign_operation (
337- client : GoogleAdsClient , customer_id : str , campaign_budget_resource_name : str
368+ client : GoogleAdsClient ,
369+ customer_id : str ,
370+ campaign_budget_resource_name : str ,
338371) -> CampaignOperation :
339372 """Builds new campaign operation for the specified customer ID.
340373
@@ -352,9 +385,13 @@ def build_campaign_operation(
352385 campaign = campaign_operation .create
353386 campaign_id : int = get_next_temporary_id ()
354387 # Creates a resource name using the temporary ID.
355- campaign .resource_name = campaign_service .campaign_path (customer_id , campaign_id )
388+ campaign .resource_name = campaign_service .campaign_path (
389+ customer_id , campaign_id
390+ )
356391 campaign .name = f"Batch job campaign #{ customer_id } .{ campaign_id } "
357- campaign .advertising_channel_type = client .enums .AdvertisingChannelTypeEnum .SEARCH
392+ campaign .advertising_channel_type = (
393+ client .enums .AdvertisingChannelTypeEnum .SEARCH
394+ )
358395 # Recommendation: Set the campaign to PAUSED when creating it to prevent
359396 # the ads from immediately serving. Set to ENABLED once you've added
360397 # targeting and the ads are ready to serve.
@@ -401,7 +438,9 @@ def build_campaign_criterion_operation(
401438 # Creates a campaign criterion.
402439 campaign_criterion = campaign_criterion_operation .create
403440 campaign_criterion .keyword .text = "venus"
404- campaign_criterion .keyword .match_type = client .enums .KeywordMatchTypeEnum .BROAD
441+ campaign_criterion .keyword .match_type = (
442+ client .enums .KeywordMatchTypeEnum .BROAD
443+ )
405444 # Sets the campaign criterion as a negative criterion.
406445 campaign_criterion .negative = True
407446 campaign_criterion .campaign = campaign_operation .create .resource_name
@@ -428,14 +467,18 @@ def build_ad_group_operations(
428467 for campaign_operation in campaign_operations :
429468 for _ in range (NUMBER_OF_AD_GROUPS_TO_ADD ):
430469 operations .append (
431- build_ad_group_operation (client , customer_id , campaign_operation )
470+ build_ad_group_operation (
471+ client , customer_id , campaign_operation
472+ )
432473 )
433474
434475 return operations
435476
436477
437478def build_ad_group_operation (
438- client : GoogleAdsClient , customer_id : str , campaign_operation : CampaignOperation
479+ client : GoogleAdsClient ,
480+ customer_id : str ,
481+ campaign_operation : CampaignOperation ,
439482) -> AdGroupOperation :
440483 """Builds a new ad group operation for the specified customer ID.
441484
@@ -452,7 +495,9 @@ def build_ad_group_operation(
452495 ad_group = ad_group_operation .create
453496 ad_group_id : int = get_next_temporary_id ()
454497 # Creates a resource name using the temporary ID.
455- ad_group .resource_name = ad_group_service .ad_group_path (customer_id , ad_group_id )
498+ ad_group .resource_name = ad_group_service .ad_group_path (
499+ customer_id , ad_group_id
500+ )
456501 ad_group .name = f"Batch job ad group #{ uuid4 ()} .{ ad_group_id } "
457502 ad_group .campaign = campaign_operation .create .resource_name
458503 ad_group .type_ = client .enums .AdGroupTypeEnum .SEARCH_STANDARD
@@ -524,7 +569,9 @@ def build_ad_group_criterion_operation(
524569 if not is_valid :
525570 ad_group_criterion .keyword .text += "!!!"
526571
527- ad_group_criterion .keyword .match_type = client .enums .KeywordMatchTypeEnum .BROAD
572+ ad_group_criterion .keyword .match_type = (
573+ client .enums .KeywordMatchTypeEnum .BROAD
574+ )
528575 ad_group_criterion .ad_group = ad_group_operation .create .resource_name
529576 # Keyword criteria do not have a status field.
530577 # ad_group_criterion.status = client.enums.AdGroupCriterionStatusEnum.ENABLED
@@ -560,7 +607,9 @@ def build_ad_group_ad_operation(
560607
561608 Returns: an AdGroupAdOperation instance.
562609 """
563- ad_group_ad_operation : AdGroupAdOperation = client .get_type ("AdGroupAdOperation" )
610+ ad_group_ad_operation : AdGroupAdOperation = client .get_type (
611+ "AdGroupAdOperation"
612+ )
564613 # Creates an ad group ad.
565614 ad_group_ad = ad_group_ad_operation .create
566615 # Creates the expanded text ad info.
@@ -592,7 +641,10 @@ def run_batch_job(
592641 response : Operation = batch_job_service .run_batch_job (
593642 resource_name = resource_name
594643 )
595- print (f'Batch job with resource name "{ resource_name } " has been ' "executed." )
644+ print (
645+ f'Batch job with resource name "{ resource_name } " has been '
646+ "executed."
647+ )
596648 return response
597649 except GoogleAdsException as exception :
598650 handle_googleads_exception (exception )
@@ -610,7 +662,9 @@ def run_batch_job(
610662
611663
612664# [START add_complete_campaigns_using_batch_job_3]
613- def poll_batch_job (operations_response : Operation , event : asyncio .Event ) -> None :
665+ def poll_batch_job (
666+ operations_response : Operation , event : asyncio .Event
667+ ) -> None :
614668 """Polls the server until the batch job execution finishes.
615669
616670 Sets the initial poll delay time and the total time to wait before time-out.
0 commit comments