Skip to content

Commit f9324e3

Browse files
chalmerloweLinchin
andauthored
feat: adds user agent parameters to two functions (#1100)
* adds user agents parameters to two functions * adds typing import * adds test for google_client_info * tweaks test to only check starting characters * updates linting * updates type hints, linting * updates type hints, linting - redux * add system test --------- Co-authored-by: Lingqing Gan <lingqing.gan@gmail.com>
1 parent 80781ef commit f9324e3

File tree

3 files changed

+63
-10
lines changed

3 files changed

+63
-10
lines changed

sqlalchemy_bigquery/_helpers.py

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import functools
88
import re
9+
from typing import Optional
910

1011
from google.api_core import client_info
1112
import google.auth
@@ -24,19 +25,48 @@
2425
)
2526

2627

27-
def google_client_info():
28-
user_agent = USER_AGENT_TEMPLATE.format(sqlalchemy.__version__)
28+
def google_client_info(
29+
user_agent: Optional[str] = None,
30+
) -> google.api_core.client_info.ClientInfo:
31+
"""
32+
Return a client_info object, with an optional user agent
33+
string. If user_agent is None, use a default value.
34+
"""
35+
36+
if user_agent is None:
37+
user_agent = USER_AGENT_TEMPLATE.format(sqlalchemy.__version__)
2938
return client_info.ClientInfo(user_agent=user_agent)
3039

3140

3241
def create_bigquery_client(
33-
credentials_info=None,
34-
credentials_path=None,
35-
credentials_base64=None,
36-
default_query_job_config=None,
37-
location=None,
38-
project_id=None,
39-
):
42+
credentials_info: Optional[dict] = None,
43+
credentials_path: Optional[str] = None,
44+
credentials_base64: Optional[str] = None,
45+
default_query_job_config: Optional[google.cloud.bigquery.job.QueryJobConfig] = None,
46+
location: Optional[str] = None,
47+
project_id: Optional[str] = None,
48+
user_agent: Optional[google.api_core.client_info.ClientInfo] = None,
49+
) -> google.cloud.bigquery.Client:
50+
"""Construct a BigQuery client object.
51+
52+
Args:
53+
credentials_info Optional[dict]:
54+
credentials_path Optional[str]:
55+
credentials_base64 Optional[str]:
56+
default_query_job_config (Optional[google.cloud.bigquery.job.QueryJobConfig]):
57+
Default ``QueryJobConfig``.
58+
Will be merged into job configs passed into the ``query`` method.
59+
location (Optional[str]):
60+
Default location for jobs / datasets / tables.
61+
project_id (Optional[str]):
62+
Project ID for the project which the client acts on behalf of.
63+
user_agent (Optional[google.api_core.client_info.ClientInfo]):
64+
The client info used to send a user-agent string along with API
65+
requests. If ``None``, then default info will be used. Generally,
66+
you only need to set this if you're developing your own library
67+
or partner tool.
68+
"""
69+
4070
default_project = None
4171

4272
if credentials_base64:
@@ -60,8 +90,10 @@ def create_bigquery_client(
6090
if project_id is None:
6191
project_id = default_project
6292

93+
client_info = google_client_info(user_agent=user_agent)
94+
6395
return bigquery.Client(
64-
client_info=google_client_info(),
96+
client_info=client_info,
6597
project=project_id,
6698
credentials=credentials,
6799
location=location,

tests/system/test_helpers.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,3 +104,11 @@ def test_create_bigquery_client_with_credentials_base64_respects_project(
104104
project_id="connection-url-project",
105105
)
106106
assert bqclient.project == "connection-url-project"
107+
108+
109+
def test_create_bigquery_client_with_user_agent(module_under_test):
110+
user_agent = "test_user_agent"
111+
112+
bqclient = module_under_test.create_bigquery_client(user_agent=user_agent)
113+
114+
assert bqclient._connection._client_info.user_agent == user_agent

tests/unit/test_helpers.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import google.auth.credentials
1313
import pytest
1414
from google.oauth2 import service_account
15+
from sqlalchemy_bigquery import _helpers
1516

1617

1718
class AnonymousCredentialsWithProject(google.auth.credentials.AnonymousCredentials):
@@ -244,3 +245,15 @@ def foo_to_bar(self, m):
244245
Replacer("hah").foo_to_bar("some foo and FOO is good")
245246
== "some hah and FOO is good"
246247
)
248+
249+
250+
@pytest.mark.parametrize(
251+
"user_agent, expected_user_agent",
252+
[
253+
(None, f"sqlalchemy/{_helpers.sqlalchemy.__version__}"),
254+
("my-user-agent", "my-user-agent"),
255+
],
256+
)
257+
def test_google_client_info(user_agent, expected_user_agent):
258+
client_info = _helpers.google_client_info(user_agent=user_agent)
259+
assert client_info.to_user_agent().startswith(expected_user_agent)

0 commit comments

Comments
 (0)