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

Switch from oauth2client to google-auth #2726

Merged
merged 25 commits into from
Dec 2, 2016
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
43 changes: 15 additions & 28 deletions bigquery/unit_tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def _make_one(self, *args, **kw):
def test_ctor(self):
from google.cloud.bigquery._http import Connection
PROJECT = 'PROJECT'
creds = _Credentials()
creds = object()
http = object()
client = self._make_one(project=PROJECT, credentials=creds, http=http)
self.assertIsInstance(client._connection, Connection)
Expand Down Expand Up @@ -57,7 +57,7 @@ def test_list_projects_defaults(self):
'friendlyName': 'Two'},
]
}
creds = _Credentials()
creds = object()
client = self._make_one(PROJECT_1, creds)
conn = client._connection = _Connection(DATA)

Expand Down Expand Up @@ -86,7 +86,7 @@ def test_list_projects_explicit_response_missing_projects_key(self):
PATH = 'projects'
TOKEN = 'TOKEN'
DATA = {}
creds = _Credentials()
creds = object()
client = self._make_one(PROJECT, creds)
conn = client._connection = _Connection(DATA)

Expand Down Expand Up @@ -128,7 +128,7 @@ def test_list_datasets_defaults(self):
'friendlyName': 'Two'},
]
}
creds = _Credentials()
creds = object()
client = self._make_one(PROJECT, creds)
conn = client._connection = _Connection(DATA)

Expand Down Expand Up @@ -156,7 +156,7 @@ def test_list_datasets_explicit_response_missing_datasets_key(self):
PATH = 'projects/%s/datasets' % PROJECT
TOKEN = 'TOKEN'
DATA = {}
creds = _Credentials()
creds = object()
client = self._make_one(PROJECT, creds)
conn = client._connection = _Connection(DATA)

Expand All @@ -180,7 +180,7 @@ def test_dataset(self):
from google.cloud.bigquery.dataset import Dataset
PROJECT = 'PROJECT'
DATASET = 'dataset_name'
creds = _Credentials()
creds = object()
http = object()
client = self._make_one(project=PROJECT, credentials=creds, http=http)
dataset = client.dataset(DATASET)
Expand All @@ -190,7 +190,7 @@ def test_dataset(self):

def test_job_from_resource_unknown_type(self):
PROJECT = 'PROJECT'
creds = _Credentials()
creds = object()
client = self._make_one(PROJECT, creds)
with self.assertRaises(ValueError):
client.job_from_resource({'configuration': {'nonesuch': {}}})
Expand Down Expand Up @@ -304,7 +304,7 @@ def test_list_jobs_defaults(self):
LOAD_DATA,
]
}
creds = _Credentials()
creds = object()
client = self._make_one(PROJECT, creds)
conn = client._connection = _Connection(DATA)

Expand Down Expand Up @@ -360,7 +360,7 @@ def test_list_jobs_load_job_wo_sourceUris(self):
LOAD_DATA,
]
}
creds = _Credentials()
creds = object()
client = self._make_one(PROJECT, creds)
conn = client._connection = _Connection(DATA)

Expand Down Expand Up @@ -388,7 +388,7 @@ def test_list_jobs_explicit_missing(self):
PATH = 'projects/%s/jobs' % PROJECT
DATA = {}
TOKEN = 'TOKEN'
creds = _Credentials()
creds = object()
client = self._make_one(PROJECT, creds)
conn = client._connection = _Connection(DATA)

Expand Down Expand Up @@ -419,7 +419,7 @@ def test_load_table_from_storage(self):
DATASET = 'dataset_name'
DESTINATION = 'destination_table'
SOURCE_URI = 'http://example.com/source.csv'
creds = _Credentials()
creds = object()
http = object()
client = self._make_one(project=PROJECT, credentials=creds, http=http)
dataset = client.dataset(DATASET)
Expand All @@ -438,7 +438,7 @@ def test_copy_table(self):
DATASET = 'dataset_name'
SOURCE = 'source_table'
DESTINATION = 'destination_table'
creds = _Credentials()
creds = object()
http = object()
client = self._make_one(project=PROJECT, credentials=creds, http=http)
dataset = client.dataset(DATASET)
Expand All @@ -458,7 +458,7 @@ def test_extract_table_to_storage(self):
DATASET = 'dataset_name'
SOURCE = 'source_table'
DESTINATION = 'gs://bucket_name/object_name'
creds = _Credentials()
creds = object()
http = object()
client = self._make_one(project=PROJECT, credentials=creds, http=http)
dataset = client.dataset(DATASET)
Expand All @@ -475,7 +475,7 @@ def test_run_async_query(self):
PROJECT = 'PROJECT'
JOB = 'job_name'
QUERY = 'select count(*) from persons'
creds = _Credentials()
creds = object()
http = object()
client = self._make_one(project=PROJECT, credentials=creds, http=http)
job = client.run_async_query(JOB, QUERY)
Expand All @@ -488,7 +488,7 @@ def test_run_sync_query(self):
from google.cloud.bigquery.query import QueryResults
PROJECT = 'PROJECT'
QUERY = 'select count(*) from persons'
creds = _Credentials()
creds = object()
http = object()
client = self._make_one(project=PROJECT, credentials=creds, http=http)
job = client.run_sync_query(QUERY)
Expand All @@ -498,19 +498,6 @@ def test_run_sync_query(self):
self.assertEqual(job.query, QUERY)


class _Credentials(object):

_scopes = None

@staticmethod
def create_scoped_required():
return True

def create_scoped(self, scope):
self._scopes = scope
return self


class _Connection(object):

def __init__(self, *responses):
Expand Down
17 changes: 9 additions & 8 deletions bigtable/google/cloud/bigtable/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

import os

import google.auth.credentials
from google.longrunning import operations_grpc

from google.cloud._helpers import make_insecure_stub
Expand Down Expand Up @@ -201,14 +202,16 @@ def __init__(self, project=None, credentials=None,
else:
scopes.append(DATA_SCOPE)

self._read_only = bool(read_only)

if admin:
scopes.append(ADMIN_SCOPE)

self._admin = bool(admin)
try:
credentials = credentials.create_scoped(scopes)
except AttributeError:
pass

credentials = google.auth.credentials.with_scopes_if_required(
credentials, scopes)

self._credentials = credentials
self.user_agent = user_agent
self.emulator_host = os.getenv(BIGTABLE_EMULATOR)
Expand All @@ -229,12 +232,10 @@ def copy(self):
:rtype: :class:`.Client`
:returns: A copy of the current client.
"""
credentials = self._credentials
copied_creds = credentials.create_scoped(credentials.scopes)
return self.__class__(
self.project,
copied_creds,
READ_ONLY_SCOPE in copied_creds.scopes,
self._credentials,
self._read_only,
self._admin,
self.user_agent,
)
Expand Down
1 change: 1 addition & 0 deletions bigtable/tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ localdeps =
pip install --quiet --upgrade {toxinidir}/../core
deps =
{toxinidir}/../core
mock
pytest
covercmd =
py.test --quiet \
Expand Down
Loading