Skip to content

feat: adds user agent parameters to two functions #1100

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 8 commits into from
Aug 5, 2024
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
52 changes: 42 additions & 10 deletions sqlalchemy_bigquery/_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import functools
import re
from typing import Optional

from google.api_core import client_info
import google.auth
Expand All @@ -24,19 +25,48 @@
)


def google_client_info():
user_agent = USER_AGENT_TEMPLATE.format(sqlalchemy.__version__)
def google_client_info(
user_agent: Optional[str] = None,
) -> google.api_core.client_info.ClientInfo:
"""
Return a client_info object, with an optional user agent
string. If user_agent is None, use a default value.
"""

if user_agent is None:
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,
credentials_base64=None,
default_query_job_config=None,
location=None,
project_id=None,
):
credentials_info: Optional[dict] = None,
credentials_path: Optional[str] = None,
credentials_base64: Optional[str] = None,
default_query_job_config: Optional[google.cloud.bigquery.job.QueryJobConfig] = None,
location: Optional[str] = None,
project_id: Optional[str] = None,
user_agent: Optional[google.api_core.client_info.ClientInfo] = None,
) -> google.cloud.bigquery.Client:
"""Construct a BigQuery client object.

Args:
credentials_info Optional[dict]:
credentials_path Optional[str]:
credentials_base64 Optional[str]:
default_query_job_config (Optional[google.cloud.bigquery.job.QueryJobConfig]):
Default ``QueryJobConfig``.
Will be merged into job configs passed into the ``query`` method.
location (Optional[str]):
Default location for jobs / datasets / tables.
project_id (Optional[str]):
Project ID for the project which the client acts on behalf of.
user_agent (Optional[google.api_core.client_info.ClientInfo]):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIT:

Looks like it should be string?

Suggested change
user_agent (Optional[google.api_core.client_info.ClientInfo]):
user_agent (Optional[str]):

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line is correct. The other line that had str was in error.

google_client_info returns a ClientInfo object which may include the user_agent string as well as several other attributes:

self.python_version = python_version
self.grpc_version = grpc_version
self.api_core_version = api_core_version
self.gapic_version = gapic_version
self.client_library_version = client_library_version
self.user_agent = user_agent
self.rest_version = rest_version

In addition, looking at the bigquery.Client object, it is expecting a ClientInfo object:

client_info (Optional[google.api_core.client_info.ClientInfo]):

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just looking at the code, user_agent is passed to google_client_info(), which is expecting a string. google.api_core.client_info.ClientInfo seems to be what google_client_info() returns instead?

The client info used to send a user-agent string along with API
requests. If ``None``, then default info will be used. Generally,
you only need to set this if you're developing your own library
or partner tool.
"""

default_project = None

if credentials_base64:
Expand All @@ -60,8 +90,10 @@ def create_bigquery_client(
if project_id is None:
project_id = default_project

client_info = google_client_info(user_agent=user_agent)

return bigquery.Client(
client_info=google_client_info(),
client_info=client_info,
project=project_id,
credentials=credentials,
location=location,
Expand Down
8 changes: 8 additions & 0 deletions tests/system/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,11 @@ def test_create_bigquery_client_with_credentials_base64_respects_project(
project_id="connection-url-project",
)
assert bqclient.project == "connection-url-project"


def test_create_bigquery_client_with_user_agent(module_under_test):
user_agent = "test_user_agent"

bqclient = module_under_test.create_bigquery_client(user_agent=user_agent)

assert bqclient._connection._client_info.user_agent == user_agent
13 changes: 13 additions & 0 deletions tests/unit/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import google.auth.credentials
import pytest
from google.oauth2 import service_account
from sqlalchemy_bigquery import _helpers


class AnonymousCredentialsWithProject(google.auth.credentials.AnonymousCredentials):
Expand Down Expand Up @@ -244,3 +245,15 @@ def foo_to_bar(self, m):
Replacer("hah").foo_to_bar("some foo and FOO is good")
== "some hah and FOO is good"
)


@pytest.mark.parametrize(
"user_agent, expected_user_agent",
[
(None, f"sqlalchemy/{_helpers.sqlalchemy.__version__}"),
("my-user-agent", "my-user-agent"),
],
)
def test_google_client_info(user_agent, expected_user_agent):
client_info = _helpers.google_client_info(user_agent=user_agent)
assert client_info.to_user_agent().startswith(expected_user_agent)