Skip to content

Commit 65449c6

Browse files
authored
Add dismiss_recommendation example (#35)
1 parent 14d2fd9 commit 65449c6

1 file changed

Lines changed: 74 additions & 0 deletions

File tree

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# Copyright 2019 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
"""This example dismisses a given recommendation.
15+
16+
To retrieve recommendations for text ads, run get_text_ad_recommendations.py.
17+
"""
18+
19+
from __future__ import absolute_import
20+
21+
import argparse
22+
import six
23+
import sys
24+
25+
import google.ads.google_ads.client
26+
27+
28+
def main(client, customer_id, recommendation_id):
29+
recommendation_service = client.get_service('RecommendationService')
30+
31+
dismiss_recommendation_request = client.get_type(
32+
'DismissRecommendationRequest')
33+
34+
dismiss_recommendation_operation = (dismiss_recommendation_request.
35+
DismissRecommendationOperation())
36+
37+
dismiss_recommendation_operation.resource_name = (
38+
recommendation_service.recommendation_path(
39+
customer_id, recommendation_id))
40+
41+
try:
42+
dismissal_response = recommendation_service.dismiss_recommendation(
43+
customer_id,
44+
[dismiss_recommendation_operation])
45+
except google.ads.google_ads.errors.GoogleAdsException as ex:
46+
print('Request with ID "%s" failed with status "%s" and includes the '
47+
'following errors:' % (ex.request_id, ex.error.code().name))
48+
for error in ex.failure.errors:
49+
print('\tError with message "%s".' % error.message)
50+
if error.location:
51+
for field_path_element in error.location.field_path_elements:
52+
print('\t\tOn field: %s' % field_path_element.field_name)
53+
sys.exit(1)
54+
55+
print('Dismissed recommendation with resource name: "%s".'
56+
% dismissal_response.results[0].resource_name)
57+
58+
59+
if __name__ == '__main__':
60+
# GoogleAdsClient will read the google-ads.yaml configuration file in the
61+
# home directory if none is specified.
62+
google_ads_client = (google.ads.google_ads.client.GoogleAdsClient
63+
.load_from_storage())
64+
65+
parser = argparse.ArgumentParser(
66+
description=('Dismisses a recommendation with the given ID.'))
67+
# The following argument(s) should be provided to run the example.
68+
parser.add_argument('-c', '--customer_id', type=six.text_type,
69+
required=True, help='The Google Ads customer ID.')
70+
parser.add_argument('-r', '--recommendation_id', type=six.text_type,
71+
required=True, help='The recommendation ID.')
72+
args = parser.parse_args()
73+
74+
main(google_ads_client, args.customer_id, args.recommendation_id)

0 commit comments

Comments
 (0)