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

BigQuery: Add client_options support to Client. #19

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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 bigquery/google/cloud/bigquery/_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 = "https://www.googleapis.com"

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 = "https://www.googleapis.com"
"""The base of the API call URL."""

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

Expand Down
17 changes: 16 additions & 1 deletion bigquery/google/cloud/bigquery/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
from google.resumable_media.requests import MultipartUpload
from google.resumable_media.requests import ResumableUpload

import google.api_core.client_options
import google.api_core.exceptions
from google.api_core import page_iterator
import google.cloud._helpers
Expand Down Expand Up @@ -141,6 +142,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.
client_options (google.api_core.client_options.ClientOptions or dict):
(Optional) Client options used to set user options on the client.
API Endpoint should be set through client_options.
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a bad feeling about this.
Can't find in docs how should I write the description with several class types. Just listed them with or, as it shown in previous versions.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Quoting an already merged PR that addressed the same issue:

    :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.

Hope this helps.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, i saw that. But these are different types of docs. That's why i assumed to put just or here.


Raises:
google.auth.exceptions.DefaultCredentialsError:
Expand All @@ -162,11 +166,22 @@ def __init__(
location=None,
default_query_job_config=None,
client_info=None,
client_options=None,
):
super(Client, self).__init__(
project=project, credentials=credentials, _http=_http
)
self._connection = Connection(self, client_info=client_info)
kw_args = {"client_info": client_info}
emar-kar marked this conversation as resolved.
Show resolved Hide resolved
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

self._connection = Connection(self, **kw_args)
self._location = location
self._default_query_job_config = default_query_job_config

Expand Down
2 changes: 1 addition & 1 deletion bigquery/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 = [
'enum34; python_version < "3.4"',
"google-cloud-core >= 1.0.0, < 2.0dev",
"google-cloud-core >= 1.0.3, < 2.0dev",
"google-resumable-media >= 0.3.1",
"protobuf >= 3.6.0",
]
Expand Down
8 changes: 7 additions & 1 deletion bigquery/tests/unit/test__http.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,13 @@ def _make_one(self, *args, **kw):

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

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

def test_build_api_url_w_extra_query_params(self):
Expand Down
52 changes: 51 additions & 1 deletion bigquery/tests/unit/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,61 @@ def test_ctor_defaults(self):

creds = _make_credentials()
http = object()
client = self._make_one(project=self.PROJECT, credentials=creds, _http=http)
client = self._make_one(
project=self.PROJECT,
credentials=creds,
_http=http,
client_options={"api_endpoint": "https://www.foo-googleapis.com"},
emar-kar marked this conversation as resolved.
Show resolved Hide resolved
)
self.assertIsInstance(client._connection, Connection)
self.assertIs(client._connection.credentials, creds)
self.assertIs(client._connection.http, http)
self.assertIsNone(client.location)
self.assertEqual(
client._connection.API_BASE_URL, "https://www.foo-googleapis.com"
emar-kar marked this conversation as resolved.
Show resolved Hide resolved
)

def test_ctor_w_empty_client_options(self):
from google.cloud.bigquery._http import Connection
from google.api_core.client_options import ClientOptions

creds = _make_credentials()
http = object()
client_options = ClientOptions()
client = self._make_one(
project=self.PROJECT,
credentials=creds,
_http=http,
client_options=client_options,
)
self.assertIsInstance(client._connection, Connection)
self.assertIs(client._connection.credentials, creds)
self.assertIs(client._connection.http, http)
self.assertIsNone(client.location)
self.assertEqual(
client._connection.API_BASE_URL, client._connection.DEFAULT_API_ENDPOINT
)

def test_ctor_w_client_options_object(self):
from google.cloud.bigquery._http import Connection
from google.api_core.client_options import ClientOptions

creds = _make_credentials()
http = object()
client_options = ClientOptions(api_endpoint="https://www.foo-googleapis.com")
emar-kar marked this conversation as resolved.
Show resolved Hide resolved
client = self._make_one(
project=self.PROJECT,
credentials=creds,
_http=http,
client_options=client_options,
)
self.assertIsInstance(client._connection, Connection)
self.assertIs(client._connection.credentials, creds)
self.assertIs(client._connection.http, http)
self.assertIsNone(client.location)
self.assertEqual(
client._connection.API_BASE_URL, "https://www.foo-googleapis.com"
emar-kar marked this conversation as resolved.
Show resolved Hide resolved
)

def test_ctor_w_location(self):
from google.cloud.bigquery._http import Connection
Expand Down