Skip to content

Commit d4c7e3d

Browse files
committed
Issue #13577 - Feedback incorporation
1 parent 7f1fd26 commit d4c7e3d

File tree

2 files changed

+34
-44
lines changed

2 files changed

+34
-44
lines changed

pandas/io/gbq.py

+28-28
Original file line numberDiff line numberDiff line change
@@ -160,33 +160,30 @@ def get_credentials(self):
160160
return self.get_service_account_credentials()
161161
else:
162162
# Try to retrieve Application Default Credentials
163-
project_id = self.project_id
164-
credentials = self.get_application_default_credentials(project_id)
163+
credentials = self.get_application_default_credentials()
165164
if not credentials:
166165
credentials = self.get_user_account_credentials()
167166
return credentials
168167

169-
@staticmethod
170-
def get_application_default_credentials(project_id): # pragma: no cover
168+
def get_application_default_credentials(self):
171169
"""
172-
Given a project_id tries to retrieve the
173-
"default application credentials"
170+
This method tries to retrieve the "default application credentials"
174171
Could be useful for running code on Google Cloud Platform
175172
"""
176-
credentials = None
173+
from oauth2client.client import AccessTokenRefreshError
174+
try:
175+
from googleapiclient.discovery import build
176+
from googleapiclient.errors import HttpError
177+
except:
178+
from apiclient.discovery import build
179+
from apiclient.errors import HttpError
177180
try:
178181
from oauth2client.client import GoogleCredentials
179-
from oauth2client.client import AccessTokenRefreshError
180182
from oauth2client.client import ApplicationDefaultCredentialsError
181-
try:
182-
from googleapiclient.discovery import build
183-
from googleapiclient.errors import HttpError
184-
except:
185-
from apiclient.discovery import build
186-
from apiclient.errors import HttpError
187183
except ImportError:
188184
return None
189185

186+
credentials = None
190187
try:
191188
credentials = GoogleCredentials.get_application_default()
192189
except ApplicationDefaultCredentialsError:
@@ -196,7 +193,7 @@ def get_application_default_credentials(project_id): # pragma: no cover
196193
jobs = bigquery_service.jobs()
197194
job_data = {'configuration': {'query': {'query': 'SELECT 1'}}}
198195
try:
199-
jobs.insert(projectId=project_id, body=job_data).execute()
196+
jobs.insert(projectId=self.project_id, body=job_data).execute()
200197
except (AccessTokenRefreshError, HttpError, TypeError):
201198
return None
202199
return credentials
@@ -616,12 +613,14 @@ def read_gbq(query, project_id=None, index_col=None, col_order=None,
616613
https://developers.google.com/api-client-library/python/apis/bigquery/v2
617614
618615
Authentication to the Google BigQuery service is via OAuth 2.0.
619-
By default "application default credentials" are used.
620-
If default application credentials are not found or are restrictive -
621-
User account credentials are used. You will be asked to
622-
grant permissions for product name 'pandas GBQ'. It is also posible
623-
to authenticate via service account credentials by using
624-
private_key parameter.
616+
If "private_key" is not provided:
617+
By default "application default credentials" are used [new behavior]
618+
If default application credentials are not found or are restrictive -
619+
User account credentials are used. In this case - you will be asked to
620+
grant permissions for product name 'pandas GBQ'.
621+
If "private_key" is provided:
622+
It is also posible to authenticate via service account credentials
623+
by using this parameter.
625624
626625
Parameters
627626
----------
@@ -713,13 +712,14 @@ def to_gbq(dataframe, destination_table, project_id, chunksize=10000,
713712
Documentation is available at
714713
https://developers.google.com/api-client-library/python/apis/bigquery/v2
715714
716-
Authentication to the Google BigQuery service is via OAuth 2.0.
717-
By default "application default credentials" are used.
718-
If default application credentials are not found or are restrictive -
719-
User account credentials are used. You will be asked to
720-
grant permissions for product name 'pandas GBQ'. It is also posible
721-
to authenticate via service account credentials by using
722-
private_key parameter.
715+
If "private_key" is not provided:
716+
By default "application default credentials" are used [new behavior]
717+
If default application credentials are not found or are restrictive -
718+
User account credentials are used. In this case - you will be asked to
719+
grant permissions for product name 'pandas GBQ'.
720+
If "private_key" is provided:
721+
It is also posible to authenticate via service account credentials
722+
by using this parameter.
723723
724724
Parameters
725725
----------

pandas/io/tests/test_gbq.py

+6-16
Original file line numberDiff line numberDiff line change
@@ -189,22 +189,6 @@ def test_generate_bq_schema_deprecated():
189189
gbq.generate_bq_schema(df)
190190

191191

192-
def google_credentials_import():
193-
try:
194-
from oauth2client.client import GoogleCredentials
195-
return GoogleCredentials
196-
except ImportError:
197-
return type(None)
198-
199-
200-
def test_should_be_able_to_get_credentials_from_default_credentials():
201-
GoogleCredentials = google_credentials_import()
202-
connector = gbq.GbqConnector
203-
credentials = connector.get_application_default_credentials(PROJECT_ID)
204-
valid_types = (type(None), GoogleCredentials)
205-
assert isinstance(credentials, valid_types)
206-
207-
208192
class TestGBQConnectorIntegration(tm.TestCase):
209193

210194
def setUp(self):
@@ -234,6 +218,12 @@ def test_should_be_able_to_get_results_from_query(self):
234218
schema, pages = self.sut.run_query('SELECT 1')
235219
self.assertTrue(pages is not None)
236220

221+
def test_get_application_default_credentials_should_not_throw_error(self):
222+
from oauth2client.client import GoogleCredentials
223+
credentials = self.sut.get_application_default_credentials()
224+
valid_types = (type(None), GoogleCredentials)
225+
assert isinstance(credentials, valid_types)
226+
237227

238228
class TestGBQConnectorServiceAccountKeyPathIntegration(tm.TestCase):
239229
def setUp(self):

0 commit comments

Comments
 (0)