Skip to content

Commit 8bc0d02

Browse files
committed
Add v2 refs
Merge branch 'update-api-version-v20' into add-type-hints-custom-logging
2 parents e508fa0 + 6f79e33 commit 8bc0d02

File tree

6 files changed

+218
-104
lines changed

6 files changed

+218
-104
lines changed

examples/assets/add_call.py

Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,33 @@
1616

1717

1818
import argparse
19+
from typing import Optional, Sequence
1920
import sys
2021

2122
from google.ads.googleads.client import GoogleAdsClient
2223
from google.ads.googleads.errors import GoogleAdsException
24+
from google.ads.googleads.v20.services.types.asset_service import AssetOperation
25+
from google.ads.googleads.v20.resources.types.asset import Asset
26+
from google.ads.googleads.v20.common.types.criteria import AdScheduleInfo
27+
from google.ads.googleads.v20.enums.types.day_of_week import DayOfWeekEnum
28+
from google.ads.googleads.v20.enums.types.minute_of_hour import MinuteOfHourEnum
29+
from google.ads.googleads.v20.enums.types.call_conversion_reporting_state import CallConversionReportingStateEnum
30+
from google.ads.googleads.v20.services.types.customer_asset_service import CustomerAssetOperation
31+
from google.ads.googleads.v20.resources.types.customer_asset import CustomerAsset
32+
from google.ads.googleads.v20.enums.types.asset_field_type import AssetFieldTypeEnum
2333

2434
# Country code is a two-letter ISO-3166 code, for a list of all codes see:
2535
# https://developers.google.com/google-ads/api/reference/data/codes-formats#expandable-17
26-
_DEFAULT_PHONE_COUNTRY = "US"
36+
_DEFAULT_PHONE_COUNTRY: str = "US"
2737

2838

2939
def main(
30-
client, customer_id, phone_number, phone_country, conversion_action_id
31-
):
40+
client: GoogleAdsClient,
41+
customer_id: str,
42+
phone_number: str,
43+
phone_country: str,
44+
conversion_action_id: Optional[str],
45+
) -> None:
3246
"""The main method that creates all necessary entities for the example.
3347
3448
Args:
@@ -38,15 +52,19 @@ def main(
3852
phone_country: a two-letter ISO-3166 code.
3953
conversion_action_id: an ID for a conversion action.
4054
"""
41-
asset_resource_name = add_call_asset(
55+
asset_resource_name: str = add_call_asset(
4256
client, customer_id, phone_number, phone_country, conversion_action_id
4357
)
4458
link_asset_to_account(client, customer_id, asset_resource_name)
4559

4660

4761
def add_call_asset(
48-
client, customer_id, phone_number, phone_country, conversion_action_id
49-
):
62+
client: GoogleAdsClient,
63+
customer_id: str,
64+
phone_number: str,
65+
phone_country: str,
66+
conversion_action_id: Optional[str],
67+
) -> str:
5068
"""Creates a new asset for the call.
5169
5270
Args:
@@ -59,13 +77,13 @@ def add_call_asset(
5977
Returns:
6078
a resource name for a new call asset.
6179
"""
62-
operation = client.get_type("AssetOperation")
80+
operation: AssetOperation = client.get_type("AssetOperation")
6381
# Creates the call asset.
64-
asset = operation.create.call_asset
82+
asset: Asset = operation.create.call_asset
6583
asset.country_code = phone_country
6684
asset.phone_number = phone_number
6785
# Optional: Specifies day and time intervals for which the asset may serve.
68-
ad_schedule = client.get_type("AdScheduleInfo")
86+
ad_schedule: AdScheduleInfo = client.get_type("AdScheduleInfo")
6987
# Sets the day of this schedule as Monday.
7088
ad_schedule.day_of_week = client.enums.DayOfWeekEnum.MONDAY
7189
# Sets the start hour to 9am.
@@ -93,35 +111,39 @@ def add_call_asset(
93111
response = asset_service.mutate_assets(
94112
customer_id=customer_id, operations=[operation]
95113
)
96-
resource_name = response.results[0].resource_name
114+
resource_name: str = response.results[0].resource_name
97115
print(f"Created a call asset with resource name: '{resource_name}'")
98116

99117
return resource_name
100118

101119

102-
def link_asset_to_account(client, customer_id, asset_resource_name):
120+
def link_asset_to_account(
121+
client: GoogleAdsClient, customer_id: str, asset_resource_name: str
122+
) -> None:
103123
"""Links the call asset at the account level to serve in eligible campaigns.
104124
105125
Args:
106126
client: an initialized GoogleAdsClient instance.
107127
customer_id: a client customer ID.
108128
asset_resource_name: a resource name for the call asset.
109129
"""
110-
operation = client.get_type("CustomerAssetOperation")
111-
customer_asset = operation.create
130+
operation: CustomerAssetOperation = client.get_type(
131+
"CustomerAssetOperation"
132+
)
133+
customer_asset: CustomerAsset = operation.create
112134
customer_asset.asset = asset_resource_name
113135
customer_asset.field_type = client.enums.AssetFieldTypeEnum.CALL
114136

115137
customer_asset_service = client.get_service("CustomerAssetService")
116138
response = customer_asset_service.mutate_customer_assets(
117139
customer_id=customer_id, operations=[operation]
118140
)
119-
resource_name = response.results[0].resource_name
141+
resource_name: str = response.results[0].resource_name
120142
print(f"Created a customer asset with resource name: '{resource_name}'")
121143

122144

123145
if __name__ == "__main__":
124-
parser = argparse.ArgumentParser(
146+
parser: argparse.ArgumentParser = argparse.ArgumentParser(
125147
description=("Adds a call asset to a specific account.")
126148
)
127149
# The following argument(s) should be provided to run the example.
@@ -157,11 +179,13 @@ def link_asset_to_account(client, customer_id, asset_resource_name):
157179
help=("An optional conversion action ID to attribute conversions to."),
158180
)
159181

160-
args = parser.parse_args()
182+
args: argparse.Namespace = parser.parse_args()
161183

162184
# GoogleAdsClient will read the google-ads.yaml configuration file in the
163185
# home directory if none is specified.
164-
googleads_client = GoogleAdsClient.load_from_storage(version="v19")
186+
googleads_client: GoogleAdsClient = GoogleAdsClient.load_from_storage(
187+
version="v20"
188+
)
165189

166190
try:
167191
main(

examples/assets/add_hotel_callout.py

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,22 @@
1616

1717

1818
import argparse
19+
from typing import List, Sequence
1920
import sys
2021

2122

2223
from google.ads.googleads.client import GoogleAdsClient
2324
from google.ads.googleads.errors import GoogleAdsException
25+
from google.ads.googleads.v20.services.types.asset_service import AssetOperation
26+
from google.ads.googleads.v20.resources.types.asset import Asset
27+
from google.ads.googleads.v20.services.types.customer_asset_service import CustomerAssetOperation
28+
from google.ads.googleads.v20.resources.types.customer_asset import CustomerAsset
29+
from google.ads.googleads.v20.enums.types.asset_field_type import AssetFieldTypeEnum
2430

2531

26-
def main(client, customer_id, language_code):
32+
def main(
33+
client: GoogleAdsClient, customer_id: str, language_code: str
34+
) -> None:
2735
"""The main method that creates all necessary entities for the example.
2836
2937
Args:
@@ -32,7 +40,7 @@ def main(client, customer_id, language_code):
3240
language_code: the language of the hotel callout feed item text.
3341
"""
3442
# Creates assets for the hotel callout assets.
35-
asset_resource_names = add_hotel_callout_assets(
43+
asset_resource_names: List[str] = add_hotel_callout_assets(
3644
client, customer_id, language_code
3745
)
3846

@@ -41,7 +49,9 @@ def main(client, customer_id, language_code):
4149
link_asset_to_account(client, customer_id, asset_resource_names)
4250

4351

44-
def add_hotel_callout_assets(client, customer_id, language_code):
52+
def add_hotel_callout_assets(
53+
client: GoogleAdsClient, customer_id: str, language_code: str
54+
) -> List[str]:
4555
"""Creates new assets for the hotel callout.
4656
4757
Args:
@@ -52,11 +62,11 @@ def add_hotel_callout_assets(client, customer_id, language_code):
5262
Returns:
5363
a list of asset resource names.
5464
"""
55-
operations = []
65+
operations: List[AssetOperation] = []
5666
# Create a hotel callout asset operation for each of the below texts.
5767
for text in ["Activities", "Facilities"]:
58-
operation = client.get_type("AssetOperation")
59-
asset = operation.create
68+
operation: AssetOperation = client.get_type("AssetOperation")
69+
asset: Asset = operation.create
6070
asset.hotel_callout_asset.text = text
6171
asset.hotel_callout_asset.language_code = language_code
6272
operations.append(operation)
@@ -66,7 +76,9 @@ def add_hotel_callout_assets(client, customer_id, language_code):
6676
response = asset_service.mutate_assets(
6777
customer_id=customer_id, operations=operations
6878
)
69-
resource_names = [result.resource_name for result in response.results]
79+
resource_names: List[str] = [
80+
result.resource_name for result in response.results
81+
]
7082

7183
# Prints information about the result.
7284
for resource_name in resource_names:
@@ -78,7 +90,9 @@ def add_hotel_callout_assets(client, customer_id, language_code):
7890
return resource_names
7991

8092

81-
def link_asset_to_account(client, customer_id, resource_names):
93+
def link_asset_to_account(
94+
client: GoogleAdsClient, customer_id: str, resource_names: List[str]
95+
) -> None:
8296
"""Links Asset at the Customer level to serve in all eligible campaigns.
8397
8498
Args:
@@ -87,10 +101,12 @@ def link_asset_to_account(client, customer_id, resource_names):
87101
resource_names: a list of asset resource names.
88102
"""
89103
# Creates a CustomerAsset link for each Asset resource name provided.
90-
operations = []
104+
operations: List[CustomerAssetOperation] = []
91105
for resource_name in resource_names:
92-
operation = client.get_type("CustomerAssetOperation")
93-
asset = operation.create
106+
operation: CustomerAssetOperation = client.get_type(
107+
"CustomerAssetOperation"
108+
)
109+
asset: CustomerAsset = operation.create
94110
asset.asset = resource_name
95111
asset.field_type = client.enums.AssetFieldTypeEnum.HOTEL_CALLOUT
96112
operations.append(operation)
@@ -108,7 +124,7 @@ def link_asset_to_account(client, customer_id, resource_names):
108124

109125

110126
if __name__ == "__main__":
111-
parser = argparse.ArgumentParser(
127+
parser: argparse.ArgumentParser = argparse.ArgumentParser(
112128
description=(
113129
"Adds a hotel callout asset and adds the asset to the given "
114130
"account."
@@ -133,11 +149,13 @@ def link_asset_to_account(client, customer_id, resource_names):
133149
"https://developers.google.com/hotels/hotel-ads/api-reference/language-codes."
134150
),
135151
)
136-
args = parser.parse_args()
152+
args: argparse.Namespace = parser.parse_args()
137153

138154
# GoogleAdsClient will read the google-ads.yaml configuration file in the
139155
# home directory if none is specified.
140-
googleads_client = GoogleAdsClient.load_from_storage(version="v19")
156+
googleads_client: GoogleAdsClient = GoogleAdsClient.load_from_storage(
157+
version="v20"
158+
)
141159

142160
try:
143161
main(googleads_client, args.customer_id, args.language_code)

0 commit comments

Comments
 (0)