Skip to content

Commit 96b2376

Browse files
committed
Move download image bytes functionality to shared example helper
1 parent f4dabd0 commit 96b2376

File tree

7 files changed

+189
-69
lines changed

7 files changed

+189
-69
lines changed

examples/advanced_operations/add_demand_gen_campaign.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2024 Google LLC
1+
# Copyright 2025 Google LLC
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.
@@ -18,10 +18,10 @@
1818
"""
1919

2020
import argparse
21-
import requests
2221
import sys
2322
from uuid import uuid4
2423

24+
from examples.utils.example_helpers import get_image_bytes_from_url
2525
from google.ads.googleads.client import GoogleAdsClient
2626
from google.ads.googleads.errors import GoogleAdsException
2727

@@ -341,7 +341,7 @@ def create_image_asset_operation(
341341
# name will be dropped silently.
342342
asset.name = asset_name
343343
asset.type_ = client.enums.AssetTypeEnum.IMAGE
344-
asset.image_asset.data = requests.get(url).content
344+
asset.image_asset.data = get_image_bytes_from_url(url)
345345

346346
return mutate_operation
347347

examples/advanced_operations/add_performance_max_campaign.py

Lines changed: 41 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,11 @@
3333
import sys
3434
from uuid import uuid4
3535

36+
from examples.utils.example_helpers import get_image_bytes_from_url
3637
from google.ads.googleads.client import GoogleAdsClient
3738
from google.ads.googleads.errors import GoogleAdsException
3839
from google.ads.googleads.util import convert_snake_case_to_upper_case
3940

40-
import requests
41-
4241

4342
# We specify temporary IDs that are specific to a single mutate request.
4443
# Temporary IDs are always negative and unique within one mutate request.
@@ -106,10 +105,12 @@ def main(client, customer_id, audience_id, brand_guidelines_enabled):
106105
client,
107106
customer_id,
108107
)
109-
performance_max_campaign_operation = create_performance_max_campaign_operation(
110-
client,
111-
customer_id,
112-
brand_guidelines_enabled,
108+
performance_max_campaign_operation = (
109+
create_performance_max_campaign_operation(
110+
client,
111+
customer_id,
112+
brand_guidelines_enabled,
113+
)
113114
)
114115
campaign_criterion_operations = create_campaign_criterion_operations(
115116
client,
@@ -170,7 +171,9 @@ def create_campaign_budget_operation(
170171
campaign_budget.name = f"Performance Max campaign budget #{uuid4()}"
171172
# The budget period already defaults to DAILY.
172173
campaign_budget.amount_micros = 50000000
173-
campaign_budget.delivery_method = client.enums.BudgetDeliveryMethodEnum.STANDARD
174+
campaign_budget.delivery_method = (
175+
client.enums.BudgetDeliveryMethodEnum.STANDARD
176+
)
174177
# A Performance Max campaign cannot use a shared campaign budget.
175178
campaign_budget.explicitly_shared = False
176179

@@ -444,7 +447,9 @@ def create_asset_group_operation(
444447
for resource_name in description_asset_resource_names:
445448
mutate_operation = client.get_type("MutateOperation")
446449
asset_group_asset = mutate_operation.asset_group_asset_operation.create
447-
asset_group_asset.field_type = client.enums.AssetFieldTypeEnum.DESCRIPTION
450+
asset_group_asset.field_type = (
451+
client.enums.AssetFieldTypeEnum.DESCRIPTION
452+
)
448453
asset_group_asset.asset_group = asset_group_service.asset_group_path(
449454
customer_id,
450455
_ASSET_GROUP_TEMPORARY_ID,
@@ -530,7 +535,9 @@ def create_and_link_text_asset(client, customer_id, text, field_type):
530535
customer_id,
531536
_ASSET_GROUP_TEMPORARY_ID,
532537
)
533-
asset_group_asset.asset = asset_service.asset_path(customer_id, next_temp_id)
538+
asset_group_asset.asset = asset_service.asset_path(
539+
customer_id, next_temp_id
540+
)
534541
operations.append(mutate_operation)
535542

536543
next_temp_id -= 1
@@ -539,7 +546,9 @@ def create_and_link_text_asset(client, customer_id, text, field_type):
539546

540547

541548
# [START add_performance_max_campaign_8]
542-
def create_and_link_image_asset(client, customer_id, url, field_type, asset_name):
549+
def create_and_link_image_asset(
550+
client, customer_id, url, field_type, asset_name
551+
):
543552
"""Creates a list of MutateOperations that create a new linked image asset.
544553
545554
Args:
@@ -566,7 +575,7 @@ def create_and_link_image_asset(client, customer_id, url, field_type, asset_name
566575
# name, the new name will be dropped silently.
567576
asset.name = asset_name
568577
asset.type_ = client.enums.AssetTypeEnum.IMAGE
569-
asset.image_asset.data = get_image_bytes(url)
578+
asset.image_asset.data = get_image_bytes_from_url(url)
570579
operations.append(mutate_operation)
571580

572581
# Create an AssetGroupAsset to link the Asset to the AssetGroup.
@@ -577,7 +586,9 @@ def create_and_link_image_asset(client, customer_id, url, field_type, asset_name
577586
customer_id,
578587
_ASSET_GROUP_TEMPORARY_ID,
579588
)
580-
asset_group_asset.asset = asset_service.asset_path(customer_id, next_temp_id)
589+
asset_group_asset.asset = asset_service.asset_path(
590+
customer_id, next_temp_id
591+
)
581592
operations.append(mutate_operation)
582593

583594
next_temp_id -= 1
@@ -586,7 +597,12 @@ def create_and_link_image_asset(client, customer_id, url, field_type, asset_name
586597

587598

588599
def create_and_link_brand_assets(
589-
client, customer_id, brand_guidelines_enabled, business_name, logo_url, logo_name
600+
client,
601+
customer_id,
602+
brand_guidelines_enabled,
603+
business_name,
604+
logo_url,
605+
logo_name,
590606
):
591607
"""Creates a list of MutateOperations that create linked brand assets.
592608
@@ -612,7 +628,9 @@ def create_and_link_brand_assets(
612628

613629
text_mutate_operation = client.get_type("MutateOperation")
614630
text_asset = text_mutate_operation.asset_operation.create
615-
text_asset.resource_name = asset_service.asset_path(customer_id, text_asset_temp_id)
631+
text_asset.resource_name = asset_service.asset_path(
632+
customer_id, text_asset_temp_id
633+
)
616634
text_asset.text_asset.text = business_name
617635
operations.append(text_mutate_operation)
618636

@@ -630,7 +648,7 @@ def create_and_link_brand_assets(
630648
# name, the new name will be dropped silently.
631649
image_asset.name = logo_name
632650
image_asset.type_ = client.enums.AssetTypeEnum.IMAGE
633-
image_asset.image_asset.data = get_image_bytes(logo_url)
651+
image_asset.image_asset.data = get_image_bytes_from_url(logo_url)
634652
operations.append(image_mutate_operation)
635653

636654
if brand_guidelines_enabled:
@@ -653,7 +671,9 @@ def create_and_link_brand_assets(
653671
operations.append(business_name_mutate_operation)
654672

655673
logo_mutate_operation = client.get_type("MutateOperation")
656-
logo_campaign_asset = logo_mutate_operation.campaign_asset_operation.create
674+
logo_campaign_asset = (
675+
logo_mutate_operation.campaign_asset_operation.create
676+
)
657677
logo_campaign_asset.field_type = client.enums.AssetFieldTypeEnum.LOGO
658678
logo_campaign_asset.campaign = campaign_service.campaign_path(
659679
customer_id, _PERFORMANCE_MAX_CAMPAIGN_TEMPORARY_ID
@@ -690,9 +710,11 @@ def create_and_link_brand_assets(
690710
logo_mutate_operation.asset_group_asset_operation.create
691711
)
692712
logo_asset_group_asset.field_type = client.enums.AssetFieldTypeEnum.LOGO
693-
logo_asset_group_asset.asset_group = asset_group_service.asset_group_path(
694-
customer_id,
695-
_ASSET_GROUP_TEMPORARY_ID,
713+
logo_asset_group_asset.asset_group = (
714+
asset_group_service.asset_group_path(
715+
customer_id,
716+
_ASSET_GROUP_TEMPORARY_ID,
717+
)
696718
)
697719
logo_asset_group_asset.asset = asset_service.asset_path(
698720
customer_id, image_asset_temp_id
@@ -702,19 +724,6 @@ def create_and_link_brand_assets(
702724
return operations
703725

704726

705-
def get_image_bytes(url):
706-
"""Loads image data from a URL.
707-
708-
Args:
709-
url: a URL str.
710-
711-
Returns:
712-
Images bytes loaded from the given URL.
713-
"""
714-
response = requests.get(url)
715-
return response.content
716-
717-
718727
def print_response_details(response):
719728
"""Prints the details of a MutateGoogleAdsResponse.
720729
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
#!/usr/bin/env python
2+
# Copyright 2020 Google LLC
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# https://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
"""This code example uploads an image asset.
16+
17+
To get image assets, run get_all_image_assets.py.
18+
"""
19+
20+
21+
import argparse
22+
import sys
23+
24+
from examples.utils.example_helpers import get_image_bytes_from_url
25+
from google.ads.googleads.client import GoogleAdsClient
26+
from google.ads.googleads.errors import GoogleAdsException
27+
28+
29+
# [START upload_image_asset]
30+
def main(client, customer_id):
31+
"""Main method, to run this code example as a standalone application."""
32+
33+
# Download image from URL
34+
url = "https://gaagl.page.link/Eit5"
35+
image_content = get_image_bytes_from_url(url)
36+
37+
asset_service = client.get_service("AssetService")
38+
asset_operation = client.get_type("AssetOperation")
39+
asset = asset_operation.create
40+
asset.type_ = client.enums.AssetTypeEnum.IMAGE
41+
asset.image_asset.data = image_content
42+
asset.image_asset.file_size = len(image_content)
43+
asset.image_asset.mime_type = client.enums.MimeTypeEnum.IMAGE_JPEG
44+
# Use your favorite image library to determine dimensions
45+
asset.image_asset.full_size.height_pixels = 315
46+
asset.image_asset.full_size.width_pixels = 600
47+
asset.image_asset.full_size.url = url
48+
# Provide a unique friendly name to identify your asset.
49+
# When there is an existing image asset with the same content but a different
50+
# name, the new name will be dropped silently.
51+
asset.name = "Marketing Image"
52+
53+
mutate_asset_response = asset_service.mutate_assets(
54+
customer_id=customer_id, operations=[asset_operation]
55+
)
56+
print("Uploaded file(s):")
57+
for row in mutate_asset_response.results:
58+
print(f"\tResource name: {row.resource_name}")
59+
60+
# [END upload_image_asset]
61+
62+
63+
if __name__ == "__main__":
64+
parser = argparse.ArgumentParser(
65+
description="Upload an image asset from a URL."
66+
)
67+
# The following argument(s) should be provided to run the example.
68+
parser.add_argument(
69+
"-c",
70+
"--customer_id",
71+
type=str,
72+
required=True,
73+
help="The Google Ads customer ID.",
74+
)
75+
args = parser.parse_args()
76+
77+
# GoogleAdsClient will read the google-ads.yaml configuration file in the
78+
# home directory if none is specified.
79+
googleads_client = GoogleAdsClient.load_from_storage(version="v19")
80+
81+
try:
82+
main(googleads_client, args.customer_id)
83+
except GoogleAdsException as ex:
84+
print(
85+
f'Request with ID "{ex.request_id}" failed with status '
86+
f'"{ex.error.code().name}" and includes the following errors:'
87+
)
88+
for error in ex.failure.errors:
89+
print(f'\tError with message "{error.message}".')
90+
if error.location:
91+
for field_path_element in error.location.field_path_elements:
92+
print(f"\t\tOn field: {field_path_element.field_name}")
93+
sys.exit(1)

examples/misc/upload_image_asset.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@
2020

2121
import argparse
2222
import sys
23-
import requests
2423

2524
from google.ads.googleads.client import GoogleAdsClient
2625
from google.ads.googleads.errors import GoogleAdsException
26+
from examples.utils.example_helpers import get_image_bytes_from_url
2727

2828

2929
# [START upload_image_asset]
@@ -32,7 +32,7 @@ def main(client, customer_id):
3232

3333
# Download image from URL
3434
url = "https://gaagl.page.link/Eit5"
35-
image_content = requests.get(url).content
35+
image_content = get_image_bytes_from_url(url)
3636

3737
asset_service = client.get_service("AssetService")
3838
asset_operation = client.get_type("AssetOperation")

0 commit comments

Comments
 (0)