Skip to content

Commit

Permalink
Update examples to new enum syntax (#459)
Browse files Browse the repository at this point in the history
  • Loading branch information
laurachevalier4 committed Jul 2, 2021
1 parent b83a305 commit 77974af
Show file tree
Hide file tree
Showing 86 changed files with 726 additions and 875 deletions.
4 changes: 1 addition & 3 deletions examples/account_management/approve_merchant_center_link.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 55,7 @@ def main(client, customer_id, merchant_center_account_id):
)
# [END approve_merchant_center_link]

merchant_center_link_status_enum = client.get_type(
"MerchantCenterLinkStatusEnum"
).MerchantCenterLinkStatus
merchant_center_link_status_enum = client.enums.MerchantCenterLinkStatusEnum

# Iterate through the results and filter for links with pending statuses.
for merchant_center_link in response.merchant_center_links:
Expand Down
8 changes: 2 additions & 6 deletions examples/account_management/invite_user_with_access_role.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 41,7 @@ def main(client, customer_id, email_address, access_role):
)
invitation = invitation_operation.create
invitation.email_address = email_address
invitation.access_role = client.get_type(
"AccessRoleEnum"
)._pb.AccessRole.Value(access_role)
invitation.access_role = client.enums.AccessRoleEnum[access_role].value

response = service.mutate_customer_user_access_invitation(
customer_id=customer_id, operation=invitation_operation
Expand Down Expand Up @@ -89,9 87,7 @@ def main(client, customer_id, email_address, access_role):
"--access_role",
type=str,
required=True,
choices=googleads_client.get_type(
"AccessRoleEnum"
)._pb.AccessRole.keys(),
choices=[e.name for e in googleads_client.enums.AccessRoleEnum],
help="The updated user access role.",
)
args = parser.parse_args()
Expand Down
16 changes: 8 additions & 8 deletions examples/account_management/link_manager_to_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 39,7 @@ def main(client, customer_id, manager_customer_id):
client_link_operation = client.get_type("CustomerClientLinkOperation")
client_link = client_link_operation.create
client_link.client_customer = f"customers/{customer_id}"
client_link.status = client.get_type(
"ManagerLinkStatusEnum"
).ManagerLinkStatus.PENDING
client_link.status = client.enums.ManagerLinkStatusEnum.PENDING

response = customer_client_link_service.mutate_customer_client_link(
customer_id=manager_customer_id, operation=client_link_operation
Expand Down Expand Up @@ -85,13 83,15 @@ def main(client, customer_id, manager_customer_id):
)
manager_link_operation = client.get_type("CustomerManagerLinkOperation")
manager_link = manager_link_operation.update
manager_link.resource_name = customer_manager_link_service.customer_manager_link_path(
customer_id, manager_customer_id, manager_link_id,
manager_link.resource_name = (
customer_manager_link_service.customer_manager_link_path(
customer_id,
manager_customer_id,
manager_link_id,
)
)

manager_link.status = client.get_type(
"ManagerLinkStatusEnum"
).ManagerLinkStatus.PENDING
manager_link.status = client.enums.ManagerLinkStatusEnum.PENDING
client.copy_from(
manager_link_operation.update_mask,
protobuf_helpers.field_mask(None, manager_link._pb),
Expand Down
8 changes: 5 additions & 3 deletions examples/account_management/update_user_access.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 111,12 @@ def _modify_user_access(client, customer_id, user_id, access_role):
"CustomerUserAccessService"
)
customer_user_access_op = client.get_type("CustomerUserAccessOperation")
access_role_enum = client.get_type("AccessRoleEnum").AccessRole
access_role_enum = client.enums.AccessRoleEnum
customer_user_access = customer_user_access_op.update
customer_user_access.resource_name = customer_user_access_service.customer_user_access_path(
customer_id, user_id
customer_user_access.resource_name = (
customer_user_access_service.customer_user_access_path(
customer_id, user_id
)
)
customer_user_access.access_role = getattr(access_role_enum, access_role)
client.copy_from(
Expand Down
22 changes: 10 additions & 12 deletions examples/advanced_operations/add_ad_customizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 81,7 @@ def _create_add_customizer_feed(client, customer_id, feed_name):
# Creates three feed attributes: a name, a price and a date.
# The attribute names are arbitrary choices and will be used as
# placeholders in the ad text fields.
feed_attr_type_enum = client.get_type(
"FeedAttributeTypeEnum"
).FeedAttributeType
feed_attr_type_enum = client.enums.FeedAttributeTypeEnum

name_attr = client.get_type("FeedAttribute")
name_attr.type_ = feed_attr_type_enum.STRING
Expand All @@ -102,7 100,7 @@ def _create_add_customizer_feed(client, customer_id, feed_name):

feed.name = feed_name
feed.attributes.extend([name_attr, price_attr, date_attr])
feed.origin = client.get_type("FeedOriginEnum").FeedOrigin.USER
feed.origin = client.enums.FeedOriginEnum.USER

feed_service = client.get_service("FeedService")

Expand Down Expand Up @@ -151,7 149,6 @@ def _get_feed_attributes(client, customer_id, feed_resource_name):
except GoogleAdsException as ex:
_handle_googleads_exception(ex)

feed_attr_type_enum = client.get_type("FeedAttributeTypeEnum")
feed_details = {}
for feed_attribute in feed.attributes:
name = feed_attribute.name
Expand All @@ -166,7 163,10 @@ def _get_feed_attributes(client, customer_id, feed_resource_name):

# [START add_ad_customizer_2]
def _create_ad_customizer_mapping(
client, customer_id, ad_customizer_feed_resource_name, feed_details,
client,
customer_id,
ad_customizer_feed_resource_name,
feed_details,
):
"""Creates a feed mapping for a given feed.
Expand All @@ -177,9 177,7 @@ def _create_ad_customizer_mapping(
feed.
feed_details: a dict mapping feed attribute names to their IDs.
"""
placeholder_field_enum = client.get_type(
"AdCustomizerPlaceholderFieldEnum"
).AdCustomizerPlaceholderField
placeholder_field_enum = client.enums.AdCustomizerPlaceholderFieldEnum

# Map the feed attributes to ad customizer placeholder fields. For a full
# list of ad customizer placeholder fields, see:
Expand All @@ -199,9 197,9 @@ def _create_ad_customizer_mapping(
feed_mapping_op = client.get_type("FeedMappingOperation")
feed_mapping = feed_mapping_op.create
feed_mapping.feed = ad_customizer_feed_resource_name
feed_mapping.placeholder_type = client.get_type(
"PlaceholderTypeEnum"
).PlaceholderType.AD_CUSTOMIZER
feed_mapping.placeholder_type = (
client.enums.PlaceholderTypeEnum.AD_CUSTOMIZER
)
feed_mapping.attribute_field_mappings.extend(
[name_field_mapping, price_field_mapping, date_field_mapping]
)
Expand Down
5 changes: 3 additions & 2 deletions examples/advanced_operations/add_ad_group_bid_modifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 46,13 @@ def main(client, customer_id, ad_group_id, bid_modifier_value):
ad_group_bid_modifier.bid_modifier = bid_modifier_value

# Sets the device.
device_enum = client.get_type("DeviceEnum").Device
device_enum = client.enums.DeviceEnum
ad_group_bid_modifier.device.type_ = device_enum.MOBILE

# Add the ad group bid modifier.
ad_group_bm_response = ad_group_bm_service.mutate_ad_group_bid_modifiers(
customer_id=customer_id, operations=[ad_group_bid_modifier_operation],
customer_id=customer_id,
operations=[ad_group_bid_modifier_operation],
)
# [END add_ad_group_bid_modifier]

Expand Down
52 changes: 24 additions & 28 deletions examples/advanced_operations/add_app_campaign.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 73,9 @@ def _create_budget(client, customer_id):
campaign_budget = campaign_budget_operation.create
campaign_budget.name = f"Interplanetary Cruise #{uuid4()}"
campaign_budget.amount_micros = 50000000
campaign_budget.delivery_method = client.get_type(
"BudgetDeliveryMethodEnum"
).BudgetDeliveryMethod.STANDARD
campaign_budget.delivery_method = (
client.enums.BudgetDeliveryMethodEnum.STANDARD
)
# An App campaign cannot use a shared campaign budget.
# explicitly_shared must be set to false.
campaign_budget.explicitly_shared = False
Expand Down Expand Up @@ -108,18 108,16 @@ def _create_campaign(client, customer_id, budget_resource_name):
# Recommendation: Set the campaign to PAUSED when creating it to
# prevent the ads from immediately serving. Set to ENABLED once you've
# added targeting and the ads are ready to serve.
campaign.status = client.get_type(
"CampaignStatusEnum"
).CampaignStatus.PAUSED
campaign.status = client.enums.CampaignStatusEnum.PAUSED
# All App campaigns have an advertising_channel_type of
# MULTI_CHANNEL to reflect the fact that ads from these campaigns are
# eligible to appear on multiple channels.
campaign.advertising_channel_type = client.get_type(
"AdvertisingChannelTypeEnum"
).AdvertisingChannelType.MULTI_CHANNEL
campaign.advertising_channel_sub_type = client.get_type(
"AdvertisingChannelSubTypeEnum"
).AdvertisingChannelSubType.APP_CAMPAIGN
campaign.advertising_channel_type = (
client.enums.AdvertisingChannelTypeEnum.MULTI_CHANNEL
)
campaign.advertising_channel_sub_type = (
client.enums.AdvertisingChannelSubTypeEnum.APP_CAMPAIGN
)
# Sets the target CPA to $1 / app install.
#
# campaign_bidding_strategy is a 'oneof' message so setting target_cpa
Expand All @@ -130,13 128,13 @@ def _create_campaign(client, customer_id, budget_resource_name):
campaign.target_cpa.target_cpa_micros = 1000000
# Sets the App Campaign Settings.
campaign.app_campaign_setting.app_id = "com.google.android.apps.adwords"
campaign.app_campaign_setting.app_store = client.get_type(
"AppCampaignAppStoreEnum"
).AppCampaignAppStore.GOOGLE_APP_STORE
campaign.app_campaign_setting.app_store = (
client.enums.AppCampaignAppStoreEnum.GOOGLE_APP_STORE
)
# Optimize this campaign for getting new users for your app.
campaign.app_campaign_setting.bidding_strategy_goal_type = client.get_type(
"AppCampaignBiddingStrategyGoalTypeEnum"
).AppCampaignBiddingStrategyGoalType.OPTIMIZE_INSTALLS_TARGET_INSTALL_COST
campaign.app_campaign_setting.bidding_strategy_goal_type = (
client.enums.AppCampaignBiddingStrategyGoalTypeEnum.OPTIMIZE_INSTALLS_TARGET_INSTALL_COST
)
# Optional fields
campaign.start_date = (datetime.now() timedelta(1)).strftime("%Y%m%d")
campaign.end_date = (datetime.now() timedelta(365)).strftime("%Y%m%d")
Expand Down Expand Up @@ -172,8 170,8 @@ def _set_campaign_targeting_criteria(
campaign_resource_name: the campaign to apply targeting to
"""
campaign_criterion_service = client.get_service("CampaignCriterionService")
location_type = client.get_type("CriterionTypeEnum").CriterionType.LOCATION
language_type = client.get_type("CriterionTypeEnum").CriterionType.LANGUAGE
location_type = client.enums.CriterionTypeEnum.LOCATION
language_type = client.enums.CriterionTypeEnum.LANGUAGE
geo_target_constant_service = client.get_service("GeoTargetConstantService")
language_constant_service = client.get_service("LanguageConstantService")

Expand All @@ -190,8 188,8 @@ def _set_campaign_targeting_criteria(
campaign_criterion = campaign_criterion_operation.create
campaign_criterion.campaign = campaign_resource_name
campaign_criterion.type_ = location_type
campaign_criterion.location.geo_target_constant = geo_target_constant_service.geo_target_constant_path(
location_id
campaign_criterion.location.geo_target_constant = (
geo_target_constant_service.geo_target_constant_path(location_id)
)
campaign_criterion_operations.append(campaign_criterion_operation)

Expand All @@ -203,8 201,8 @@ def _set_campaign_targeting_criteria(
campaign_criterion = campaign_criterion_operation.create
campaign_criterion.campaign = campaign_resource_name
campaign_criterion.type_ = language_type
campaign_criterion.language.language_constant = language_constant_service.language_constant_path(
language_id
campaign_criterion.language.language_constant = (
language_constant_service.language_constant_path(language_id)
)
campaign_criterion_operations.append(campaign_criterion_operation)

Expand Down Expand Up @@ -239,7 237,7 @@ def _create_ad_group(client, customer_id, campaign_resource_name):
ad_group_operation = client.get_type("AdGroupOperation")
ad_group = ad_group_operation.create
ad_group.name = f"Earth to Mars cruises {uuid4()}"
ad_group.status = client.get_type("AdGroupStatusEnum").AdGroupStatus.ENABLED
ad_group.status = client.enums.AdGroupStatusEnum.ENABLED
ad_group.campaign = campaign_resource_name

ad_group_response = ad_group_service.mutate_ad_groups(
Expand All @@ -263,9 261,7 @@ def _create_app_ad(client, customer_id, ad_group_resource_name):
ad_group_ad_service = client.get_service("AdGroupAdService")
ad_group_ad_operation = client.get_type("AdGroupAdOperation")
ad_group_ad = ad_group_ad_operation.create
ad_group_ad.status = client.get_type(
"AdGroupAdStatusEnum"
).AdGroupAdStatus.ENABLED
ad_group_ad.status = client.enums.AdGroupAdStatusEnum.ENABLED
ad_group_ad.ad_group = ad_group_resource_name
# ad_data is a 'oneof' message so setting app_ad
# is mutually exclusive with ad data fields such as
Expand Down
14 changes: 5 additions & 9 deletions examples/advanced_operations/add_display_upload_ad.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 72,7 @@ def _create_media_bundle_asset(client, customer_id):
# Construct an asset operation and populate its fields.
asset_operation = client.get_type("AssetOperation")
media_bundle_asset = asset_operation.create
media_bundle_asset.type_ = client.get_type(
"AssetTypeEnum"
).AssetType.MEDIA_BUNDLE
media_bundle_asset.type_ = client.enums.AssetTypeEnum.MEDIA_BUNDLE
# The HTML5 zip file contains all the HTML, CSS, and images needed for the
# HTML5 ad. For help on creating an HTML5 zip file, check out Google Web
# Designer (https://www.google.com/webdesigner/).
Expand Down Expand Up @@ -117,9 115,7 @@ def _create_display_upload_ad_group_ad(

# Configure the ad group ad fields.
ad_group_ad = ad_group_ad_operation.create
ad_group_ad.status = client.get_type(
"AdGroupAdStatusEnum"
).AdGroupAdStatus.PAUSED
ad_group_ad.status = client.enums.AdGroupAdStatusEnum.PAUSED
ad_group_ad.ad_group = client.get_service("AdGroupService").ad_group_path(
customer_id, ad_group_id
)
Expand All @@ -135,9 131,9 @@ def _create_display_upload_ad_group_ad(
display_upload_ad.display_upload_ad.media_bundle.asset = (
ad_asset_resource_name
)
display_upload_ad.display_upload_ad.display_upload_product_type = client.get_type(
"DisplayUploadProductTypeEnum"
).DisplayUploadProductType.HTML5_UPLOAD_AD
display_upload_ad.display_upload_ad.display_upload_product_type = (
client.enums.DisplayUploadProductTypeEnum.HTML5_UPLOAD_AD
)

# Add the ad group ad to the client account and display the resulting
# ad's resource name.
Expand Down
22 changes: 9 additions & 13 deletions examples/advanced_operations/add_dynamic_page_feed.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 88,9 @@ def _create_feed(client, customer_id):
# Create a new feed.
feed = feed_operation.create
feed.name = f"DSA Feed #{uuid.uuid4()}"
feed.origin = client.get_type("FeedOriginEnum").FeedOrigin.USER
feed.origin = client.enums.FeedOriginEnum.USER

feed_attribute_type_enum = client.get_type(
"FeedAttributeTypeEnum"
).FeedAttributeType
feed_attribute_type_enum = client.enums.FeedAttributeTypeEnum

# Create the feed's attributes.
feed_attribute_url = client.get_type("FeedAttribute")
Expand Down Expand Up @@ -169,13 167,11 @@ def _create_feed_mapping(client, customer_id, feed_details):
feed_mapping_operation = client.get_type("FeedMappingOperation")
# Create a new feed mapping.
feed_mapping = feed_mapping_operation.create
feed_mapping.criterion_type = client.get_type(
"FeedMappingCriterionTypeEnum"
).FeedMappingCriterionType.DSA_PAGE_FEED
feed_mapping.criterion_type = (
client.enums.FeedMappingCriterionTypeEnum.DSA_PAGE_FEED
)
feed_mapping.feed = feed_details.resource_name
dsa_page_feed_field_enum = client.get_type(
"DsaPageFeedCriterionFieldEnum"
).DsaPageFeedCriterionField
dsa_page_feed_field_enum = client.enums.DsaPageFeedCriterionFieldEnum

url_field_mapping = client.get_type("AttributeFieldMapping")
url_field_mapping.feed_attribute_id = feed_details.url_attribute_id
Expand Down Expand Up @@ -343,9 339,9 @@ def _add_dsa_targeting(client, customer_id, ad_group_resource_name, label):
"WebpageConditionInfo"
) # ad_group_criterion.webpage.conditions.add()
webpage_criterion_info.argument = label
webpage_criterion_info.operand = client.get_type(
"WebpageConditionOperandEnum"
).WebpageConditionOperand.CUSTOM_LABEL
webpage_criterion_info.operand = (
client.enums.WebpageConditionOperandEnum.CUSTOM_LABEL
)
ad_group_criterion.webpage.conditions.append(webpage_criterion_info)

# Retrieve the ad group criterion service.
Expand Down
Loading

0 comments on commit 77974af

Please sign in to comment.