-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Add client_info
to BigQuery constructor for user-amenable user agent headers
#7806
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,21 +14,32 @@ | |
|
||
"""Create / interact with Google BigQuery connections.""" | ||
|
||
import google.api_core.gapic_v1.client_info | ||
from google.cloud import _http | ||
|
||
from google.cloud.bigquery import __version__ | ||
|
||
|
||
_CLIENT_INFO = _http.CLIENT_INFO_TEMPLATE.format(__version__) | ||
|
||
|
||
class Connection(_http.JSONConnection): | ||
"""A connection to Google BigQuery via the JSON REST API. | ||
|
||
:type client: :class:`~google.cloud.bigquery.client.Client` | ||
:param client: The client that owns the current connection. | ||
""" | ||
|
||
def __init__(self, client, client_info=None): | ||
super(Connection, self).__init__(client) | ||
|
||
if client_info is None: | ||
client_info = google.api_core.gapic_v1.client_info.ClientInfo( | ||
gapic_version=__version__, client_library_version=__version__ | ||
) | ||
else: | ||
client_info.gapic_version = __version__ | ||
client_info.client_library_version = __version__ | ||
self._client_info = client_info | ||
self._extra_headers = {} | ||
|
||
API_BASE_URL = "https://www.googleapis.com" | ||
"""The base of the API call URL.""" | ||
|
||
|
@@ -38,4 +49,21 @@ class Connection(_http.JSONConnection): | |
API_URL_TEMPLATE = "{api_base_url}/bigquery/{api_version}{path}" | ||
"""A template for the URL of a particular API call.""" | ||
|
||
_EXTRA_HEADERS = {_http.CLIENT_INFO_HEADER: _CLIENT_INFO} | ||
@property | ||
def USER_AGENT(self): | ||
return self._client_info.to_user_agent() | ||
|
||
@USER_AGENT.setter | ||
def USER_AGENT(self, value): | ||
self._client_info.user_agent = value | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These aren't constants, so why uppercase? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For backwards compatibility. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's a |
||
|
||
@property | ||
def _EXTRA_HEADERS(self): | ||
self._extra_headers[ | ||
_http.CLIENT_INFO_HEADER | ||
] = self._client_info.to_user_agent() | ||
return self._extra_headers | ||
|
||
@_EXTRA_HEADERS.setter | ||
def _EXTRA_HEADERS(self, value): | ||
self._extra_headers = value |
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.
@tswast Need to document the
client_info
parameter in the class docstring.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.
We should probably hoist this machinery into the
google.cloud._http.JSONConnection
base class.