Skip to content
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

Core, BigQuery: refactor 'client_info' support. #7849

Merged
merged 14 commits into from
May 7, 2019
Merged
Show file tree
Hide file tree
Changes from 6 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
32 changes: 3 additions & 29 deletions bigquery/google/cloud/bigquery/_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,10 @@ class Connection(_http.JSONConnection):
"""

def __init__(self, client, client_info=None):
super(Connection, self).__init__(client)
super(Connection, self).__init__(client, client_info)

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 = {}
self._client_info.gapic_version = __version__
self._client_info.client_library_version = __version__

API_BASE_URL = "https://www.googleapis.com"
"""The base of the API call URL."""
Expand All @@ -48,22 +41,3 @@ def __init__(self, client, client_info=None):

API_URL_TEMPLATE = "{api_base_url}/bigquery/{api_version}{path}"
"""A template for the URL of a particular API call."""

@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

@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
42 changes: 7 additions & 35 deletions bigquery/tests/unit/test__http.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,49 +57,21 @@ def test_user_agent(self):
client = mock.Mock(_http=http, spec=["_http"])

conn = self._make_one(client)
conn.USER_AGENT = "my-application/1.2.3"
conn.user_agent = "my-application/1.2.3"
req_data = "req-data-boring"
result = conn.api_request("GET", "/rainbow", data=req_data, expect_json=False)
self.assertEqual(result, data)

expected_headers = {
"Accept-Encoding": "gzip",
base_http.CLIENT_INFO_HEADER: conn.USER_AGENT,
"User-Agent": conn.USER_AGENT,
}
expected_uri = conn.build_api_url("/rainbow")
http.request.assert_called_once_with(
data=req_data, headers=expected_headers, method="GET", url=expected_uri
)
self.assertIn("my-application/1.2.3", conn.USER_AGENT)

def test_extra_headers(self):
from google.cloud import _http as base_http

http = mock.create_autospec(requests.Session, instance=True)
response = requests.Response()
response.status_code = 200
data = b"brent-spiner"
response._content = data
http.request.return_value = response
client = mock.Mock(_http=http, spec=["_http"])

conn = self._make_one(client)
conn._EXTRA_HEADERS["x-test-header"] = "a test value"
req_data = "req-data-boring"
result = conn.api_request("GET", "/rainbow", data=req_data, expect_json=False)
self.assertEqual(result, data)

expected_headers = {
"Accept-Encoding": "gzip",
base_http.CLIENT_INFO_HEADER: conn.USER_AGENT,
"User-Agent": conn.USER_AGENT,
"x-test-header": "a test value",
base_http.CLIENT_INFO_HEADER: conn.user_agent,
"User-Agent": conn.user_agent,
}
expected_uri = conn.build_api_url("/rainbow")
http.request.assert_called_once_with(
data=req_data, headers=expected_headers, method="GET", url=expected_uri
)
self.assertIn("my-application/1.2.3", conn.user_agent)

def test_extra_headers_replace(self):
from google.cloud import _http as base_http
Expand All @@ -113,15 +85,15 @@ def test_extra_headers_replace(self):
client = mock.Mock(_http=http, spec=["_http"])

conn = self._make_one(client)
conn._EXTRA_HEADERS = {"x-test-header": "a test value"}
conn.extra_headers = {"x-test-header": "a test value"}
req_data = "req-data-boring"
result = conn.api_request("GET", "/rainbow", data=req_data, expect_json=False)
self.assertEqual(result, data)

expected_headers = {
"Accept-Encoding": "gzip",
base_http.CLIENT_INFO_HEADER: conn.USER_AGENT,
"User-Agent": conn.USER_AGENT,
base_http.CLIENT_INFO_HEADER: conn.user_agent,
"User-Agent": conn.user_agent,
"x-test-header": "a test value",
}
expected_uri = conn.build_api_url("/rainbow")
Expand Down
4 changes: 3 additions & 1 deletion bigquery/tests/unit/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -4518,7 +4518,9 @@ def test_list_rows_with_missing_schema(self):
self.assertIsNone(rows[2].age, msg=repr(table))

def test_list_rows_error(self):
client = self._make_one()
creds = _make_credentials()
http = object()
client = self._make_one(project=self.PROJECT, credentials=creds, _http=http)

# neither Table nor tableReference
with self.assertRaises(TypeError):
Expand Down
96 changes: 87 additions & 9 deletions core/google/cloud/_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@

import json
import platform
import warnings

from pkg_resources import get_distribution
from six.moves.urllib.parse import urlencode

from google.api_core.gapic_v1.client_info import ClientInfo
from google.cloud import exceptions


Expand All @@ -34,24 +36,100 @@
CLIENT_INFO_HEADER = "X-Goog-API-Client"
CLIENT_INFO_TEMPLATE = "gl-python/" + platform.python_version() + " gccl/{}"

_USER_AGENT_ALL_CAPS_DEPRECATED = """\
The 'USER_AGENT' class-level attribute is deprecated. Please use
'user_agent' instead.
"""

_EXTRA_HEADERS_ALL_CAPS_DEPRECATED = """\
The '_EXTRA_HEADERS' class-level attribute is deprecated. Please use
'extra_headers' instead.
"""


class Connection(object):
"""A generic connection to Google Cloud Platform.

:type client: :class:`~google.cloud.client.Client`
:param client: The client that owns the current connection.
"""

USER_AGENT = DEFAULT_USER_AGENT
_EXTRA_HEADERS = {}
"""Headers to be sent with every request.

Intended to be over-ridden by subclasses.
:type client_info: :class:`~google.api_core.client_info.ClientInfo`
:param client_info: (Optional) instance used to generate user agent.
"""

def __init__(self, client):
_user_agent = DEFAULT_USER_AGENT
_extra_headers = None

def __init__(self, client, client_info=None):
self._client = client

if client_info is None:
client_info = ClientInfo()

self._client_info = client_info

@property
def USER_AGENT(self):
"""Deprecated: get / set user agent sent by connection.

:rtype: str
:returns: user agent
"""
warnings.warn(
_USER_AGENT_ALL_CAPS_DEPRECATED, DeprecationWarning, stacklevel=2)
return self.user_agent

@USER_AGENT.setter
def USER_AGENT(self, value):
warnings.warn(
_USER_AGENT_ALL_CAPS_DEPRECATED, DeprecationWarning, stacklevel=2)
self.user_agent = value

@property
def user_agent(self):
"""Get / set user agent sent by connection.

:rtype: str
:returns: user agent
"""
return self._client_info.to_user_agent()

@user_agent.setter
def user_agent(self, value):
self._client_info.user_agent = value

@property
def _EXTRA_HEADERS(self):
"""Deprecated: get / set extra headers sent by connection.

:rtype: dict
:returns: header keys / values
"""
warnings.warn(
_EXTRA_HEADERS_ALL_CAPS_DEPRECATED, DeprecationWarning, stacklevel=2)
return self.extra_headers

@_EXTRA_HEADERS.setter
def _EXTRA_HEADERS(self, value):
warnings.warn(
_EXTRA_HEADERS_ALL_CAPS_DEPRECATED, DeprecationWarning, stacklevel=2)
self.extra_headers = value

@property
def extra_headers(self):
"""Get / set extra headers sent by connection.

:rtype: dict
:returns: header keys / values
"""
result = self._extra_headers.copy() if self._extra_headers else {}
result[CLIENT_INFO_HEADER] = self.user_agent
return result

@extra_headers.setter
def extra_headers(self, value):
self._extra_headers = value

@property
def credentials(self):
"""Getter for current credentials.
Expand Down Expand Up @@ -181,13 +259,13 @@ def _make_request(
:returns: The HTTP response.
"""
headers = headers or {}
headers.update(self._EXTRA_HEADERS)
headers.update(self.extra_headers)
headers["Accept-Encoding"] = "gzip"

if content_type:
headers["Content-Type"] = content_type

headers["User-Agent"] = self.USER_AGENT
headers["User-Agent"] = self.user_agent

return self._do_request(method, url, headers, data, target_object)

Expand Down
Loading