Skip to content

Commit 8b660d6

Browse files
authored
Added the get_feed_items_of_feed_item_set example. (googleads#355)
1 parent 8a2dad0 commit 8b660d6

1 file changed

Lines changed: 108 additions & 0 deletions

File tree

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
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 example gets all feed items of the specified feed item set.
16+
17+
To create a new feed item set, run create_feed_item_set.py.
18+
To link a feed item to a feed item set, run link_feed_item_set.py.
19+
"""
20+
21+
22+
import argparse
23+
import sys
24+
25+
from google.ads.google_ads.client import GoogleAdsClient
26+
from google.ads.google_ads.errors import GoogleAdsException
27+
from google.ads.google_ads.util import ResourceName
28+
29+
30+
def main(client, customer_id, feed_id, feed_item_set_id):
31+
"""The main method that creates all necessary entities for the example.
32+
33+
Args:
34+
client: an initialized GoogleAdsClient instance.
35+
customer_id: a client customer ID.
36+
feed_id: the ID for a Feed belonging to the given customer.
37+
feed_item_set_id: the ID for a FeedItemSet belonging to the given
38+
customer.
39+
"""
40+
ga_service = client.get_service("GoogleAdsService", version="v6")
41+
feed_item_set_service = client.get_service(
42+
"FeedItemSetService", version="v6"
43+
)
44+
45+
feed_item_set_path = feed_item_set_service.feed_item_set_path(
46+
customer_id, ResourceName.format_composite(feed_id, feed_item_set_id),
47+
)
48+
query = f"""
49+
SELECT
50+
feed_item_set_link.feed_item
51+
FROM feed_item_set_link
52+
WHERE feed_item_set_link.feed_item_set = '{feed_item_set_path}'"""
53+
54+
# Issues a search request using streaming.
55+
response = ga_service.search_stream(customer_id, query=query)
56+
57+
print(
58+
"The feed items with the following resource names are linked with "
59+
f"the feed item set with ID {feed_item_set_id}:"
60+
)
61+
try:
62+
for batch in response:
63+
for row in batch.results:
64+
print(f"'{row.feed_item_set_link.feed_item}'")
65+
except GoogleAdsException as ex:
66+
print(
67+
f'Request with ID "{ex.request_id}" failed with status '
68+
f'"{ex.error.code().name}" and includes the following errors:'
69+
)
70+
for error in ex.failure.errors:
71+
print(f'\tError with message "{error.message}".')
72+
if error.location:
73+
for field_path_element in error.location.field_path_elements:
74+
print(f"\t\tOn field: {field_path_element.field_name}")
75+
sys.exit(1)
76+
77+
78+
if __name__ == "__main__":
79+
# GoogleAdsClient will read the google-ads.yaml configuration file in the
80+
# home directory if none is specified.
81+
google_ads_client = GoogleAdsClient.load_from_storage()
82+
83+
parser = argparse.ArgumentParser(
84+
description="Gets all feed items of the specified feed item set."
85+
)
86+
# The following argument(s) should be provided to run the example.
87+
parser.add_argument(
88+
"-c",
89+
"--customer_id",
90+
type=str,
91+
required=True,
92+
help="The Google Ads customer ID.",
93+
)
94+
parser.add_argument(
95+
"-i", "--feed_id", type=str, required=True, help="The Feed ID.",
96+
)
97+
parser.add_argument(
98+
"-s",
99+
"--feed_item_set_id",
100+
type=str,
101+
required=True,
102+
help="The Feed Item Set ID.",
103+
)
104+
args = parser.parse_args()
105+
106+
main(
107+
google_ads_client, args.customer_id, args.feed_id, args.feed_item_set_id
108+
)

0 commit comments

Comments
 (0)