Skip to content

Commit

Permalink
Changes for release v6_0 and deprecation of v2_0. (#335)
Browse files Browse the repository at this point in the history
  • Loading branch information
wihl authored Nov 9, 2020
1 parent 599541b commit 8ed125a
Show file tree
Hide file tree
Showing 2,943 changed files with 205,156 additions and 170,682 deletions.
6 changes: 6 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
* 8.0.0
- Google Ads v6_0 release
- Deprecate v2_0
- Update all examples to support migration from wrapper types to primitives.
- Add masking logic to prevent PII in requests/responses from being logged.

* 7.0.0
- Google Ads v5_0 release
- Updates to examples to support addition of field presence on certain
Expand Down
2 changes: 2 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ Authors
* `Mark Saniscalchi`_
* `David Wihl`_
* `Ben Karl`_
* `Andrew Burke`_

.. |build-status| image:: https://storage.googleapis.com/gaa-clientlibs/badges/google-ads-python/buildstatus_ubuntu.svg
.. _Developer Site: https://developers.google.com/google-ads/api/docs/client-libs/python/
Expand All @@ -50,3 +51,4 @@ Authors
.. _Mark Saniscalchi: https://github.com/msaniscalchi
.. _David Wihl: https://github.com/wihl
.. _Ben Karl: https://github.com/BenRKarl
.. _Andrew Burke: https://github.com/AndrewMBurke
4 changes: 2 additions & 2 deletions examples/account_management/create_customer.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@


def main(client, manager_customer_id):
customer_service = client.get_service("CustomerService", version="v5")
customer = client.get_type("Customer", version="v5")
customer_service = client.get_service("CustomerService", version="v6")
customer = client.get_type("Customer", version="v6")
today = datetime.today().strftime("%Y%m%d %H:%M:%S")
customer.descriptive_name = (
"Account created with " "CustomerService on %s" % today
Expand Down
8 changes: 4 additions & 4 deletions examples/account_management/get_account_changes.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def resource_name_for_resource_type(resource_type, row):


def main(client, customer_id):
ads_service = client.get_service("GoogleAdsService", version="v5")
ads_service = client.get_service("GoogleAdsService", version="v6")
query = """
SELECT
change_status.resource_name,
Expand All @@ -77,10 +77,10 @@ def main(client, customer_id):
)

resource_type_enum = client.get_type(
"ChangeStatusResourceTypeEnum", version="v5"
"ChangeStatusResourceTypeEnum", version="v6"
).ChangeStatusResourceType
change_status_operation_enum = client.get_type(
"ChangeStatusOperationEnum", version="v5"
"ChangeStatusOperationEnum", version="v6"
).ChangeStatusOperation

try:
Expand All @@ -95,7 +95,7 @@ def main(client, customer_id):
'On "%s", change status "%s" shows a resource type of "%s" '
'with resource name "%s" was "%s".'
% (
row.change_status.last_change_date_time.value,
row.change_status.last_change_date_time,
row.change_status.resource_name,
resource_type,
resource_name_for_resource_type(resource_type, row),
Expand Down
26 changes: 12 additions & 14 deletions examples/account_management/get_account_hierarchy.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ def main(client, login_customer_id=None):
"""

# Gets instances of the GoogleAdsService and CustomerService clients.
google_ads_service = client.get_service("GoogleAdsService", version="v5")
customer_service = client.get_service("CustomerService", version="v5")
google_ads_service = client.get_service("GoogleAdsService", version="v6")
customer_service = client.get_service("CustomerService", version="v6")

# A collection of customer IDs to handle.
seed_customer_ids = []
Expand Down Expand Up @@ -99,7 +99,7 @@ def main(client, login_customer_id=None):

# The customer client that with level 0 is the specified
# customer.
if customer_client.level.value == 0:
if customer_client.level == 0:
if root_customer_client is None:
root_customer_client = customer_client
continue
Expand All @@ -115,23 +115,21 @@ def main(client, login_customer_id=None):
customer_client
)

if customer_client.manager.value:
if customer_client.manager:
# A customer can be managed by multiple managers, so to
# prevent visiting the same customer many times, we
# need to check if it's already in the Dictionary.
if (
customer_client.id.value
customer_client.id
not in customer_ids_to_child_accounts
and customer_client.level.value == 1
and customer_client.level == 1
):
unprocessed_customer_ids.append(
customer_client.id.value
)
unprocessed_customer_ids.append(customer_client.id)

if root_customer_client is not None:
print(
"The hierarchy of customer ID "
f"{root_customer_client.id.value} is printed below:"
f"{root_customer_client.id} is printed below:"
)
print_account_hierarchy(
root_customer_client, customer_ids_to_child_accounts, 0
Expand Down Expand Up @@ -172,12 +170,12 @@ def print_account_hierarchy(
if depth == 0:
print("Customer ID (Descriptive Name, Currency Code, Time Zone)")

customer_id = str(customer_client.id.value)
customer_id = str(customer_client.id)
print("-" * (depth * 2), end="")
print(
f"{customer_id} ({customer_client.descriptive_name.value}, "
f"{customer_client.currency_code.value}, "
f"{customer_client.time_zone.value})"
f"{customer_id} ({customer_client.descriptive_name}, "
f"{customer_client.currency_code}, "
f"{customer_client.time_zone})"
)

# Recursively call this function for all child accounts of customer_client.
Expand Down
2 changes: 1 addition & 1 deletion examples/account_management/get_account_information.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@


def main(client, customer_id):
customer_service = client.get_service("CustomerService", version="v5")
customer_service = client.get_service("CustomerService", version="v6")

resource_name = customer_service.customer_path(customer_id)

Expand Down
16 changes: 8 additions & 8 deletions examples/account_management/link_manager_to_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ def main(client, customer_id, manager_customer_id):

# Extend an invitation to the client while authenticating as the manager.
client_link_operation = client.get_type(
"CustomerClientLinkOperation", version="v5"
"CustomerClientLinkOperation", version="v6"
)
client_link = client_link_operation.create
client_link.client_customer.value = "customers/{}".format(customer_id)
client_link.client_customer = "customers/{}".format(customer_id)
client_link.status = client.get_type("ManagerLinkStatusEnum").PENDING

customer_client_link_service = client.get_service(
"CustomerClientLinkService", version="v5"
"CustomerClientLinkService", version="v6"
)
response = customer_client_link_service.mutate_customer_client_link(
manager_customer_id, client_link_operation
Expand All @@ -62,7 +62,7 @@ def main(client, customer_id, manager_customer_id):
FROM customer_client_link
WHERE customer_client_link.resource_name = '{resource_name}'"""

ga_service = client.get_service("GoogleAdsService", version="v5")
ga_service = client.get_service("GoogleAdsService", version="v6")
response = ga_service.search(manager_customer_id, query=query)

# Since the google_ads_service.search method returns an iterator we need
Expand All @@ -72,19 +72,19 @@ def main(client, customer_id, manager_customer_id):
manager_link_id = row.customer_client_link.manager_link_id

manager_link_operation = client.get_type(
"CustomerManagerLinkOperation", version="v5"
"CustomerManagerLinkOperation", version="v6"
)
manager_link = manager_link_operation.update
manager_link.resource_name = "customers/{}/customerManagerLinks/{}~{}".format(
customer_id, manager_customer_id, manager_link_id
)

manager_link.status = client.get_type("ManagerLinkStatusEnum", version="v5")
manager_link.status = client.get_type("ManagerLinkStatusEnum", version="v6")
field_mask = protobuf_helpers.field_mask(None, manager_link)
manager_link_operation.update_mask.CopyFrom(field_mask)

manager_link_service = client.get_service(
"ManagerLinkService", version="v5"
"ManagerLinkService", version="v6"
)
response = manager_link_service.mutate_manager_links(
manager_customer_id, [manager_link_operation]
Expand All @@ -105,7 +105,7 @@ def main(client, customer_id, manager_customer_id):

parser = argparse.ArgumentParser(
description=(
"Links and existing manager customer to an existing"
"Links an existing manager customer to an existing"
"client customer"
)
)
Expand Down
2 changes: 1 addition & 1 deletion examples/account_management/list_accessible_customers.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@


def main(client):
customer_service = client.get_service("CustomerService", version="v5")
customer_service = client.get_service("CustomerService", version="v6")

try:
accessible_customers = customer_service.list_accessible_customers()
Expand Down
170 changes: 170 additions & 0 deletions examples/account_management/reject_merchant_center_link.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
#!/usr/bin/env python
# Copyright 2020 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Demonstrates how to reject or unlink a Merchant Center link request.
Prerequisite: You need to have access to a Merchant Center account. You can find
instructions to create a Merchant Center account here:
https://support.google.com/merchants/answer/188924.
To run this example, you must use the Merchant Center UI or the Content API for
Shopping to send a link request between your Merchant Center and Google Ads
accounts. You can find detailed instructions to link your Merchant Center and
Google Ads accounts here: https://support.google.com/merchants/answer/6159060.
"""

import argparse
import sys

from google.ads.google_ads.client import GoogleAdsClient
from google.ads.google_ads.errors import GoogleAdsException


def main(client, customer_id, merchant_center_account_id):
"""Demonstrates how to reject a Merchant Center link request.
Args:
client: An initialized Google Ads client.
customer_id: The Google Ads customer ID.
merchant_center_account_id: The Merchant Center account ID for the
account requesting to link.
"""
# Get the MerchantCenterLinkService client.
merchant_center_link_service = client.get_service(
"MerchantCenterLinkService", version="v6"
)
try:
# Get the extant customer account to Merchant Center account links.
list_merchant_center_links_response = merchant_center_link_service.list_merchant_center_links(
customer_id
)

number_of_links = len(
list_merchant_center_links_response.merchant_center_links
)

if number_of_links <= 0:
print(
"There are no current merchant center links to Google Ads "
f"account {customer_id}. This example will now exit."
)
return

print(
f"{number_of_links} Merchant Center link(s) found with the "
"following details:"
)

merchant_center_link_status_enum = client.get_type(
"MerchantCenterLinkStatusEnum", version="v6"
).MerchantCenterLinkStatus

for merchant_center_link in list_merchant_center_links_response.merchant_center_links:
print(
f"\tLink '{merchant_center_link.resource_name}' has status "
f"'{merchant_center_link_status_enum.Name(merchant_center_link.status)}'."
)

# Check if this is the link to the target Merchant Center account.
if merchant_center_link.id == merchant_center_account_id:
# A Merchant Center link can be pending or enabled; in both
# cases, we reject it by removing the link.
_remove_merchant_center_link(
client,
merchant_center_link_service,
customer_id,
merchant_center_link,
)

# We can terminate early since this example concerns only one
# Google Ads account to Merchant Center account link.
return

# Raise an exception if no matching Merchant Center link was found.
raise ValueError(
"No link could was found between Google Ads account "
f"{customer_id} and Merchant Center account "
f"{merchant_center_account_id}."
)

except GoogleAdsException as ex:
print(
f'Request with ID "{ex.request_id}" failed with status '
f'"{ex.error.code().name}" and includes the following errors:'
)
for error in ex.failure.errors:
print(f'\tError with message "{error.message}".')
if error.location:
for field_path_element in error.location.field_path_elements:
print(f"\t\tOn field: {field_path_element.field_name}")
sys.exit(1)


def _remove_merchant_center_link(
client, merchant_center_link_service, customer_id, merchant_center_link
):
"""Removes a Merchant Center link from a Google Ads client customer account.
Args:
client: An initialized Google Ads client.
merchant_center_link_service: An initialized
MerchantCenterLinkService client.
customer_id: The Google Ads customer ID of the account that has the link
request.
merchant_center_link: The MerchantCenterLink object to remove.
"""
# Create a single remove operation, specifying the Merchant Center link
# resource name.
operation = client.get_type("MerchantCenterLinkOperation", version="v6")
operation.remove = merchant_center_link.resource_name

# Send the operation in a mutate request.
response = merchant_center_link_service.mutate_merchant_center_link(
customer_id, operation
)
print(
"Removed Merchant Center link with resource name "
f"'{response.result.resource_name}'."
)


if __name__ == "__main__":
# GoogleAdsClient will read the google-ads.yaml configuration file in the
# home directory if none is specified.
google_ads_client = GoogleAdsClient.load_from_storage()

parser = argparse.ArgumentParser(
description=(
"Demonstrates how to reject a Merchant Center link request."
)
)
# The following argument(s) should be provided to run the example.
parser.add_argument(
"-c",
"--customer_id",
type=str,
required=True,
help="The Google Ads customer ID.",
)
parser.add_argument(
"-m",
"--merchant_center_account_id",
type=int,
required=True,
help="The Merchant Center account ID for the account requesting to "
"link.",
)
args = parser.parse_args()

main(google_ads_client, args.customer_id, args.merchant_center_account_id)
Loading

0 comments on commit 8ed125a

Please sign in to comment.