Skip to content

Commit 77974af

Browse files
Update examples to new enum syntax (#459)
1 parent b83a305 commit 77974af

File tree

86 files changed

+726
-875
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

86 files changed

+726
-875
lines changed

examples/account_management/approve_merchant_center_link.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,7 @@ def main(client, customer_id, merchant_center_account_id):
5555
)
5656
# [END approve_merchant_center_link]
5757

58-
merchant_center_link_status_enum = client.get_type(
59-
"MerchantCenterLinkStatusEnum"
60-
).MerchantCenterLinkStatus
58+
merchant_center_link_status_enum = client.enums.MerchantCenterLinkStatusEnum
6159

6260
# Iterate through the results and filter for links with pending statuses.
6361
for merchant_center_link in response.merchant_center_links:

examples/account_management/invite_user_with_access_role.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,7 @@ def main(client, customer_id, email_address, access_role):
4141
)
4242
invitation = invitation_operation.create
4343
invitation.email_address = email_address
44-
invitation.access_role = client.get_type(
45-
"AccessRoleEnum"
46-
)._pb.AccessRole.Value(access_role)
44+
invitation.access_role = client.enums.AccessRoleEnum[access_role].value
4745

4846
response = service.mutate_customer_user_access_invitation(
4947
customer_id=customer_id, operation=invitation_operation
@@ -89,9 +87,7 @@ def main(client, customer_id, email_address, access_role):
8987
"--access_role",
9088
type=str,
9189
required=True,
92-
choices=googleads_client.get_type(
93-
"AccessRoleEnum"
94-
)._pb.AccessRole.keys(),
90+
choices=[e.name for e in googleads_client.enums.AccessRoleEnum],
9591
help="The updated user access role.",
9692
)
9793
args = parser.parse_args()

examples/account_management/link_manager_to_client.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,7 @@ def main(client, customer_id, manager_customer_id):
3939
client_link_operation = client.get_type("CustomerClientLinkOperation")
4040
client_link = client_link_operation.create
4141
client_link.client_customer = f"customers/{customer_id}"
42-
client_link.status = client.get_type(
43-
"ManagerLinkStatusEnum"
44-
).ManagerLinkStatus.PENDING
42+
client_link.status = client.enums.ManagerLinkStatusEnum.PENDING
4543

4644
response = customer_client_link_service.mutate_customer_client_link(
4745
customer_id=manager_customer_id, operation=client_link_operation
@@ -85,13 +83,15 @@ def main(client, customer_id, manager_customer_id):
8583
)
8684
manager_link_operation = client.get_type("CustomerManagerLinkOperation")
8785
manager_link = manager_link_operation.update
88-
manager_link.resource_name = customer_manager_link_service.customer_manager_link_path(
89-
customer_id, manager_customer_id, manager_link_id,
86+
manager_link.resource_name = (
87+
customer_manager_link_service.customer_manager_link_path(
88+
customer_id,
89+
manager_customer_id,
90+
manager_link_id,
91+
)
9092
)
9193

92-
manager_link.status = client.get_type(
93-
"ManagerLinkStatusEnum"
94-
).ManagerLinkStatus.PENDING
94+
manager_link.status = client.enums.ManagerLinkStatusEnum.PENDING
9595
client.copy_from(
9696
manager_link_operation.update_mask,
9797
protobuf_helpers.field_mask(None, manager_link._pb),

examples/account_management/update_user_access.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,12 @@ def _modify_user_access(client, customer_id, user_id, access_role):
111111
"CustomerUserAccessService"
112112
)
113113
customer_user_access_op = client.get_type("CustomerUserAccessOperation")
114-
access_role_enum = client.get_type("AccessRoleEnum").AccessRole
114+
access_role_enum = client.enums.AccessRoleEnum
115115
customer_user_access = customer_user_access_op.update
116-
customer_user_access.resource_name = customer_user_access_service.customer_user_access_path(
117-
customer_id, user_id
116+
customer_user_access.resource_name = (
117+
customer_user_access_service.customer_user_access_path(
118+
customer_id, user_id
119+
)
118120
)
119121
customer_user_access.access_role = getattr(access_role_enum, access_role)
120122
client.copy_from(

examples/advanced_operations/add_ad_customizer.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,7 @@ def _create_add_customizer_feed(client, customer_id, feed_name):
8181
# Creates three feed attributes: a name, a price and a date.
8282
# The attribute names are arbitrary choices and will be used as
8383
# placeholders in the ad text fields.
84-
feed_attr_type_enum = client.get_type(
85-
"FeedAttributeTypeEnum"
86-
).FeedAttributeType
84+
feed_attr_type_enum = client.enums.FeedAttributeTypeEnum
8785

8886
name_attr = client.get_type("FeedAttribute")
8987
name_attr.type_ = feed_attr_type_enum.STRING
@@ -102,7 +100,7 @@ def _create_add_customizer_feed(client, customer_id, feed_name):
102100

103101
feed.name = feed_name
104102
feed.attributes.extend([name_attr, price_attr, date_attr])
105-
feed.origin = client.get_type("FeedOriginEnum").FeedOrigin.USER
103+
feed.origin = client.enums.FeedOriginEnum.USER
106104

107105
feed_service = client.get_service("FeedService")
108106

@@ -151,7 +149,6 @@ def _get_feed_attributes(client, customer_id, feed_resource_name):
151149
except GoogleAdsException as ex:
152150
_handle_googleads_exception(ex)
153151

154-
feed_attr_type_enum = client.get_type("FeedAttributeTypeEnum")
155152
feed_details = {}
156153
for feed_attribute in feed.attributes:
157154
name = feed_attribute.name
@@ -166,7 +163,10 @@ def _get_feed_attributes(client, customer_id, feed_resource_name):
166163

167164
# [START add_ad_customizer_2]
168165
def _create_ad_customizer_mapping(
169-
client, customer_id, ad_customizer_feed_resource_name, feed_details,
166+
client,
167+
customer_id,
168+
ad_customizer_feed_resource_name,
169+
feed_details,
170170
):
171171
"""Creates a feed mapping for a given feed.
172172
@@ -177,9 +177,7 @@ def _create_ad_customizer_mapping(
177177
feed.
178178
feed_details: a dict mapping feed attribute names to their IDs.
179179
"""
180-
placeholder_field_enum = client.get_type(
181-
"AdCustomizerPlaceholderFieldEnum"
182-
).AdCustomizerPlaceholderField
180+
placeholder_field_enum = client.enums.AdCustomizerPlaceholderFieldEnum
183181

184182
# Map the feed attributes to ad customizer placeholder fields. For a full
185183
# list of ad customizer placeholder fields, see:
@@ -199,9 +197,9 @@ def _create_ad_customizer_mapping(
199197
feed_mapping_op = client.get_type("FeedMappingOperation")
200198
feed_mapping = feed_mapping_op.create
201199
feed_mapping.feed = ad_customizer_feed_resource_name
202-
feed_mapping.placeholder_type = client.get_type(
203-
"PlaceholderTypeEnum"
204-
).PlaceholderType.AD_CUSTOMIZER
200+
feed_mapping.placeholder_type = (
201+
client.enums.PlaceholderTypeEnum.AD_CUSTOMIZER
202+
)
205203
feed_mapping.attribute_field_mappings.extend(
206204
[name_field_mapping, price_field_mapping, date_field_mapping]
207205
)

examples/advanced_operations/add_ad_group_bid_modifier.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,13 @@ def main(client, customer_id, ad_group_id, bid_modifier_value):
4646
ad_group_bid_modifier.bid_modifier = bid_modifier_value
4747

4848
# Sets the device.
49-
device_enum = client.get_type("DeviceEnum").Device
49+
device_enum = client.enums.DeviceEnum
5050
ad_group_bid_modifier.device.type_ = device_enum.MOBILE
5151

5252
# Add the ad group bid modifier.
5353
ad_group_bm_response = ad_group_bm_service.mutate_ad_group_bid_modifiers(
54-
customer_id=customer_id, operations=[ad_group_bid_modifier_operation],
54+
customer_id=customer_id,
55+
operations=[ad_group_bid_modifier_operation],
5556
)
5657
# [END add_ad_group_bid_modifier]
5758

examples/advanced_operations/add_app_campaign.py

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,9 @@ def _create_budget(client, customer_id):
7373
campaign_budget = campaign_budget_operation.create
7474
campaign_budget.name = f"Interplanetary Cruise #{uuid4()}"
7575
campaign_budget.amount_micros = 50000000
76-
campaign_budget.delivery_method = client.get_type(
77-
"BudgetDeliveryMethodEnum"
78-
).BudgetDeliveryMethod.STANDARD
76+
campaign_budget.delivery_method = (
77+
client.enums.BudgetDeliveryMethodEnum.STANDARD
78+
)
7979
# An App campaign cannot use a shared campaign budget.
8080
# explicitly_shared must be set to false.
8181
campaign_budget.explicitly_shared = False
@@ -108,18 +108,16 @@ def _create_campaign(client, customer_id, budget_resource_name):
108108
# Recommendation: Set the campaign to PAUSED when creating it to
109109
# prevent the ads from immediately serving. Set to ENABLED once you've
110110
# added targeting and the ads are ready to serve.
111-
campaign.status = client.get_type(
112-
"CampaignStatusEnum"
113-
).CampaignStatus.PAUSED
111+
campaign.status = client.enums.CampaignStatusEnum.PAUSED
114112
# All App campaigns have an advertising_channel_type of
115113
# MULTI_CHANNEL to reflect the fact that ads from these campaigns are
116114
# eligible to appear on multiple channels.
117-
campaign.advertising_channel_type = client.get_type(
118-
"AdvertisingChannelTypeEnum"
119-
).AdvertisingChannelType.MULTI_CHANNEL
120-
campaign.advertising_channel_sub_type = client.get_type(
121-
"AdvertisingChannelSubTypeEnum"
122-
).AdvertisingChannelSubType.APP_CAMPAIGN
115+
campaign.advertising_channel_type = (
116+
client.enums.AdvertisingChannelTypeEnum.MULTI_CHANNEL
117+
)
118+
campaign.advertising_channel_sub_type = (
119+
client.enums.AdvertisingChannelSubTypeEnum.APP_CAMPAIGN
120+
)
123121
# Sets the target CPA to $1 / app install.
124122
#
125123
# campaign_bidding_strategy is a 'oneof' message so setting target_cpa
@@ -130,13 +128,13 @@ def _create_campaign(client, customer_id, budget_resource_name):
130128
campaign.target_cpa.target_cpa_micros = 1000000
131129
# Sets the App Campaign Settings.
132130
campaign.app_campaign_setting.app_id = "com.google.android.apps.adwords"
133-
campaign.app_campaign_setting.app_store = client.get_type(
134-
"AppCampaignAppStoreEnum"
135-
).AppCampaignAppStore.GOOGLE_APP_STORE
131+
campaign.app_campaign_setting.app_store = (
132+
client.enums.AppCampaignAppStoreEnum.GOOGLE_APP_STORE
133+
)
136134
# Optimize this campaign for getting new users for your app.
137-
campaign.app_campaign_setting.bidding_strategy_goal_type = client.get_type(
138-
"AppCampaignBiddingStrategyGoalTypeEnum"
139-
).AppCampaignBiddingStrategyGoalType.OPTIMIZE_INSTALLS_TARGET_INSTALL_COST
135+
campaign.app_campaign_setting.bidding_strategy_goal_type = (
136+
client.enums.AppCampaignBiddingStrategyGoalTypeEnum.OPTIMIZE_INSTALLS_TARGET_INSTALL_COST
137+
)
140138
# Optional fields
141139
campaign.start_date = (datetime.now() + timedelta(1)).strftime("%Y%m%d")
142140
campaign.end_date = (datetime.now() + timedelta(365)).strftime("%Y%m%d")
@@ -172,8 +170,8 @@ def _set_campaign_targeting_criteria(
172170
campaign_resource_name: the campaign to apply targeting to
173171
"""
174172
campaign_criterion_service = client.get_service("CampaignCriterionService")
175-
location_type = client.get_type("CriterionTypeEnum").CriterionType.LOCATION
176-
language_type = client.get_type("CriterionTypeEnum").CriterionType.LANGUAGE
173+
location_type = client.enums.CriterionTypeEnum.LOCATION
174+
language_type = client.enums.CriterionTypeEnum.LANGUAGE
177175
geo_target_constant_service = client.get_service("GeoTargetConstantService")
178176
language_constant_service = client.get_service("LanguageConstantService")
179177

@@ -190,8 +188,8 @@ def _set_campaign_targeting_criteria(
190188
campaign_criterion = campaign_criterion_operation.create
191189
campaign_criterion.campaign = campaign_resource_name
192190
campaign_criterion.type_ = location_type
193-
campaign_criterion.location.geo_target_constant = geo_target_constant_service.geo_target_constant_path(
194-
location_id
191+
campaign_criterion.location.geo_target_constant = (
192+
geo_target_constant_service.geo_target_constant_path(location_id)
195193
)
196194
campaign_criterion_operations.append(campaign_criterion_operation)
197195

@@ -203,8 +201,8 @@ def _set_campaign_targeting_criteria(
203201
campaign_criterion = campaign_criterion_operation.create
204202
campaign_criterion.campaign = campaign_resource_name
205203
campaign_criterion.type_ = language_type
206-
campaign_criterion.language.language_constant = language_constant_service.language_constant_path(
207-
language_id
204+
campaign_criterion.language.language_constant = (
205+
language_constant_service.language_constant_path(language_id)
208206
)
209207
campaign_criterion_operations.append(campaign_criterion_operation)
210208

@@ -239,7 +237,7 @@ def _create_ad_group(client, customer_id, campaign_resource_name):
239237
ad_group_operation = client.get_type("AdGroupOperation")
240238
ad_group = ad_group_operation.create
241239
ad_group.name = f"Earth to Mars cruises {uuid4()}"
242-
ad_group.status = client.get_type("AdGroupStatusEnum").AdGroupStatus.ENABLED
240+
ad_group.status = client.enums.AdGroupStatusEnum.ENABLED
243241
ad_group.campaign = campaign_resource_name
244242

245243
ad_group_response = ad_group_service.mutate_ad_groups(
@@ -263,9 +261,7 @@ def _create_app_ad(client, customer_id, ad_group_resource_name):
263261
ad_group_ad_service = client.get_service("AdGroupAdService")
264262
ad_group_ad_operation = client.get_type("AdGroupAdOperation")
265263
ad_group_ad = ad_group_ad_operation.create
266-
ad_group_ad.status = client.get_type(
267-
"AdGroupAdStatusEnum"
268-
).AdGroupAdStatus.ENABLED
264+
ad_group_ad.status = client.enums.AdGroupAdStatusEnum.ENABLED
269265
ad_group_ad.ad_group = ad_group_resource_name
270266
# ad_data is a 'oneof' message so setting app_ad
271267
# is mutually exclusive with ad data fields such as

examples/advanced_operations/add_display_upload_ad.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,7 @@ def _create_media_bundle_asset(client, customer_id):
7272
# Construct an asset operation and populate its fields.
7373
asset_operation = client.get_type("AssetOperation")
7474
media_bundle_asset = asset_operation.create
75-
media_bundle_asset.type_ = client.get_type(
76-
"AssetTypeEnum"
77-
).AssetType.MEDIA_BUNDLE
75+
media_bundle_asset.type_ = client.enums.AssetTypeEnum.MEDIA_BUNDLE
7876
# The HTML5 zip file contains all the HTML, CSS, and images needed for the
7977
# HTML5 ad. For help on creating an HTML5 zip file, check out Google Web
8078
# Designer (https://www.google.com/webdesigner/).
@@ -117,9 +115,7 @@ def _create_display_upload_ad_group_ad(
117115

118116
# Configure the ad group ad fields.
119117
ad_group_ad = ad_group_ad_operation.create
120-
ad_group_ad.status = client.get_type(
121-
"AdGroupAdStatusEnum"
122-
).AdGroupAdStatus.PAUSED
118+
ad_group_ad.status = client.enums.AdGroupAdStatusEnum.PAUSED
123119
ad_group_ad.ad_group = client.get_service("AdGroupService").ad_group_path(
124120
customer_id, ad_group_id
125121
)
@@ -135,9 +131,9 @@ def _create_display_upload_ad_group_ad(
135131
display_upload_ad.display_upload_ad.media_bundle.asset = (
136132
ad_asset_resource_name
137133
)
138-
display_upload_ad.display_upload_ad.display_upload_product_type = client.get_type(
139-
"DisplayUploadProductTypeEnum"
140-
).DisplayUploadProductType.HTML5_UPLOAD_AD
134+
display_upload_ad.display_upload_ad.display_upload_product_type = (
135+
client.enums.DisplayUploadProductTypeEnum.HTML5_UPLOAD_AD
136+
)
141137

142138
# Add the ad group ad to the client account and display the resulting
143139
# ad's resource name.

examples/advanced_operations/add_dynamic_page_feed.py

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,9 @@ def _create_feed(client, customer_id):
8888
# Create a new feed.
8989
feed = feed_operation.create
9090
feed.name = f"DSA Feed #{uuid.uuid4()}"
91-
feed.origin = client.get_type("FeedOriginEnum").FeedOrigin.USER
91+
feed.origin = client.enums.FeedOriginEnum.USER
9292

93-
feed_attribute_type_enum = client.get_type(
94-
"FeedAttributeTypeEnum"
95-
).FeedAttributeType
93+
feed_attribute_type_enum = client.enums.FeedAttributeTypeEnum
9694

9795
# Create the feed's attributes.
9896
feed_attribute_url = client.get_type("FeedAttribute")
@@ -169,13 +167,11 @@ def _create_feed_mapping(client, customer_id, feed_details):
169167
feed_mapping_operation = client.get_type("FeedMappingOperation")
170168
# Create a new feed mapping.
171169
feed_mapping = feed_mapping_operation.create
172-
feed_mapping.criterion_type = client.get_type(
173-
"FeedMappingCriterionTypeEnum"
174-
).FeedMappingCriterionType.DSA_PAGE_FEED
170+
feed_mapping.criterion_type = (
171+
client.enums.FeedMappingCriterionTypeEnum.DSA_PAGE_FEED
172+
)
175173
feed_mapping.feed = feed_details.resource_name
176-
dsa_page_feed_field_enum = client.get_type(
177-
"DsaPageFeedCriterionFieldEnum"
178-
).DsaPageFeedCriterionField
174+
dsa_page_feed_field_enum = client.enums.DsaPageFeedCriterionFieldEnum
179175

180176
url_field_mapping = client.get_type("AttributeFieldMapping")
181177
url_field_mapping.feed_attribute_id = feed_details.url_attribute_id
@@ -343,9 +339,9 @@ def _add_dsa_targeting(client, customer_id, ad_group_resource_name, label):
343339
"WebpageConditionInfo"
344340
) # ad_group_criterion.webpage.conditions.add()
345341
webpage_criterion_info.argument = label
346-
webpage_criterion_info.operand = client.get_type(
347-
"WebpageConditionOperandEnum"
348-
).WebpageConditionOperand.CUSTOM_LABEL
342+
webpage_criterion_info.operand = (
343+
client.enums.WebpageConditionOperandEnum.CUSTOM_LABEL
344+
)
349345
ad_group_criterion.webpage.conditions.append(webpage_criterion_info)
350346

351347
# Retrieve the ad group criterion service.

0 commit comments

Comments
 (0)