Skip to content

Commit ccf5016

Browse files
Add link_feed_item_set example (#352)
1 parent fe995c2 commit ccf5016

File tree

1 file changed

+134
-0
lines changed

1 file changed

+134
-0
lines changed
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
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+
"""Links the specified feed item set to the specified feed item.
16+
17+
The specified feed item set must not be created as a dynamic set, i.e. both
18+
dynamic_location_set_filter and dynamic_affiliate_location_set_filter must not
19+
be set. Learn more here:
20+
https://developers.google.com/google-ads/api/docs/location-extensions/feed-item-set?hl=en#adding-feed-item
21+
"""
22+
23+
import argparse
24+
import sys
25+
26+
from google.ads.google_ads.client import GoogleAdsClient
27+
from google.ads.google_ads.errors import GoogleAdsException
28+
from google.ads.google_ads.util import ResourceName
29+
30+
31+
def main(client, customer_id, feed_id, feed_item_id, feed_item_set_id):
32+
"""The main method that creates all necessary entities for the example.
33+
34+
Args:
35+
client: an initialized GoogleAdsClient instance.
36+
customer_id: a client customer ID.
37+
feed_id: the ID of the feed associated with the specified feed item set.
38+
feed_item_id: the ID of the specified feed item.
39+
feed_item_set_id: the ID of the feed item set to link to the feed item.
40+
"""
41+
feed_item_set_link_service = client.get_service(
42+
"FeedItemSetLinkService", version="v6"
43+
)
44+
feed_item_set_link_operation = client.get_type(
45+
"FeedItemSetLinkOperation", version="v6"
46+
)
47+
48+
# Construct an operation that will link the feed item to the feed item set.
49+
feed_item_set_link = feed_item_set_link_operation.create
50+
51+
# Construct a resource name for a feed item, which is in the format:
52+
# customers/{customer_id}/feedItems/{feed_id}~{feed_item_id}
53+
feed_item_set_link.feed_item = client.get_service(
54+
"FeedItemService", version="v6"
55+
).feed_item_path(
56+
customer_id, ResourceName.format_composite(feed_id, feed_item_id),
57+
)
58+
# Construct a resource name for a feed item set, which is in the
59+
# format: customers/{customer_id}/feedItemSets/{feed_id}~{feed_item_set_id}
60+
feed_item_set_link.feed_item_set = client.get_service(
61+
"FeedItemSetService", version="v6"
62+
).feed_item_set_path(
63+
customer_id, ResourceName.format_composite(feed_id, feed_item_set_id),
64+
)
65+
66+
# Issue a mutate request to add the feed item set link on the server.
67+
try:
68+
response = feed_item_set_link_service.mutate_feed_item_set_links(
69+
customer_id, [feed_item_set_link_operation]
70+
)
71+
print(
72+
"Created a feed item set link with resource name: "
73+
f"'{response.results[0].resource_name}'"
74+
)
75+
except GoogleAdsException as ex:
76+
print(
77+
f'Request with ID "{ex.request_id}" failed with status '
78+
f'"{ex.error.code().name}" and includes the following errors:'
79+
)
80+
for error in ex.failure.errors:
81+
print(f'\tError with message "{error.message}".')
82+
if error.location:
83+
for field_path_element in error.location.field_path_elements:
84+
print(f"\t\tOn field: {field_path_element.field_name}")
85+
sys.exit(1)
86+
87+
88+
if __name__ == "__main__":
89+
# GoogleAdsClient will read the google-ads.yaml configuration file in the
90+
# home directory if none is specified.
91+
google_ads_client = GoogleAdsClient.load_from_storage()
92+
93+
parser = argparse.ArgumentParser(
94+
description=
95+
"Links the specified feed item set to the specified feed item."
96+
)
97+
# The following argument(s) should be provided to run the example.
98+
parser.add_argument(
99+
"-c",
100+
"--customer_id",
101+
type=str,
102+
required=True,
103+
help="The Google Ads customer ID.",
104+
)
105+
parser.add_argument(
106+
"-f",
107+
"--feed_id",
108+
type=str,
109+
required=True,
110+
help="The feed ID.",
111+
)
112+
parser.add_argument(
113+
"-i",
114+
"--feed_item_id",
115+
type=str,
116+
required=True,
117+
help="The feed item ID.",
118+
)
119+
parser.add_argument(
120+
"-s",
121+
"--feed_item_set_id",
122+
type=str,
123+
required=True,
124+
help="The feed item set ID.",
125+
)
126+
args = parser.parse_args()
127+
128+
main(
129+
google_ads_client,
130+
args.customer_id,
131+
args.feed_id,
132+
args.feed_item_id,
133+
args.feed_item_set_id,
134+
)

0 commit comments

Comments
 (0)