1919
2020import argparse
2121import sys
22+ from typing import List , Any
2223
2324from google .ads .googleads .client import GoogleAdsClient
2425from google .ads .googleads .errors import GoogleAdsException
26+ from google .ads .googleads .v20 .services .services .campaign_label_service import (
27+ CampaignLabelServiceClient ,
28+ )
29+ from google .ads .googleads .v20 .services .services .campaign_service import (
30+ CampaignServiceClient ,
31+ )
32+ from google .ads .googleads .v20 .services .services .label_service import (
33+ LabelServiceClient ,
34+ )
35+ from google .ads .googleads .v20 .services .types .campaign_label_service import (
36+ MutateCampaignLabelsResponse ,
37+ )
38+ from google .ads .googleads .v20 .resources .types .campaign_label import (
39+ CampaignLabel ,
40+ )
2541
2642
2743# [START add_campaign_labels]
28- def main (client , customer_id , label_id , campaign_ids ):
44+ def main (
45+ client : GoogleAdsClient ,
46+ customer_id : str ,
47+ label_id : str ,
48+ campaign_ids : List [str ],
49+ ) -> None :
2950 """This code example adds a campaign label to a list of campaigns.
3051
3152 Args:
@@ -36,28 +57,36 @@ def main(client, customer_id, label_id, campaign_ids):
3657 """
3758
3859 # Get an instance of CampaignLabelService client.
39- campaign_label_service = client .get_service ("CampaignLabelService" )
40- campaign_service = client .get_service ("CampaignService" )
41- label_service = client .get_service ("LabelService" )
60+ campaign_label_service : CampaignLabelServiceClient = client .get_service (
61+ "CampaignLabelService"
62+ )
63+ campaign_service : CampaignServiceClient = client .get_service (
64+ "CampaignService"
65+ )
66+ label_service : LabelServiceClient = client .get_service ("LabelService" )
4267
4368 # Build the resource name of the label to be added across the campaigns.
44- label_resource_name = label_service .label_path (customer_id , label_id )
69+ label_resource_name : str = label_service .label_path (customer_id , label_id )
4570
46- operations = []
71+ operations : List [ Any ] = []
4772
4873 for campaign_id in campaign_ids :
49- campaign_resource_name = campaign_service .campaign_path (
74+ campaign_resource_name : str = campaign_service .campaign_path (
5075 customer_id , campaign_id
5176 )
52- campaign_label_operation = client .get_type ("CampaignLabelOperation" )
77+ campaign_label_operation : Any = client .get_type (
78+ "CampaignLabelOperation"
79+ )
5380
54- campaign_label = campaign_label_operation .create
81+ campaign_label : CampaignLabel = campaign_label_operation .create
5582 campaign_label .campaign = campaign_resource_name
5683 campaign_label .label = label_resource_name
5784 operations .append (campaign_label_operation )
5885
59- response = campaign_label_service .mutate_campaign_labels (
60- customer_id = customer_id , operations = operations
86+ response : MutateCampaignLabelsResponse = (
87+ campaign_label_service .mutate_campaign_labels (
88+ customer_id = customer_id , operations = operations
89+ )
6190 )
6291 print (f"Added { len (response .results )} campaign labels:" )
6392 for result in response .results :
@@ -93,11 +122,13 @@ def main(client, customer_id, label_id, campaign_ids):
93122 required = True ,
94123 help = "The campaign IDs to receive the label." ,
95124 )
96- args = parser .parse_args ()
125+ args : argparse . Namespace = parser .parse_args ()
97126
98127 # GoogleAdsClient will read the google-ads.yaml configuration file in the
99128 # home directory if none is specified.
100- googleads_client = GoogleAdsClient .load_from_storage (version = "v20" )
129+ googleads_client : GoogleAdsClient = GoogleAdsClient .load_from_storage (
130+ version = "v20"
131+ )
101132 try :
102133 main (
103134 googleads_client , args .customer_id , args .label_id , args .campaign_ids
0 commit comments