Skip to content

Commit 5a2dd15

Browse files
committed
feedback changes
1 parent 17dd814 commit 5a2dd15

File tree

2 files changed

+42
-29
lines changed

2 files changed

+42
-29
lines changed

pandas/io/gbq.py

+29-21
Original file line numberDiff line numberDiff line change
@@ -171,25 +171,28 @@ def get_application_default_credentials(self):
171171
This method tries to retrieve the "default application credentials"
172172
Could be useful for running code on Google Cloud Platform
173173
"""
174-
from oauth2client.client import AccessTokenRefreshError
175-
try:
176-
from googleapiclient.discovery import build
177-
from googleapiclient.errors import HttpError
178-
except:
179-
from apiclient.discovery import build
180-
from apiclient.errors import HttpError
174+
oauth2client_library_imported = False
181175
try:
182176
from oauth2client.client import GoogleCredentials
177+
from oauth2client.client import AccessTokenRefreshError
183178
from oauth2client.client import HttpAccessTokenRefreshError
184179
from oauth2client.client import ApplicationDefaultCredentialsError
180+
oauth2client_library_imported = True
181+
from googleapiclient.discovery import build
182+
from googleapiclient.errors import HttpError
185183
except ImportError:
184+
from apiclient.discovery import build
185+
from apiclient.errors import HttpError
186+
187+
if not oauth2client_library_imported:
186188
return None
187189

188190
credentials = None
189191
try:
190192
credentials = GoogleCredentials.get_application_default()
191193
except ApplicationDefaultCredentialsError:
192194
return None
195+
193196
# Check if the application has rights to the BigQuery project
194197
bigquery_service = build('bigquery', 'v2', credentials=credentials)
195198
jobs = bigquery_service.jobs()
@@ -617,14 +620,16 @@ def read_gbq(query, project_id=None, index_col=None, col_order=None,
617620
https://developers.google.com/api-client-library/python/apis/bigquery/v2
618621
619622
Authentication to the Google BigQuery service is via OAuth 2.0.
620-
If "private_key" is not provided:
621-
By default "application default credentials" are used [new behavior]
622-
If default application credentials are not found or are restrictive -
623-
User account credentials are used. In this case - you will be asked to
623+
- If "private_key" is not provided:
624+
By default "application default credentials" are used.
625+
626+
.. versionadded:: 0.19.0
627+
628+
If default application credentials are not found or are restrictive,
629+
user account credentials are used. In this case, you will be asked to
624630
grant permissions for product name 'pandas GBQ'.
625-
If "private_key" is provided:
626-
It is also posible to authenticate via service account credentials
627-
by using this parameter.
631+
- If "private_key" is provided:
632+
Service account credentials will be used to authenticate.
628633
629634
Parameters
630635
----------
@@ -731,14 +736,17 @@ def to_gbq(dataframe, destination_table, project_id, chunksize=10000,
731736
Documentation is available at
732737
https://developers.google.com/api-client-library/python/apis/bigquery/v2
733738
734-
If "private_key" is not provided:
735-
By default "application default credentials" are used [new behavior]
736-
If default application credentials are not found or are restrictive -
737-
User account credentials are used. In this case - you will be asked to
739+
Authentication to the Google BigQuery service is via OAuth 2.0.
740+
- If "private_key" is not provided:
741+
By default "application default credentials" are used.
742+
743+
.. versionadded:: 0.19.0
744+
745+
If default application credentials are not found or are restrictive,
746+
user account credentials are used. In this case, you will be asked to
738747
grant permissions for product name 'pandas GBQ'.
739-
If "private_key" is provided:
740-
It is also posible to authenticate via service account credentials
741-
by using this parameter.
748+
- If "private_key" is provided:
749+
Service account credentials will be used to authenticate.
742750
743751
Parameters
744752
----------

pandas/io/tests/test_gbq.py

+13-8
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,7 @@ def _skip_if_no_private_key_contents():
5656
_skip_if_no_private_key_contents()
5757

5858

59-
def _skip_if_get_correct_default_credentials(can=False):
60-
got_credentials = False
59+
def _check_if_can_get_correct_default_credentials():
6160
try:
6261
from oauth2client.client import GoogleCredentials
6362
from apiclient.discovery import build
@@ -66,13 +65,19 @@ def _skip_if_get_correct_default_credentials(can=False):
6665
jobs = bigquery_service.jobs()
6766
job_data = {'configuration': {'query': {'query': 'SELECT 1'}}}
6867
jobs.insert(projectId=PROJECT_ID, body=job_data).execute()
69-
got_credentials = True
68+
return True
7069
except:
71-
pass
72-
if can and got_credentials:
70+
return False
71+
72+
73+
def _skip_if_cant_get_correct_default_credentials():
74+
if not _check_if_can_get_correct_default_credentials():
7375
raise nose.SkipTest("Cannot get default_credentials "
7476
"from the environment!")
75-
if (not can) and (not got_credentials):
77+
78+
79+
def _skip_if_can_get_correct_default_credentials():
80+
if _check_if_can_get_correct_default_credentials():
7681
raise nose.SkipTest("Can get default_credentials "
7782
"from the environment!")
7883

@@ -240,12 +245,12 @@ def test_should_be_able_to_get_results_from_query(self):
240245
self.assertTrue(pages is not None)
241246

242247
def test_get_application_default_credentials_does_not_throw_error(self):
243-
_skip_if_get_correct_default_credentials(can=True)
248+
_skip_if_can_get_correct_default_credentials()
244249
credentials = self.sut.get_application_default_credentials()
245250
self.assertIsNone(credentials)
246251

247252
def test_get_application_default_credentials_returns_credentials(self):
248-
_skip_if_get_correct_default_credentials(can=False)
253+
_skip_if_cant_get_correct_default_credentials()
249254
from oauth2client.client import GoogleCredentials
250255
credentials = self.sut.get_application_default_credentials()
251256
self.assertTrue(isinstance(credentials, GoogleCredentials))

0 commit comments

Comments
 (0)