Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Change default of static_discovery when discoveryServiceUrl set #1261

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion UPGRADING.md
Expand Up @@ -28,7 +28,10 @@ discovery document is private and it will not be shipped with the library.
Only discovery documents listed in [this public directory](https://www.googleapis.com/discovery/v1/apis/)
are included in the library. Users of private APIs should set the
`static_discovery` argument of `discovery.build()` to `False` to continue to
retrieve the service definition from the internet.
retrieve the service definition from the internet. As of version 2.1.0,
for backwards compatability with version 1.x, if `static_discovery` is not
specified, the default value for `static_discovery` will be `True` when
the `discoveryServiceUrl` argument of `discovery.build()` is provided.

If you experience issues or have questions, please file an [issue](https://github.com/googleapis/google-api-python-client/issues).

Expand Down
24 changes: 21 additions & 3 deletions googleapiclient/discovery.py
Expand Up @@ -182,7 +182,7 @@ def build(
serviceName,
version,
http=None,
discoveryServiceUrl=DISCOVERY_URI,
discoveryServiceUrl=None,
developerKey=None,
model=None,
requestBuilder=HttpRequest,
Expand All @@ -193,7 +193,7 @@ def build(
adc_cert_path=None,
adc_key_path=None,
num_retries=1,
static_discovery=True,
static_discovery=None,
):
"""Construct a Resource for interacting with an API.

Expand Down Expand Up @@ -248,7 +248,10 @@ def build(
num_retries: Integer, number of times to retry discovery with
randomized exponential backoff in case of intermittent/connection issues.
static_discovery: Boolean, whether or not to use the static discovery docs
included in the library.
included in the library. The default value for `static_discovery` depends
on the value of `discoveryServiceUrl`. `static_discovery` will default to
`True` when `discoveryServiceUrl` is also not provided, otherwise it will
default to `False`.

Returns:
A Resource object with methods for interacting with the service.
Expand All @@ -259,6 +262,21 @@ def build(
"""
params = {"api": serviceName, "apiVersion": version}

# The default value for `static_discovery` depends on the value of
# `discoveryServiceUrl`. `static_discovery` will default to `True` when
# `discoveryServiceUrl` is also not provided, otherwise it will default to
# `False`. This is added for backwards compatability with
# google-api-python-client 1.x which does not support the `static_discovery`
# parameter.
if static_discovery is None:
if discoveryServiceUrl is None:
static_discovery = True
else:
static_discovery = False

if discoveryServiceUrl is None:
discoveryServiceUrl = DISCOVERY_URI

if http is None:
discovery_http = build_http()
else:
Expand Down
8 changes: 3 additions & 5 deletions tests/test_discovery.py
Expand Up @@ -592,7 +592,7 @@ def test_building_with_context_manager(self):

def test_resource_close(self):
discovery = read_datafile("plus.json")

with mock.patch("httplib2.Http", autospec=True) as httplib2_http:
http = httplib2_http()
plus = build_from_document(
Expand Down Expand Up @@ -927,8 +927,7 @@ def test_userip_is_added_to_discovery_uri(self):
"v1",
http=http,
developerKey=None,
discoveryServiceUrl="http://example.com",
static_discovery=False,
discoveryServiceUrl="http://example.com"
)
self.fail("Should have raised an exception.")
except HttpError as e:
Expand All @@ -946,8 +945,7 @@ def test_userip_missing_is_not_added_to_discovery_uri(self):
"v1",
http=http,
developerKey=None,
discoveryServiceUrl="http://example.com",
static_discovery=False,
discoveryServiceUrl="http://example.com"
)
self.fail("Should have raised an exception.")
except HttpError as e:
Expand Down