Skip to content

Commit c43caee

Browse files
fix: include amended user agent in read client (#17592)
Towards internal issue b/526614511 🦕 --------- Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
1 parent 7bfa41a commit c43caee

7 files changed

Lines changed: 294 additions & 175 deletions

File tree

packages/google-cloud-bigquery/google/cloud/bigquery/client.py

Lines changed: 86 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,8 @@
1414

1515
"""Client for interacting with the Google BigQuery API."""
1616

17-
from __future__ import absolute_import
18-
from __future__ import annotations
19-
from __future__ import division
17+
from __future__ import absolute_import, annotations, division
2018

21-
from collections import abc as collections_abc
2219
import copy
2320
import datetime
2421
import functools
@@ -30,36 +27,39 @@
3027
import os
3128
import tempfile
3229
import typing
30+
import uuid
31+
import warnings
32+
from collections import abc as collections_abc
3333
from typing import (
34+
IO,
3435
Any,
3536
Callable,
3637
Dict,
37-
IO,
3838
Iterable,
39-
Mapping,
4039
List,
40+
Mapping,
4141
Optional,
4242
Sequence,
4343
Tuple,
4444
Union,
4545
)
46-
import uuid
47-
import warnings
48-
49-
import requests
50-
51-
from google import resumable_media # type: ignore
52-
from google.resumable_media.requests import MultipartUpload # type: ignore
53-
from google.resumable_media.requests import ResumableUpload
5446

5547
import google.api_core.client_options
5648
import google.api_core.exceptions as core_exceptions
57-
from google.api_core.iam import Policy
49+
import google.cloud._helpers # type: ignore
50+
import requests
51+
from google import resumable_media # type: ignore
5852
from google.api_core import page_iterator
5953
from google.api_core import retry as retries
60-
import google.cloud._helpers # type: ignore
54+
from google.api_core.iam import Policy
6155
from google.cloud import exceptions # pytype: disable=import-error
62-
from google.cloud.client import ClientWithProject # type: ignore # pytype: disable=import-error
56+
from google.cloud.client import (
57+
ClientWithProject, # type: ignore # pytype: disable=import-error
58+
)
59+
from google.resumable_media.requests import (
60+
MultipartUpload, # type: ignore
61+
ResumableUpload,
62+
)
6363

6464
try:
6565
from google.cloud.bigquery_storage_v1.services.big_query_read.client import (
@@ -70,29 +70,30 @@
7070

7171

7272
from google.auth.credentials import Credentials
73-
from google.cloud.bigquery._http import Connection
74-
from google.cloud.bigquery import _job_helpers
75-
from google.cloud.bigquery import _pandas_helpers
76-
from google.cloud.bigquery import _versions_helpers
77-
from google.cloud.bigquery import enums
73+
from google.cloud.bigquery import (
74+
_job_helpers,
75+
_pandas_helpers,
76+
_versions_helpers,
77+
enums,
78+
job,
79+
)
7880
from google.cloud.bigquery import exceptions as bq_exceptions
79-
from google.cloud.bigquery import job
80-
from google.cloud.bigquery._helpers import _get_sub_prop
81-
from google.cloud.bigquery._helpers import _record_field_to_json
82-
from google.cloud.bigquery._helpers import _str_or_none
83-
from google.cloud.bigquery._helpers import _verify_job_config_type
84-
from google.cloud.bigquery._helpers import _get_bigquery_host
85-
from google.cloud.bigquery._helpers import _DEFAULT_HOST
86-
from google.cloud.bigquery._helpers import _DEFAULT_HOST_TEMPLATE
87-
from google.cloud.bigquery._helpers import _DEFAULT_UNIVERSE
88-
from google.cloud.bigquery._helpers import _validate_universe
89-
from google.cloud.bigquery._helpers import _get_client_universe
90-
from google.cloud.bigquery._helpers import TimeoutType
81+
from google.cloud.bigquery._helpers import (
82+
_DEFAULT_HOST,
83+
_DEFAULT_HOST_TEMPLATE,
84+
_DEFAULT_UNIVERSE,
85+
TimeoutType,
86+
_get_bigquery_host,
87+
_get_client_universe,
88+
_get_sub_prop,
89+
_record_field_to_json,
90+
_str_or_none,
91+
_validate_universe,
92+
_verify_job_config_type,
93+
)
94+
from google.cloud.bigquery._http import Connection
9195
from google.cloud.bigquery._job_helpers import make_job_id as _make_job_id
92-
from google.cloud.bigquery.dataset import Dataset
93-
from google.cloud.bigquery.dataset import DatasetListItem
94-
from google.cloud.bigquery.dataset import DatasetReference
95-
96+
from google.cloud.bigquery.dataset import Dataset, DatasetListItem, DatasetReference
9697
from google.cloud.bigquery.enums import AutoRowIDs, DatasetView, UpdateMode
9798
from google.cloud.bigquery.format_options import ParquetOptions
9899
from google.cloud.bigquery.job import (
@@ -105,27 +106,26 @@
105106
QueryJob,
106107
QueryJobConfig,
107108
)
108-
from google.cloud.bigquery.model import Model
109-
from google.cloud.bigquery.model import ModelReference
110-
from google.cloud.bigquery.model import _model_arg_to_model_ref
109+
from google.cloud.bigquery.model import Model, ModelReference, _model_arg_to_model_ref
111110
from google.cloud.bigquery.opentelemetry_tracing import create_span
112111
from google.cloud.bigquery.query import _QueryResults
113112
from google.cloud.bigquery.retry import (
113+
DEFAULT_GET_JOB_TIMEOUT,
114114
DEFAULT_JOB_RETRY,
115115
DEFAULT_RETRY,
116116
DEFAULT_TIMEOUT,
117-
DEFAULT_GET_JOB_TIMEOUT,
118117
POLLING_DEFAULT_VALUE,
119118
)
120-
from google.cloud.bigquery.routine import Routine
121-
from google.cloud.bigquery.routine import RoutineReference
119+
from google.cloud.bigquery.routine import Routine, RoutineReference
122120
from google.cloud.bigquery.schema import SchemaField
123-
from google.cloud.bigquery.table import _table_arg_to_table
124-
from google.cloud.bigquery.table import _table_arg_to_table_ref
125-
from google.cloud.bigquery.table import Table
126-
from google.cloud.bigquery.table import TableListItem
127-
from google.cloud.bigquery.table import TableReference
128-
from google.cloud.bigquery.table import RowIterator
121+
from google.cloud.bigquery.table import (
122+
RowIterator,
123+
Table,
124+
TableListItem,
125+
TableReference,
126+
_table_arg_to_table,
127+
_table_arg_to_table_ref,
128+
)
129129

130130
pyarrow = _versions_helpers.PYARROW_VERSIONS.try_import()
131131
pandas = (
@@ -621,11 +621,45 @@ def _ensure_bqstorage_client(
621621
)
622622
return None
623623

624-
if bqstorage_client is None: # pragma: NO COVER
624+
# Import here since it should be installed if the BQ Storage client is
625+
# also installed.
626+
import google.api_core.gapic_v1.client_info
627+
628+
try:
629+
import pandas_gbq # type: ignore
630+
except ImportError:
631+
pandas_gbq = None # type: ignore
632+
633+
if pandas_gbq is None:
634+
user_agent = "pandas-gbq/0.0.0"
635+
else:
636+
user_agent = f"pandas-gbq/{pandas_gbq.__version__}"
637+
638+
if client_info is None:
639+
amended_client_info = google.api_core.gapic_v1.client_info.ClientInfo(
640+
user_agent=user_agent,
641+
)
642+
else:
643+
if client_info.user_agent is None:
644+
amended_user_agent = user_agent
645+
else:
646+
amended_user_agent = client_info.user_agent + " " + user_agent
647+
648+
amended_client_info = google.api_core.gapic_v1.client_info.ClientInfo(
649+
python_version=client_info.python_version,
650+
grpc_version=client_info.grpc_version,
651+
api_core_version=client_info.api_core_version,
652+
gapic_version=client_info.gapic_version,
653+
user_agent=amended_user_agent,
654+
rest_version=client_info.rest_version,
655+
protobuf_runtime_version=client_info.protobuf_runtime_version,
656+
)
657+
658+
if bqstorage_client is None:
625659
bqstorage_client = bigquery_storage.BigQueryReadClient(
626660
credentials=self._credentials,
627661
client_options=client_options,
628-
client_info=client_info, # type: ignore # (None is also accepted)
662+
client_info=amended_client_info,
629663
)
630664

631665
return bqstorage_client

packages/google-cloud-bigquery/noxfile.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@
1515
from __future__ import absolute_import
1616

1717
import contextlib
18-
from functools import wraps
1918
import os
2019
import pathlib
21-
from typing import Generator
2220
import re
2321
import shutil
2422
import time
23+
from functools import wraps
24+
from typing import Generator
2525

2626
import nox
2727

packages/google-cloud-bigquery/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ classifiers = [
4141
"Topic :: Internet",
4242
]
4343
dependencies = [
44-
"google-api-core[grpc] >= 2.11.1, < 3.0.0",
44+
"google-api-core[grpc] >= 2.25.0, < 3.0.0",
4545
"google-auth[pyopenssl] >= 2.14.1, < 3.0.0",
4646
"google-cloud-core >= 2.4.1, < 3.0.0",
4747
"google-resumable-media >= 2.0.0, < 3.0.0",

packages/google-cloud-bigquery/testing/constraints-3.10.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
google-api-core==2.11.1
1+
google-api-core==2.25.0
22
google-auth==2.14.1
33
google-cloud-core==2.4.1
44
google-resumable-media==2.0.0

0 commit comments

Comments
 (0)