Skip to content

Commit

Permalink
feat: allow to use 'six.moves.collections_abc.Mapping' in 'client_opt…
Browse files Browse the repository at this point in the history
…ions.from_dict()' (#943)
  • Loading branch information
paul1319 committed Jun 18, 2020
1 parent cc83ec2 commit 21af37b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
18 changes: 9 additions & 9 deletions googleapiclient/discovery.py
Expand Up @@ -213,10 +213,10 @@ def build(
cache_discovery: Boolean, whether or not to cache the discovery doc.
cache: googleapiclient.discovery_cache.base.CacheBase, an optional
cache object for the discovery documents.
client_options: Dictionary or google.api_core.client_options, Client options to set user
options on the client. API endpoint should be set through client_options.
client_cert_source is not supported, client cert should be provided using
client_encrypted_cert_source instead.
client_options: Mapping object or google.api_core.client_options, client
options to set user options on the client. The API endpoint should be set
through client_options. client_cert_source is not supported, client cert
should be provided using client_encrypted_cert_source instead.
adc_cert_path: str, client certificate file path to save the application
default client certificate for mTLS. This field is required if you want to
use the default client certificate.
Expand Down Expand Up @@ -359,10 +359,10 @@ def build_from_document(
credentials: oauth2client.Credentials or
google.auth.credentials.Credentials, credentials to be used for
authentication.
client_options: Dictionary or google.api_core.client_options, Client options to set user
options on the client. API endpoint should be set through client_options.
client_cert_source is not supported, client cert should be provided using
client_encrypted_cert_source instead.
client_options: Mapping object or google.api_core.client_options, client
options to set user options on the client. The API endpoint should be set
through client_options. client_cert_source is not supported, client cert
should be provided using client_encrypted_cert_source instead.
adc_cert_path: str, client certificate file path to save the application
default client certificate for mTLS. This field is required if you want to
use the default client certificate.
Expand Down Expand Up @@ -398,7 +398,7 @@ def build_from_document(
# If an API Endpoint is provided on client options, use that as the base URL
base = urljoin(service["rootUrl"], service["servicePath"])
if client_options:
if type(client_options) == dict:
if isinstance(client_options, six.moves.collections_abc.Mapping):
client_options = google.api_core.client_options.from_dict(client_options)
if client_options.api_endpoint:
base = client_options.api_endpoint
Expand Down
13 changes: 13 additions & 0 deletions tests/test_discovery.py
Expand Up @@ -38,6 +38,7 @@
import re
import sys
import unittest2 as unittest
from collections import defaultdict

from parameterized import parameterized
import mock
Expand Down Expand Up @@ -534,6 +535,18 @@ def test_api_endpoint_override_from_client_options(self):

self.assertEqual(plus._baseUrl, api_endpoint)

def test_api_endpoint_override_from_client_options_mapping_object(self):

discovery = open(datafile("plus.json")).read()
api_endpoint = "https://foo.googleapis.com/"
mapping_object = defaultdict(str)
mapping_object['api_endpoint'] = api_endpoint
plus = build_from_document(
discovery, client_options=mapping_object
)

self.assertEqual(plus._baseUrl, api_endpoint)

def test_api_endpoint_override_from_client_options_dict(self):
discovery = open(datafile("plus.json")).read()
api_endpoint = "https://foo.googleapis.com/"
Expand Down

0 comments on commit 21af37b

Please sign in to comment.