Skip to content

Commit dd22fb9

Browse files
authored
Add type annotations to examples/travel (#955)
1 parent 2d36b74 commit dd22fb9

File tree

5 files changed

+602
-310
lines changed

5 files changed

+602
-310
lines changed

examples/travel/add_hotel_ad.py

Lines changed: 94 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -27,59 +27,108 @@
2727

2828
from google.ads.googleads.client import GoogleAdsClient
2929
from google.ads.googleads.errors import GoogleAdsException
30+
from google.ads.googleads.v20.resources.types.ad_group import AdGroup
31+
from google.ads.googleads.v20.resources.types.ad_group_ad import AdGroupAd
32+
from google.ads.googleads.v20.resources.types.campaign import Campaign
33+
from google.ads.googleads.v20.resources.types.campaign_budget import (
34+
CampaignBudget,
35+
)
36+
from google.ads.googleads.v20.services.services.ad_group_ad_service import (
37+
AdGroupAdServiceClient,
38+
)
39+
from google.ads.googleads.v20.services.services.ad_group_service import (
40+
AdGroupServiceClient,
41+
)
42+
from google.ads.googleads.v20.services.services.campaign_budget_service import (
43+
CampaignBudgetServiceClient,
44+
)
45+
from google.ads.googleads.v20.services.services.campaign_service import (
46+
CampaignServiceClient,
47+
)
48+
from google.ads.googleads.v20.services.types.ad_group_ad_service import (
49+
AdGroupAdOperation,
50+
MutateAdGroupAdsResponse,
51+
)
52+
from google.ads.googleads.v20.services.types.ad_group_service import (
53+
AdGroupOperation,
54+
MutateAdGroupsResponse,
55+
)
56+
from google.ads.googleads.v20.services.types.campaign_budget_service import (
57+
CampaignBudgetOperation,
58+
MutateCampaignBudgetsResponse,
59+
)
60+
from google.ads.googleads.v20.services.types.campaign_service import (
61+
CampaignOperation,
62+
MutateCampaignsResponse,
63+
)
3064

3165

3266
def main(
33-
client, customer_id, hotel_center_account_id, cpc_bid_ceiling_micro_amount
34-
):
35-
budget_resource_name = add_budget(client, customer_id)
36-
37-
campaign_resource_name = add_hotel_campaign(
67+
client: GoogleAdsClient,
68+
customer_id: str,
69+
hotel_center_account_id: int,
70+
cpc_bid_ceiling_micro_amount: int,
71+
) -> None:
72+
budget_resource_name: str = add_budget(client, customer_id)
73+
74+
campaign_resource_name: str = add_hotel_campaign(
3875
client,
3976
customer_id,
4077
budget_resource_name,
4178
hotel_center_account_id,
4279
cpc_bid_ceiling_micro_amount,
4380
)
4481

45-
ad_group_resource_name = add_hotel_ad_group(
82+
ad_group_resource_name: str = add_hotel_ad_group(
4683
client, customer_id, campaign_resource_name
4784
)
4885

4986
add_hotel_ad(client, customer_id, ad_group_resource_name)
5087

5188

52-
def add_budget(client, customer_id):
53-
campaign_budget_service = client.get_service("CampaignBudgetService")
89+
def add_budget(client: GoogleAdsClient, customer_id: str) -> str:
90+
campaign_budget_service: CampaignBudgetServiceClient = client.get_service(
91+
"CampaignBudgetService"
92+
)
5493

5594
# Create a budget, which can be shared by multiple campaigns.
56-
campaign_budget_operation = client.get_type("CampaignBudgetOperation")
57-
campaign_budget = campaign_budget_operation.create
95+
campaign_budget_operation: CampaignBudgetOperation = client.get_type(
96+
"CampaignBudgetOperation"
97+
)
98+
campaign_budget: CampaignBudget = campaign_budget_operation.create
5899
campaign_budget.name = f"Interplanetary Budget {uuid.uuid4()}"
59100
campaign_budget.delivery_method = (
60101
client.enums.BudgetDeliveryMethodEnum.STANDARD
61102
)
62103
campaign_budget.amount_micros = 500000
63104

64105
# Add budget.
65-
campaign_budget_response = campaign_budget_service.mutate_campaign_budgets(
66-
customer_id=customer_id, operations=[campaign_budget_operation]
106+
campaign_budget_response: MutateCampaignBudgetsResponse = (
107+
campaign_budget_service.mutate_campaign_budgets(
108+
customer_id=customer_id, operations=[campaign_budget_operation]
109+
)
67110
)
68111

69-
budget_resource_name = campaign_budget_response.results[0].resource_name
112+
budget_resource_name: str = campaign_budget_response.results[
113+
0
114+
].resource_name
70115

71116
print(f"Created budget with resource name '{budget_resource_name}'.")
72117

73118
return budget_resource_name
74119

75120

76121
# [START add_hotel_ad_3]
77-
def add_hotel_ad(client, customer_id, ad_group_resource_name):
78-
ad_group_ad_service = client.get_service("AdGroupAdService")
122+
def add_hotel_ad(
123+
client: GoogleAdsClient, customer_id: str, ad_group_resource_name: str
124+
) -> str:
125+
ad_group_ad_service: AdGroupAdServiceClient = client.get_service(
126+
"AdGroupAdService"
127+
)
79128

80129
# Creates a new ad group ad and sets the hotel ad to it.
81-
ad_group_ad_operation = client.get_type("AdGroupAdOperation")
82-
ad_group_ad = ad_group_ad_operation.create
130+
ad_group_ad_operation: AdGroupAdOperation = client.get_type("AdGroupAdOperation")
131+
ad_group_ad: AdGroupAd = ad_group_ad_operation.create
83132
ad_group_ad.ad_group = ad_group_resource_name
84133
# Set the ad group ad to enabled. Setting this to paused will cause an error
85134
# for hotel campaigns. For hotels pausing should happen at either the ad group or
@@ -88,11 +137,13 @@ def add_hotel_ad(client, customer_id, ad_group_resource_name):
88137
client.copy_from(ad_group_ad.ad.hotel_ad, client.get_type("HotelAdInfo"))
89138

90139
# Add the ad group ad.
91-
ad_group_ad_response = ad_group_ad_service.mutate_ad_group_ads(
140+
ad_group_ad_response: MutateAdGroupAdsResponse = ad_group_ad_service.mutate_ad_group_ads(
92141
customer_id=customer_id, operations=[ad_group_ad_operation]
93142
)
94143

95-
ad_group_ad_resource_name = ad_group_ad_response.results[0].resource_name
144+
ad_group_ad_resource_name: str = ad_group_ad_response.results[
145+
0
146+
].resource_name
96147

97148
print(f"Created hotel ad with resource name '{ad_group_ad_resource_name}'.")
98149

@@ -101,12 +152,14 @@ def add_hotel_ad(client, customer_id, ad_group_resource_name):
101152

102153

103154
# [START add_hotel_ad_2]
104-
def add_hotel_ad_group(client, customer_id, campaign_resource_name):
105-
ad_group_service = client.get_service("AdGroupService")
155+
def add_hotel_ad_group(
156+
client: GoogleAdsClient, customer_id: str, campaign_resource_name: str
157+
) -> str:
158+
ad_group_service: AdGroupServiceClient = client.get_service("AdGroupService")
106159

107160
# Create ad group.
108-
ad_group_operation = client.get_type("AdGroupOperation")
109-
ad_group = ad_group_operation.create
161+
ad_group_operation: AdGroupOperation = client.get_type("AdGroupOperation")
162+
ad_group: AdGroup = ad_group_operation.create
110163
ad_group.name = f"Earth to Mars cruise {uuid.uuid4()}"
111164
ad_group.status = client.enums.AdGroupStatusEnum.ENABLED
112165
ad_group.campaign = campaign_resource_name
@@ -115,11 +168,11 @@ def add_hotel_ad_group(client, customer_id, campaign_resource_name):
115168
ad_group.cpc_bid_micros = 10000000
116169

117170
# Add the ad group.
118-
ad_group_response = ad_group_service.mutate_ad_groups(
171+
ad_group_response: MutateAdGroupsResponse = ad_group_service.mutate_ad_groups(
119172
customer_id=customer_id, operations=[ad_group_operation]
120173
)
121174

122-
ad_group_resource_name = ad_group_response.results[0].resource_name
175+
ad_group_resource_name: str = ad_group_response.results[0].resource_name
123176

124177
print(
125178
"Added a hotel ad group with resource name '{ad_group_resource_name}'."
@@ -131,18 +184,18 @@ def add_hotel_ad_group(client, customer_id, campaign_resource_name):
131184

132185
# [START add_hotel_ad]
133186
def add_hotel_campaign(
134-
client,
135-
customer_id,
136-
budget_resource_name,
137-
hotel_center_account_id,
138-
cpc_bid_ceiling_micro_amount,
139-
):
140-
campaign_service = client.get_service("CampaignService")
187+
client: GoogleAdsClient,
188+
customer_id: str,
189+
budget_resource_name: str,
190+
hotel_center_account_id: int,
191+
cpc_bid_ceiling_micro_amount: int,
192+
) -> str:
193+
campaign_service: CampaignServiceClient = client.get_service("CampaignService")
141194

142195
# [START add_hotel_ad_1]
143196
# Create campaign.
144-
campaign_operation = client.get_type("CampaignOperation")
145-
campaign = campaign_operation.create
197+
campaign_operation: CampaignOperation = client.get_type("CampaignOperation")
198+
campaign: Campaign = campaign_operation.create
146199
campaign.name = f"Interplanetary Cruise Campaign {uuid.uuid4()}"
147200

148201
# Configures settings related to hotel campaigns including advertising
@@ -170,11 +223,11 @@ def add_hotel_campaign(
170223
# [END add_hotel_ad_1]
171224

172225
# Add the campaign.
173-
campaign_response = campaign_service.mutate_campaigns(
226+
campaign_response: MutateCampaignsResponse = campaign_service.mutate_campaigns(
174227
customer_id=customer_id, operations=[campaign_operation]
175228
)
176229

177-
campaign_resource_name = campaign_response.results[0].resource_name
230+
campaign_resource_name: str = campaign_response.results[0].resource_name
178231

179232
print(
180233
"Added a hotel campaign with resource name '{campaign_resource_name}'."
@@ -185,7 +238,7 @@ def add_hotel_campaign(
185238

186239

187240
if __name__ == "__main__":
188-
parser = argparse.ArgumentParser(
241+
parser: argparse.ArgumentParser = argparse.ArgumentParser(
189242
description=(
190243
"Adds an expanded text ad to the specified ad group ID, "
191244
"for the given customer ID."
@@ -213,11 +266,13 @@ def add_hotel_campaign(
213266
required=True,
214267
help="The hotel center account ID.",
215268
)
216-
args = parser.parse_args()
269+
args: argparse.Namespace = parser.parse_args()
217270

218271
# GoogleAdsClient will read the google-ads.yaml configuration file in the
219272
# home directory if none is specified.
220-
googleads_client = GoogleAdsClient.load_from_storage(version="v20")
273+
googleads_client: GoogleAdsClient = GoogleAdsClient.load_from_storage(
274+
version="v20"
275+
)
221276

222277
try:
223278
main(

examples/travel/add_hotel_ad_group_bid_modifiers.py

Lines changed: 44 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,37 @@
2323

2424
from google.ads.googleads.client import GoogleAdsClient
2525
from google.ads.googleads.errors import GoogleAdsException
26+
from google.ads.googleads.v20.common.types.criteria import HotelLengthOfStayInfo
27+
from google.ads.googleads.v20.resources.types.ad_group_bid_modifier import (
28+
AdGroupBidModifier,
29+
)
30+
from google.ads.googleads.v20.services.services.ad_group_bid_modifier_service import (
31+
AdGroupBidModifierServiceClient,
32+
)
33+
from google.ads.googleads.v20.services.services.ad_group_service import (
34+
AdGroupServiceClient,
35+
)
36+
from google.ads.googleads.v20.services.types.ad_group_bid_modifier_service import (
37+
AdGroupBidModifierOperation,
38+
MutateAdGroupBidModifierResult,
39+
MutateAdGroupBidModifiersResponse,
40+
)
2641

2742

2843
# [START add_hotel_ad_group_bid_modifiers]
29-
def main(client, customer_id, ad_group_id):
30-
ad_group_service = client.get_service("AdGroupService")
31-
ag_bm_service = client.get_service("AdGroupBidModifierService")
44+
def main(client: GoogleAdsClient, customer_id: str, ad_group_id: str) -> None:
45+
ad_group_service: AdGroupServiceClient = client.get_service("AdGroupService")
46+
ag_bm_service: AdGroupBidModifierServiceClient = client.get_service(
47+
"AdGroupBidModifierService"
48+
)
3249

3350
# Create ad group bid modifier based on hotel check-in day.
34-
check_in_ag_bm_operation = client.get_type("AdGroupBidModifierOperation")
35-
check_in_ag_bid_modifier = check_in_ag_bm_operation.create
51+
check_in_ag_bm_operation: AdGroupBidModifierOperation = client.get_type(
52+
"AdGroupBidModifierOperation"
53+
)
54+
check_in_ag_bid_modifier: AdGroupBidModifier = (
55+
check_in_ag_bm_operation.create
56+
)
3657
check_in_ag_bid_modifier.hotel_check_in_day.day_of_week = (
3758
client.enums.DayOfWeekEnum.MONDAY
3859
)
@@ -43,34 +64,41 @@ def main(client, customer_id, ad_group_id):
4364
check_in_ag_bid_modifier.bid_modifier = 1.5
4465

4566
# Create ad group bid modifier based on hotel length of stay info.
46-
los_ag_bm_operation = client.get_type("AdGroupBidModifierOperation")
47-
los_ag_bid_modifier = los_ag_bm_operation.create
67+
los_ag_bm_operation: AdGroupBidModifierOperation = client.get_type(
68+
"AdGroupBidModifierOperation"
69+
)
70+
los_ag_bid_modifier: AdGroupBidModifier = los_ag_bm_operation.create
4871
los_ag_bid_modifier.ad_group = ad_group_service.ad_group_path(
4972
customer_id, ad_group_id
5073
)
5174
# Creates the hotel length of stay info.
52-
hotel_length_of_stay_info = los_ag_bid_modifier.hotel_length_of_stay
75+
hotel_length_of_stay_info: HotelLengthOfStayInfo = (
76+
los_ag_bid_modifier.hotel_length_of_stay
77+
)
5378
hotel_length_of_stay_info.min_nights = 3
5479
hotel_length_of_stay_info.max_nights = 7
5580
# Sets the bid modifier value to 170%.
5681
los_ag_bid_modifier.bid_modifier = 1.7
5782

5883
# Add the bid modifiers
59-
ag_bm_response = ag_bm_service.mutate_ad_group_bid_modifiers(
60-
customer_id=customer_id,
61-
operations=[check_in_ag_bm_operation, los_ag_bm_operation],
84+
ag_bm_response: MutateAdGroupBidModifiersResponse = (
85+
ag_bm_service.mutate_ad_group_bid_modifiers(
86+
customer_id=customer_id,
87+
operations=[check_in_ag_bm_operation, los_ag_bm_operation],
88+
)
6289
)
6390

6491
# Print out resource names of the added ad group bid modifiers.
6592
print(f"Added {len(ag_bm_response.results)} hotel ad group bid modifiers:")
6693

94+
result: MutateAdGroupBidModifierResult
6795
for result in ag_bm_response.results:
6896
print(result.resource_name)
6997
# [END add_hotel_ad_group_bid_modifiers]
7098

7199

72100
if __name__ == "__main__":
73-
parser = argparse.ArgumentParser(
101+
parser: argparse.ArgumentParser = argparse.ArgumentParser(
74102
description=("Adds an ad group bid modifier to a hotel ad group.")
75103
)
76104
# The following argument(s) should be provided to run the example.
@@ -88,11 +116,13 @@ def main(client, customer_id, ad_group_id):
88116
required=True,
89117
help="The ad group ID of the hotel ad group.",
90118
)
91-
args = parser.parse_args()
119+
args: argparse.Namespace = parser.parse_args()
92120

93121
# GoogleAdsClient will read the google-ads.yaml configuration file in the
94122
# home directory if none is specified.
95-
googleads_client = GoogleAdsClient.load_from_storage(version="v20")
123+
googleads_client: GoogleAdsClient = GoogleAdsClient.load_from_storage(
124+
version="v20"
125+
)
96126

97127
try:
98128
main(googleads_client, args.customer_id, args.ad_group_id)

0 commit comments

Comments
 (0)