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

Storage: Add client_options to constructors for manual clients. #9054

Merged
merged 3 commits into from
Aug 28, 2019
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions storage/google/cloud/storage/_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,14 @@ class Connection(_http.JSONConnection):
:param client_info: (Optional) instance used to generate user agent.
"""

def __init__(self, client, client_info=None):
super(Connection, self).__init__(client, client_info)
DEFAULT_API_ENDPOINT = _http.API_BASE_URL

def __init__(self, client, client_info=None, api_endpoint=DEFAULT_API_ENDPOINT):
super(Connection, self).__init__(client, client_info)
self.API_BASE_URL = api_endpoint
self._client_info.gapic_version = __version__
self._client_info.client_library_version = __version__

API_BASE_URL = _http.API_BASE_URL
"""The base of the API call URL."""

API_VERSION = "v1"
"""The version of the API, used in building the API call's URL."""

Expand Down
27 changes: 25 additions & 2 deletions storage/google/cloud/storage/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

"""Client for interacting with the Google Cloud Storage API."""

import google.api_core.client_options

from six.moves.urllib.parse import urlsplit

from google.auth.credentials import AnonymousCredentials
Expand Down Expand Up @@ -60,6 +62,9 @@ class Client(ClientWithProject):
requests. If ``None``, then default info will be used. Generally,
you only need to set this if you're developing your own library
or partner tool.
:type client_options: :class:`~google.api_core.client_options.ClientOptions` or :class:`dict`
:param client_options: (Optional) Client options used to set user options on the client.
API Endpoint should be set through client_options.
"""

SCOPE = (
Expand All @@ -69,7 +74,14 @@ class Client(ClientWithProject):
)
"""The scopes required for authenticating as a Cloud Storage consumer."""

def __init__(self, project=_marker, credentials=None, _http=None, client_info=None):
def __init__(
self,
project=_marker,
credentials=None,
_http=None,
client_info=None,
client_options=None,
):
self._base_connection = None
if project is None:
no_project = True
Expand All @@ -81,9 +93,20 @@ def __init__(self, project=_marker, credentials=None, _http=None, client_info=No
super(Client, self).__init__(
project=project, credentials=credentials, _http=_http
)

kw_args = {"client_info": client_info}
if client_options:
if type(client_options) == dict:
client_options = google.api_core.client_options.from_dict(
client_options
)
if client_options.api_endpoint:
api_endpoint = client_options.api_endpoint
kw_args["api_endpoint"] = api_endpoint

if no_project:
self.project = None
self._connection = Connection(self, client_info=client_info)
self._connection = Connection(self, **kw_args)
self._batch_stack = _LocalStack()

@classmethod
Expand Down
2 changes: 1 addition & 1 deletion storage/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
release_status = "Development Status :: 5 - Production/Stable"
dependencies = [
"google-auth >= 1.2.0",
"google-cloud-core >= 1.0.0, < 2.0dev",
"google-cloud-core >= 1.0.3, < 2.0dev",
"google-resumable-media >= 0.3.1",
]
extras = {}
Expand Down
8 changes: 7 additions & 1 deletion storage/tests/unit/test__http.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,13 @@ def test_extra_headers(self):

def test_build_api_url_no_extra_query_params(self):
conn = self._make_one(object())
URI = "/".join([conn.API_BASE_URL, "storage", conn.API_VERSION, "foo"])
URI = "/".join([conn.DEFAULT_API_ENDPOINT, "storage", conn.API_VERSION, "foo"])
self.assertEqual(conn.build_api_url("/foo"), URI)

def test_build_api_url_w_custom_endpoint(self):
custom_endpoint = "https://foo-googleapis.com"
conn = self._make_one(object(), api_endpoint=custom_endpoint)
URI = "/".join([custom_endpoint, "storage", conn.API_VERSION, "foo"])
self.assertEqual(conn.build_api_url("/foo"), URI)

def test_build_api_url_w_extra_query_params(self):
Expand Down
47 changes: 47 additions & 0 deletions storage/tests/unit/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,53 @@ def test_ctor_connection_type(self):
self.assertIsNone(client.current_batch)
self.assertEqual(list(client._batch_stack), [])
self.assertIsInstance(client._connection._client_info, ClientInfo)
self.assertEqual(
client._connection.API_BASE_URL, Connection.DEFAULT_API_ENDPOINT
)

def test_ctor_w_empty_client_options(self):
from google.api_core.client_options import ClientOptions

PROJECT = "PROJECT"
credentials = _make_credentials()
client_options = ClientOptions()

client = self._make_one(
project=PROJECT, credentials=credentials, client_options=client_options
)

self.assertEqual(
client._connection.API_BASE_URL, client._connection.DEFAULT_API_ENDPOINT
)

def test_ctor_w_client_options_dict(self):

PROJECT = "PROJECT"
credentials = _make_credentials()
client_options = {"api_endpoint": "https://www.foo-googleapis.com"}

client = self._make_one(
project=PROJECT, credentials=credentials, client_options=client_options
)

self.assertEqual(
client._connection.API_BASE_URL, "https://www.foo-googleapis.com"
)

def test_ctor_w_client_options_object(self):
from google.api_core.client_options import ClientOptions

PROJECT = "PROJECT"
credentials = _make_credentials()
client_options = ClientOptions(api_endpoint="https://www.foo-googleapis.com")

client = self._make_one(
project=PROJECT, credentials=credentials, client_options=client_options
)

self.assertEqual(
client._connection.API_BASE_URL, "https://www.foo-googleapis.com"
)

def test_ctor_wo_project(self):
from google.cloud.storage._http import Connection
Expand Down