-
Notifications
You must be signed in to change notification settings - Fork 138
refactor: consolidate BigQuery client creation and set user-agent #100
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
8e030c6
refactor: consolidate BigQuery client creation and set user-agent
tswast 752c063
Merge branch 'master' into issue98-user-agent
tswast ccc551f
Merge branch 'master' into issue98-user-agent
tswast c35dc95
add tests for default project logic
tswast fdb4e22
use google-auth >=1.2.0 for AnonymousCredentials support
tswast ee919f3
bump minimum google-cloud-bigquery version
tswast File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# Copyright 2021 The PyBigQuery Authors | ||
# | ||
# Use of this source code is governed by an MIT-style | ||
# license that can be found in the LICENSE file or at | ||
# https://opensource.org/licenses/MIT. | ||
|
||
from google.api_core import client_info | ||
import google.auth | ||
from google.cloud import bigquery | ||
from google.oauth2 import service_account | ||
import sqlalchemy | ||
|
||
|
||
USER_AGENT_TEMPLATE = "sqlalchemy/{}" | ||
SCOPES = ( | ||
"https://www.googleapis.com/auth/bigquery", | ||
"https://www.googleapis.com/auth/cloud-platform", | ||
"https://www.googleapis.com/auth/drive", | ||
) | ||
|
||
|
||
def google_client_info(): | ||
user_agent = USER_AGENT_TEMPLATE.format(sqlalchemy.__version__) | ||
return client_info.ClientInfo(user_agent=user_agent) | ||
|
||
|
||
def create_bigquery_client( | ||
credentials_info=None, | ||
credentials_path=None, | ||
default_query_job_config=None, | ||
location=None, | ||
project_id=None, | ||
): | ||
default_project = None | ||
|
||
if credentials_path: | ||
credentials = service_account.Credentials.from_service_account_file( | ||
credentials_path | ||
) | ||
credentials = credentials.with_scopes(SCOPES) | ||
default_project = credentials.project | ||
elif credentials_info: | ||
credentials = service_account.Credentials.from_service_account_info( | ||
credentials_info | ||
) | ||
credentials = credentials.with_scopes(SCOPES) | ||
default_project = credentials.project | ||
else: | ||
credentials, default_project = google.auth.default(scopes=SCOPES) | ||
|
||
if project_id is None: | ||
project_id = default_project | ||
|
||
return bigquery.Client( | ||
client_info=google_client_info(), | ||
project=project_id, | ||
credentials=credentials, | ||
location=location, | ||
default_query_job_config=default_query_job_config, | ||
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
# Copyright 2021 The PyBigQuery Authors | ||
# | ||
# Use of this source code is governed by an MIT-style | ||
# license that can be found in the LICENSE file or at | ||
# https://opensource.org/licenses/MIT. | ||
|
||
from unittest import mock | ||
|
||
import google.auth | ||
import google.auth.credentials | ||
from google.oauth2 import service_account | ||
import pytest | ||
|
||
|
||
class AnonymousCredentialsWithProject(google.auth.credentials.AnonymousCredentials): | ||
"""Fake credentials to trick isinstance""" | ||
|
||
def __init__(self, project): | ||
super().__init__() | ||
self.project = project | ||
|
||
def with_scopes(self, scopes): | ||
return self | ||
|
||
|
||
@pytest.fixture(scope="session") | ||
def module_under_test(): | ||
from pybigquery import _helpers | ||
|
||
return _helpers | ||
|
||
|
||
def test_create_bigquery_client_with_credentials_path(monkeypatch, module_under_test): | ||
mock_service_account = mock.create_autospec(service_account.Credentials) | ||
mock_service_account.from_service_account_file.return_value = AnonymousCredentialsWithProject( | ||
"service-account-project" | ||
) | ||
monkeypatch.setattr(service_account, "Credentials", mock_service_account) | ||
|
||
bqclient = module_under_test.create_bigquery_client( | ||
credentials_path="path/to/key.json", | ||
) | ||
|
||
assert bqclient.project == "service-account-project" | ||
|
||
|
||
def test_create_bigquery_client_with_credentials_path_respects_project( | ||
monkeypatch, module_under_test | ||
): | ||
"""Test that project_id is used, even when there is a default project. | ||
|
||
https://github.com/googleapis/python-bigquery-sqlalchemy/issues/48 | ||
""" | ||
mock_service_account = mock.create_autospec(service_account.Credentials) | ||
mock_service_account.from_service_account_file.return_value = AnonymousCredentialsWithProject( | ||
"service-account-project" | ||
) | ||
monkeypatch.setattr(service_account, "Credentials", mock_service_account) | ||
|
||
bqclient = module_under_test.create_bigquery_client( | ||
credentials_path="path/to/key.json", project_id="connection-url-project", | ||
) | ||
|
||
assert bqclient.project == "connection-url-project" | ||
|
||
|
||
def test_create_bigquery_client_with_credentials_info(monkeypatch, module_under_test): | ||
mock_service_account = mock.create_autospec(service_account.Credentials) | ||
mock_service_account.from_service_account_info.return_value = AnonymousCredentialsWithProject( | ||
"service-account-project" | ||
) | ||
monkeypatch.setattr(service_account, "Credentials", mock_service_account) | ||
|
||
bqclient = module_under_test.create_bigquery_client( | ||
credentials_info={ | ||
"type": "service_account", | ||
"project_id": "service-account-project", | ||
}, | ||
) | ||
|
||
assert bqclient.project == "service-account-project" | ||
|
||
|
||
def test_create_bigquery_client_with_credentials_info_respects_project( | ||
monkeypatch, module_under_test | ||
): | ||
"""Test that project_id is used, even when there is a default project. | ||
|
||
https://github.com/googleapis/python-bigquery-sqlalchemy/issues/48 | ||
""" | ||
mock_service_account = mock.create_autospec(service_account.Credentials) | ||
mock_service_account.from_service_account_info.return_value = AnonymousCredentialsWithProject( | ||
"service-account-project" | ||
) | ||
monkeypatch.setattr(service_account, "Credentials", mock_service_account) | ||
|
||
bqclient = module_under_test.create_bigquery_client( | ||
credentials_info={ | ||
"type": "service_account", | ||
"project_id": "service-account-project", | ||
}, | ||
project_id="connection-url-project", | ||
) | ||
|
||
assert bqclient.project == "connection-url-project" | ||
|
||
|
||
def test_create_bigquery_client_with_default_credentials( | ||
monkeypatch, module_under_test | ||
): | ||
def mock_default_credentials(*args, **kwargs): | ||
return (google.auth.credentials.AnonymousCredentials(), "default-project") | ||
|
||
monkeypatch.setattr(google.auth, "default", mock_default_credentials) | ||
|
||
bqclient = module_under_test.create_bigquery_client() | ||
|
||
assert bqclient.project == "default-project" | ||
|
||
|
||
def test_create_bigquery_client_with_default_credentials_respects_project( | ||
monkeypatch, module_under_test | ||
): | ||
"""Test that project_id is used, even when there is a default project. | ||
|
||
https://github.com/googleapis/python-bigquery-sqlalchemy/issues/48 | ||
""" | ||
|
||
def mock_default_credentials(*args, **kwargs): | ||
return (google.auth.credentials.AnonymousCredentials(), "default-project") | ||
|
||
monkeypatch.setattr(google.auth, "default", mock_default_credentials) | ||
|
||
bqclient = module_under_test.create_bigquery_client( | ||
project_id="connection-url-project", | ||
) | ||
|
||
assert bqclient.project == "connection-url-project" |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Need to add hook to check if
client_info
is available before trying to add it.https://github.com/pydata/pandas-gbq/blob/853f7922a0e0c853ed4d295ff14c41bfedf7e8d1/pandas_gbq/gbq.py#L364-L369
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, 1.12.0 was released 2019-05-16. That's close enough to the NEP 29 recommended 2 years, that I think it's worth just bumping the minimum version of
google-cloud-bigquery
in this case.