Skip to content

Commit 094dadf

Browse files
authored
Add link_manager_to_client example (#117)
1 parent f1a7428 commit 094dadf

File tree

1 file changed

+113
-0
lines changed

1 file changed

+113
-0
lines changed
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
#!/usr/bin/env python
2+
# Copyright 2019 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 shows how to link a manager customer to a client customer."""
16+
17+
from __future__ import absolute_import
18+
19+
import argparse
20+
import six
21+
import sys
22+
import uuid
23+
from datetime import datetime, timedelta
24+
from google.api_core import protobuf_helpers
25+
26+
from google.ads.google_ads.client import GoogleAdsClient
27+
from google.ads.google_ads.errors import GoogleAdsException
28+
29+
30+
def main(client, customer_id, manager_customer_id):
31+
# This example assumes that the same credentials will work for both
32+
# customers, but that may not be the case. If you need to use different
33+
# credentials for each customer, then you may either update the client
34+
# configuration or instantiate two clients, where at least one points to
35+
# a specific configuration file so that both clients don't read the same
36+
# file located in the $HOME dir.
37+
38+
# Extend an invitation to the client while authenticating as the manager.
39+
client_link_operation = client.get_type(
40+
'CustomerClientLinkOperation', version='v1')
41+
client_link = client_link_operation.create
42+
client_link.client_customer.value = 'customers/{}'.format(customer_id)
43+
client_link.status = client.get_type(
44+
'ManagerLinkStatusEnum').PENDING
45+
46+
customer_client_link_service = client.get_service(
47+
'CustomerClientLinkService', version='v1')
48+
response = customer_client_link_service.mutate_customer_client_link(
49+
manager_customer_id, client_link_operation)
50+
resource_name = response.results[0].resource_name
51+
52+
print('Extended an invitation from customer #{} to customer #{} with '
53+
'client link resource_name #{}'.format(
54+
manager_customer_id, customer_id, resource_name))
55+
56+
# Find the manager_link_id of the link we just created, so we can construct
57+
# the resource name for the link from the client side. Note that since we
58+
# are filtering by resource_name, a unique identifier, only one
59+
# customer_client_link resource will be returned in the response
60+
query = '''
61+
SELECT
62+
customer_client_link.manager_link_id
63+
FROM
64+
customer_client_link
65+
WHERE
66+
customer_client_link.resource_name = "{}"
67+
'''.format(resource_name)
68+
69+
ga_service = client.get_service('GoogleAdsService', version='v1')
70+
response = ga_service.search(manager_customer_id, query=query)
71+
72+
# Since the google_ads_service.search method returns an iterator we need
73+
# to initialize an iteration in order to retrieve results, even though
74+
# we know the query will only return a single row.
75+
for row in response.result:
76+
manager_link_id = row.customer_client_link.manager_link_id
77+
78+
manager_link_operation = client.get_type(
79+
'CustomerManagerLinkOperation', version='v1')
80+
manager_link = manager_link_operation.update
81+
manager_link.resource_name.value = (
82+
'customers/{}/customerManagerLinks/{}~{}'.format(
83+
customer_id, manager_customer_id, manager_link_id))
84+
85+
manager_link.status = client.get_type('ManagerLinkStatusEnum', version='v1')
86+
field_mask = protobuf_helpers.field_mask(None, manager_link)
87+
manager_link_operation.update_mask.CopyFrom(field_mask)
88+
89+
manager_link_service = client.get_service('ManagerLinkService',
90+
version='v1')
91+
response = manager_link_service.mutate_manager_links(
92+
manager_customer_id, [manager_link_operation])
93+
resource_name = response.results[0].resource_name
94+
95+
print('Client accepted invitation with resource_name: #{}'.format(
96+
resource_name))
97+
98+
if __name__ == '__main__':
99+
# GoogleAdsClient will read the google-ads.yaml configuration file in the
100+
# home directory if none is specified.
101+
google_ads_client = GoogleAdsClient.load_from_storage()
102+
103+
parser = argparse.ArgumentParser(
104+
description= ('Links and existing manager customer to an existing'
105+
'client customer'))
106+
# The following argument(s) should be provided to run the example.
107+
parser.add_argument('-c', '--customer_id', type=six.text_type,
108+
required=True, help='The customer ID.')
109+
parser.add_argument('-m', '--manager_customer_id', type=six.text_type,
110+
required=True, help='The manager customer ID.')
111+
args = parser.parse_args()
112+
113+
main(google_ads_client, args.customer_id, args.manager_customer_id)

0 commit comments

Comments
 (0)