Skip to content
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
20 changes: 11 additions & 9 deletions bigquery/google/cloud/bigquery/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,25 +60,27 @@ class Client(ClientWithProject):

:type credentials: :class:`~google.auth.credentials.Credentials`
:param credentials: (Optional) The OAuth2 Credentials to use for this
client. If not passed (and if no ``http`` object is
client. If not passed (and if no ``_http`` object is
passed), falls back to the default inferred from the
environment.

:type http: :class:`~httplib2.Http`
:param http: (Optional) HTTP object to make requests. Can be any object
that defines ``request()`` with the same interface as
:meth:`~httplib2.Http.request`. If not passed, an
``http`` object is created that is bound to the
``credentials`` for the current object.
:type _http: :class:`~httplib2.Http`
:param _http: (Optional) HTTP object to make requests. Can be any object
that defines ``request()`` with the same interface as
:meth:`~httplib2.Http.request`. If not passed, an
``_http`` object is created that is bound to the
``credentials`` for the current object.
This parameter should be considered private, and could
change in the future.
"""

SCOPE = ('https://www.googleapis.com/auth/bigquery',
'https://www.googleapis.com/auth/cloud-platform')
"""The scopes required for authenticating as a BigQuery consumer."""

def __init__(self, project=None, credentials=None, http=None):
def __init__(self, project=None, credentials=None, _http=None):
super(Client, self).__init__(
project=project, credentials=credentials, http=http)
project=project, credentials=credentials, _http=_http)
self._connection = Connection(self)

def list_projects(self, max_results=None, page_token=None):
Expand Down
22 changes: 11 additions & 11 deletions bigquery/tests/unit/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def test_ctor(self):
PROJECT = 'PROJECT'
creds = _make_credentials()
http = object()
client = self._make_one(project=PROJECT, credentials=creds, http=http)
client = self._make_one(project=PROJECT, credentials=creds, _http=http)
self.assertIsInstance(client._connection, Connection)
self.assertIs(client._connection.credentials, creds)
self.assertIs(client._connection.http, http)
Expand Down Expand Up @@ -195,7 +195,7 @@ def test_dataset(self):
DATASET = 'dataset_name'
creds = _make_credentials()
http = object()
client = self._make_one(project=PROJECT, credentials=creds, http=http)
client = self._make_one(project=PROJECT, credentials=creds, _http=http)
dataset = client.dataset(DATASET)
self.assertIsInstance(dataset, Dataset)
self.assertEqual(dataset.name, DATASET)
Expand Down Expand Up @@ -438,7 +438,7 @@ def test_load_table_from_storage(self):
SOURCE_URI = 'http://example.com/source.csv'
creds = _make_credentials()
http = object()
client = self._make_one(project=PROJECT, credentials=creds, http=http)
client = self._make_one(project=PROJECT, credentials=creds, _http=http)
dataset = client.dataset(DATASET)
destination = dataset.table(DESTINATION)
job = client.load_table_from_storage(JOB, destination, SOURCE_URI)
Expand All @@ -458,7 +458,7 @@ def test_copy_table(self):
DESTINATION = 'destination_table'
creds = _make_credentials()
http = object()
client = self._make_one(project=PROJECT, credentials=creds, http=http)
client = self._make_one(project=PROJECT, credentials=creds, _http=http)
dataset = client.dataset(DATASET)
source = dataset.table(SOURCE)
destination = dataset.table(DESTINATION)
Expand All @@ -479,7 +479,7 @@ def test_extract_table_to_storage(self):
DESTINATION = 'gs://bucket_name/object_name'
creds = _make_credentials()
http = object()
client = self._make_one(project=PROJECT, credentials=creds, http=http)
client = self._make_one(project=PROJECT, credentials=creds, _http=http)
dataset = client.dataset(DATASET)
source = dataset.table(SOURCE)
job = client.extract_table_to_storage(JOB, source, DESTINATION)
Expand All @@ -497,7 +497,7 @@ def test_run_async_query_defaults(self):
QUERY = 'select count(*) from persons'
creds = _make_credentials()
http = object()
client = self._make_one(project=PROJECT, credentials=creds, http=http)
client = self._make_one(project=PROJECT, credentials=creds, _http=http)
job = client.run_async_query(JOB, QUERY)
self.assertIsInstance(job, QueryJob)
self.assertIs(job._client, client)
Expand All @@ -516,7 +516,7 @@ def test_run_async_w_udf_resources(self):
QUERY = 'select count(*) from persons'
creds = _make_credentials()
http = object()
client = self._make_one(project=PROJECT, credentials=creds, http=http)
client = self._make_one(project=PROJECT, credentials=creds, _http=http)
udf_resources = [UDFResource("resourceUri", RESOURCE_URI)]
job = client.run_async_query(JOB, QUERY, udf_resources=udf_resources)
self.assertIsInstance(job, QueryJob)
Expand All @@ -535,7 +535,7 @@ def test_run_async_w_query_parameters(self):
QUERY = 'select count(*) from persons'
creds = _make_credentials()
http = object()
client = self._make_one(project=PROJECT, credentials=creds, http=http)
client = self._make_one(project=PROJECT, credentials=creds, _http=http)
query_parameters = [ScalarQueryParameter('foo', 'INT64', 123)]
job = client.run_async_query(JOB, QUERY,
query_parameters=query_parameters)
Expand All @@ -553,7 +553,7 @@ def test_run_sync_query_defaults(self):
QUERY = 'select count(*) from persons'
creds = _make_credentials()
http = object()
client = self._make_one(project=PROJECT, credentials=creds, http=http)
client = self._make_one(project=PROJECT, credentials=creds, _http=http)
query = client.run_sync_query(QUERY)
self.assertIsInstance(query, QueryResults)
self.assertIs(query._client, client)
Expand All @@ -571,7 +571,7 @@ def test_run_sync_query_w_udf_resources(self):
QUERY = 'select count(*) from persons'
creds = _make_credentials()
http = object()
client = self._make_one(project=PROJECT, credentials=creds, http=http)
client = self._make_one(project=PROJECT, credentials=creds, _http=http)
udf_resources = [UDFResource("resourceUri", RESOURCE_URI)]
query = client.run_sync_query(QUERY, udf_resources=udf_resources)
self.assertIsInstance(query, QueryResults)
Expand All @@ -589,7 +589,7 @@ def test_run_sync_query_w_query_parameters(self):
QUERY = 'select count(*) from persons'
creds = _make_credentials()
http = object()
client = self._make_one(project=PROJECT, credentials=creds, http=http)
client = self._make_one(project=PROJECT, credentials=creds, _http=http)
query_parameters = [ScalarQueryParameter('foo', 'INT64', 123)]
query = client.run_sync_query(QUERY, query_parameters=query_parameters)
self.assertIsInstance(query, QueryResults)
Expand Down
50 changes: 27 additions & 23 deletions core/google/cloud/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,12 @@ def from_service_account_json(cls, json_credentials_path, *args, **kwargs):
class Client(_ClientFactoryMixin):
"""Client to bundle configuration needed for API requests.

Stores ``credentials`` and ``http`` object so that subclasses
Stores ``credentials`` and an HTTP object so that subclasses
can pass them along to a connection class.

If no value is passed in for ``http``, a :class:`httplib2.Http` object
If no value is passed in for ``_http``, a :class:`httplib2.Http` object
will be created and authorized with the ``credentials``. If not, the
``credentials`` and ``http`` need not be related.
``credentials`` and ``_http`` need not be related.

Callers and subclasses may seek to use the private key from
``credentials`` to sign data.
Expand All @@ -92,21 +92,23 @@ class Client(_ClientFactoryMixin):

In addition, ``redirections`` and ``connection_type`` may be used.

A custom ``http`` object will also need to be able to add a bearer token
A custom ``_http`` object will also need to be able to add a bearer token
to API requests and handle token refresh on 401 errors.

:type credentials: :class:`~google.auth.credentials.Credentials`
:param credentials: (Optional) The OAuth2 Credentials to use for this
client. If not passed (and if no ``http`` object is
client. If not passed (and if no ``_http`` object is
passed), falls back to the default inferred from the
environment.

:type http: :class:`~httplib2.Http`
:param http: (Optional) HTTP object to make requests. Can be any object
that defines ``request()`` with the same interface as
:meth:`~httplib2.Http.request`. If not passed, an
``http`` object is created that is bound to the
``credentials`` for the current object.
:type _http: :class:`~httplib2.Http`
:param _http: (Optional) HTTP object to make requests. Can be any object
that defines ``request()`` with the same interface as
:meth:`~httplib2.Http.request`. If not passed, an
``_http`` object is created that is bound to the
``credentials`` for the current object.
This parameter should be considered private, and could
change in the future.
"""

SCOPE = None
Expand All @@ -115,16 +117,16 @@ class Client(_ClientFactoryMixin):
Needs to be set by subclasses.
"""

def __init__(self, credentials=None, http=None):
def __init__(self, credentials=None, _http=None):
if (credentials is not None and
not isinstance(
credentials, google.auth.credentials.Credentials)):
raise ValueError(_GOOGLE_AUTH_CREDENTIALS_HELP)
if credentials is None and http is None:
if credentials is None and _http is None:
credentials = get_credentials()
self._credentials = google.auth.credentials.with_scopes_if_required(
credentials, self.SCOPE)
self._http_internal = http
self._http_internal = _http

@property
def _http(self):
Expand Down Expand Up @@ -179,21 +181,23 @@ class ClientWithProject(Client, _ClientProjectMixin):

:type credentials: :class:`~google.auth.credentials.Credentials`
:param credentials: (Optional) The OAuth2 Credentials to use for this
client. If not passed (and if no ``http`` object is
client. If not passed (and if no ``_http`` object is
passed), falls back to the default inferred from the
environment.

:type http: :class:`~httplib2.Http`
:param http: (Optional) HTTP object to make requests. Can be any object
that defines ``request()`` with the same interface as
:meth:`~httplib2.Http.request`. If not passed, an
``http`` object is created that is bound to the
``credentials`` for the current object.
:type _http: :class:`~httplib2.Http`
:param _http: (Optional) HTTP object to make requests. Can be any object
that defines ``request()`` with the same interface as
:meth:`~httplib2.Http.request`. If not passed, an
``_http`` object is created that is bound to the
``credentials`` for the current object.
This parameter should be considered private, and could
change in the future.

:raises: :class:`ValueError` if the project is neither passed in nor
set in the environment.
"""

def __init__(self, project=None, credentials=None, http=None):
def __init__(self, project=None, credentials=None, _http=None):
_ClientProjectMixin.__init__(self, project=project)
Client.__init__(self, credentials=credentials, http=http)
Client.__init__(self, credentials=credentials, _http=_http)
8 changes: 4 additions & 4 deletions core/tests/unit/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def mock_get_credentials():
def test_ctor_explicit(self):
CREDENTIALS = _make_credentials()
HTTP = object()
client_obj = self._make_one(credentials=CREDENTIALS, http=HTTP)
client_obj = self._make_one(credentials=CREDENTIALS, _http=HTTP)

self.assertIs(client_obj._credentials, CREDENTIALS)
self.assertIs(client_obj._http_internal, HTTP)
Expand Down Expand Up @@ -106,7 +106,7 @@ def test_from_service_account_json_bad_args(self):
def test__http_property_existing(self):
credentials = _make_credentials()
http = object()
client = self._make_one(credentials=credentials, http=http)
client = self._make_one(credentials=credentials, _http=http)
self.assertIs(client._http_internal, http)
self.assertIs(client._http, http)

Expand Down Expand Up @@ -186,7 +186,7 @@ def test_ctor_w_invalid_project(self):
HTTP = object()
with self.assertRaises(ValueError):
self._make_one(project=object(), credentials=CREDENTIALS,
http=HTTP)
_http=HTTP)

def _explicit_ctor_helper(self, project):
import six
Expand All @@ -195,7 +195,7 @@ def _explicit_ctor_helper(self, project):
HTTP = object()

client_obj = self._make_one(project=project, credentials=CREDENTIALS,
http=HTTP)
_http=HTTP)

if isinstance(project, six.binary_type):
self.assertEqual(client_obj.project, project.decode('utf-8'))
Expand Down
44 changes: 24 additions & 20 deletions datastore/google/cloud/datastore/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
_DATASTORE_BASE_URL = 'https://datastore.googleapis.com'
"""Datastore API request URL base."""

_USE_GAX = _HAVE_GRPC and not os.getenv(DISABLE_GRPC, False)
_USE_GRPC = _HAVE_GRPC and not os.getenv(DISABLE_GRPC, False)


def _get_gcd_project():
Expand Down Expand Up @@ -173,38 +173,42 @@ class Client(ClientWithProject):

:type credentials: :class:`~google.auth.credentials.Credentials`
:param credentials: (Optional) The OAuth2 Credentials to use for this
client. If not passed (and if no ``http`` object is
client. If not passed (and if no ``_http`` object is
passed), falls back to the default inferred from the
environment.

:type http: :class:`~httplib2.Http`
:param http: (Optional) HTTP object to make requests. Can be any object
that defines ``request()`` with the same interface as
:meth:`~httplib2.Http.request`. If not passed, an
``http`` object is created that is bound to the
``credentials`` for the current object.

:type use_gax: bool
:param use_gax: (Optional) Explicitly specifies whether
to use the gRPC transport (via GAX) or HTTP. If unset,
falls back to the ``GOOGLE_CLOUD_DISABLE_GRPC`` environment
variable.
:type _http: :class:`~httplib2.Http`
:param _http: (Optional) HTTP object to make requests. Can be any object
that defines ``request()`` with the same interface as
:meth:`~httplib2.Http.request`. If not passed, an
``_http`` object is created that is bound to the
``credentials`` for the current object.
This parameter should be considered private, and could
change in the future.

:type _use_grpc: bool
:param _use_grpc: (Optional) Explicitly specifies whether
to use the gRPC transport (via GAX) or HTTP. If unset,
falls back to the ``GOOGLE_CLOUD_DISABLE_GRPC``
environment variable.
This parameter should be considered private, and could
change in the future.
"""

SCOPE = ('https://www.googleapis.com/auth/datastore',)
"""The scopes required for authenticating as a Cloud Datastore consumer."""

def __init__(self, project=None, namespace=None,
credentials=None, http=None, use_gax=None):
credentials=None, _http=None, _use_grpc=None):
super(Client, self).__init__(
project=project, credentials=credentials, http=http)
project=project, credentials=credentials, _http=_http)
self.namespace = namespace
self._batch_stack = _LocalStack()
self._datastore_api_internal = None
if use_gax is None:
self._use_gax = _USE_GAX
if _use_grpc is None:
self._use_grpc = _USE_GRPC
else:
self._use_gax = use_gax
self._use_grpc = _use_grpc
try:
host = os.environ[GCD_HOST]
self._base_url = 'http://' + host
Expand All @@ -220,7 +224,7 @@ def _determine_default(project):
def _datastore_api(self):
"""Getter for a wrapped API object."""
if self._datastore_api_internal is None:
if self._use_gax:
if self._use_grpc:
self._datastore_api_internal = make_datastore_api(self)
else:
self._datastore_api_internal = HTTPDatastoreAPI(self)
Expand Down
4 changes: 2 additions & 2 deletions datastore/tests/system/test_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def clone_client(client):
return datastore.Client(project=client.project,
namespace=client.namespace,
credentials=client._credentials,
http=client._http)
_http=client._http)


def setUpModule():
Expand All @@ -61,7 +61,7 @@ def setUpModule():
Config.CLIENT = datastore.Client(project=emulator_dataset,
namespace=test_namespace,
credentials=credentials,
http=http)
_http=http)


def tearDownModule():
Expand Down
Loading