From eea37697285410e5f3e012e5a76fbc697239a17a Mon Sep 17 00:00:00 2001 From: Danny Hermes Date: Wed, 22 Feb 2017 07:40:10 -0800 Subject: [PATCH] Adding GCCL header for HTTP APIs. (#3046) --- bigquery/google/cloud/bigquery/__init__.py | 3 ++ bigquery/google/cloud/bigquery/_http.py | 9 ++++++ bigquery/unit_tests/test__http.py | 32 +++++++++++++++++++ datastore/google/cloud/datastore/__init__.py | 3 ++ datastore/google/cloud/datastore/_http.py | 7 ++-- dns/google/cloud/dns/__init__.py | 3 ++ dns/google/cloud/dns/_http.py | 9 ++++++ dns/unit_tests/test__http.py | 32 +++++++++++++++++++ .../google/cloud/error_reporting/__init__.py | 3 ++ language/google/cloud/language/__init__.py | 3 ++ language/google/cloud/language/_http.py | 9 ++++++ language/unit_tests/test__http.py | 32 +++++++++++++++++++ logging/google/cloud/logging/__init__.py | 3 ++ logging/google/cloud/logging/_http.py | 9 ++++++ logging/unit_tests/test__http.py | 30 +++++++++++++++++ .../google/cloud/monitoring/__init__.py | 4 +++ monitoring/google/cloud/monitoring/_http.py | 9 ++++++ monitoring/unit_tests/test__http.py | 32 +++++++++++++++++++ pubsub/google/cloud/pubsub/__init__.py | 3 ++ pubsub/google/cloud/pubsub/_http.py | 8 +++++ pubsub/unit_tests/test__http.py | 30 +++++++++++++++++ .../google/cloud/resource_manager/__init__.py | 3 ++ .../google/cloud/resource_manager/_http.py | 9 ++++++ resource_manager/unit_tests/test__http.py | 32 +++++++++++++++++++ .../google/cloud/runtimeconfig/__init__.py | 4 +++ .../google/cloud/runtimeconfig/_http.py | 9 ++++++ runtimeconfig/unit_tests/test__http.py | 32 +++++++++++++++++++ speech/google/cloud/speech/__init__.py | 4 +++ speech/google/cloud/speech/_http.py | 8 +++++ speech/unit_tests/test__http.py | 32 +++++++++++++++++++ storage/google/cloud/storage/__init__.py | 4 +++ storage/google/cloud/storage/_http.py | 7 ++-- translate/google/cloud/translate/__init__.py | 4 +++ translate/google/cloud/translate/_http.py | 9 ++++++ translate/unit_tests/test__http.py | 32 +++++++++++++++++++ vision/google/cloud/vision/__init__.py | 4 +++ vision/google/cloud/vision/_http.py | 9 ++++++ vision/unit_tests/test__http.py | 30 +++++++++++++++++ 38 files changed, 497 insertions(+), 8 deletions(-) diff --git a/bigquery/google/cloud/bigquery/__init__.py b/bigquery/google/cloud/bigquery/__init__.py index 9b5809af6cf7..615f7cfa1b6b 100644 --- a/bigquery/google/cloud/bigquery/__init__.py +++ b/bigquery/google/cloud/bigquery/__init__.py @@ -23,6 +23,9 @@ """ +from pkg_resources import get_distribution +__version__ = get_distribution('google-cloud-bigquery').version + from google.cloud.bigquery._helpers import ArrayQueryParameter from google.cloud.bigquery._helpers import ScalarQueryParameter from google.cloud.bigquery._helpers import StructQueryParameter diff --git a/bigquery/google/cloud/bigquery/_http.py b/bigquery/google/cloud/bigquery/_http.py index 8e2c218c1cc9..75fac77e5eae 100644 --- a/bigquery/google/cloud/bigquery/_http.py +++ b/bigquery/google/cloud/bigquery/_http.py @@ -16,6 +16,11 @@ 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. @@ -32,3 +37,7 @@ 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, + } diff --git a/bigquery/unit_tests/test__http.py b/bigquery/unit_tests/test__http.py index 4fd8de8017fc..9972e9859313 100644 --- a/bigquery/unit_tests/test__http.py +++ b/bigquery/unit_tests/test__http.py @@ -14,6 +14,8 @@ import unittest +import mock + class TestConnection(unittest.TestCase): @@ -48,3 +50,33 @@ def test_build_api_url_w_extra_query_params(self): '/'.join(['', 'bigquery', conn.API_VERSION, 'foo'])) parms = dict(parse_qsl(qs)) self.assertEqual(parms['bar'], 'baz') + + def test_extra_headers(self): + from google.cloud import _http as base_http + from google.cloud.bigquery import _http as MUT + + http = mock.Mock(spec=['request']) + response = mock.Mock(status=200, spec=['status']) + data = b'brent-spiner' + http.request.return_value = response, data + client = mock.Mock(_http=http, spec=['_http']) + + conn = self._make_one(client) + req_data = 'req-data-boring' + result = conn.api_request( + 'GET', '/rainbow', data=req_data, expect_json=False) + self.assertEqual(result, data) + + expected_headers = { + 'Content-Length': str(len(req_data)), + 'Accept-Encoding': 'gzip', + base_http.CLIENT_INFO_HEADER: MUT._CLIENT_INFO, + 'User-Agent': conn.USER_AGENT, + } + expected_uri = conn.build_api_url('/rainbow') + http.request.assert_called_once_with( + body=req_data, + headers=expected_headers, + method='GET', + uri=expected_uri, + ) diff --git a/datastore/google/cloud/datastore/__init__.py b/datastore/google/cloud/datastore/__init__.py index 89e0eaaa31ea..e09b2101e9ca 100644 --- a/datastore/google/cloud/datastore/__init__.py +++ b/datastore/google/cloud/datastore/__init__.py @@ -54,6 +54,9 @@ """ +from pkg_resources import get_distribution +__version__ = get_distribution('google-cloud-datastore').version + from google.cloud.datastore.batch import Batch from google.cloud.datastore.client import Client from google.cloud.datastore.entity import Entity diff --git a/datastore/google/cloud/datastore/_http.py b/datastore/google/cloud/datastore/_http.py index edef8a9f30a4..a4c0a3f8c906 100644 --- a/datastore/google/cloud/datastore/_http.py +++ b/datastore/google/cloud/datastore/_http.py @@ -15,7 +15,6 @@ """Connections to Google Cloud Datastore API servers.""" import os -from pkg_resources import get_distribution from google.rpc import status_pb2 @@ -24,6 +23,8 @@ from google.cloud.environment_vars import GCD_HOST from google.cloud import exceptions from google.cloud.grpc.datastore.v1 import datastore_pb2 as _datastore_pb2 + +from google.cloud.datastore import __version__ try: from google.cloud.datastore._gax import _DatastoreAPIOverGRPC _HAVE_GRPC = True @@ -37,9 +38,7 @@ _DISABLE_GRPC = os.getenv(DISABLE_GRPC, False) _USE_GRPC = _HAVE_GRPC and not _DISABLE_GRPC -_DATASTORE_DIST = get_distribution('google-cloud-datastore') -_CLIENT_INFO = connection_module.CLIENT_INFO_TEMPLATE.format( - _DATASTORE_DIST.version) +_CLIENT_INFO = connection_module.CLIENT_INFO_TEMPLATE.format(__version__) class _DatastoreAPIOverHttp(object): diff --git a/dns/google/cloud/dns/__init__.py b/dns/google/cloud/dns/__init__.py index 5a8e5c388022..b836fa86b2a8 100644 --- a/dns/google/cloud/dns/__init__.py +++ b/dns/google/cloud/dns/__init__.py @@ -25,6 +25,9 @@ """ +from pkg_resources import get_distribution +__version__ = get_distribution('google-cloud-dns').version + from google.cloud.dns.zone import Changes from google.cloud.dns.client import Client from google.cloud.dns.zone import ManagedZone diff --git a/dns/google/cloud/dns/_http.py b/dns/google/cloud/dns/_http.py index cf0ef347a8d4..bf8b1e342642 100644 --- a/dns/google/cloud/dns/_http.py +++ b/dns/google/cloud/dns/_http.py @@ -16,6 +16,11 @@ from google.cloud import _http +from google.cloud.dns import __version__ + + +_CLIENT_INFO = _http.CLIENT_INFO_TEMPLATE.format(__version__) + class Connection(_http.JSONConnection): """A connection to Google Cloud DNS via the JSON REST API. @@ -32,3 +37,7 @@ class Connection(_http.JSONConnection): API_URL_TEMPLATE = '{api_base_url}/dns/{api_version}{path}' """A template for the URL of a particular API call.""" + + _EXTRA_HEADERS = { + _http.CLIENT_INFO_HEADER: _CLIENT_INFO, + } diff --git a/dns/unit_tests/test__http.py b/dns/unit_tests/test__http.py index ba9173808d75..98264e2abe30 100644 --- a/dns/unit_tests/test__http.py +++ b/dns/unit_tests/test__http.py @@ -14,6 +14,8 @@ import unittest +import mock + class TestConnection(unittest.TestCase): @@ -48,3 +50,33 @@ def test_build_api_url_w_extra_query_params(self): '/'.join(['', 'dns', conn.API_VERSION, 'foo'])) parms = dict(parse_qsl(qs)) self.assertEqual(parms['bar'], 'baz') + + def test_extra_headers(self): + from google.cloud import _http as base_http + from google.cloud.dns import _http as MUT + + http = mock.Mock(spec=['request']) + response = mock.Mock(status=200, spec=['status']) + data = b'brent-spiner' + http.request.return_value = response, data + client = mock.Mock(_http=http, spec=['_http']) + + conn = self._make_one(client) + req_data = 'req-data-boring' + result = conn.api_request( + 'GET', '/rainbow', data=req_data, expect_json=False) + self.assertEqual(result, data) + + expected_headers = { + 'Content-Length': str(len(req_data)), + 'Accept-Encoding': 'gzip', + base_http.CLIENT_INFO_HEADER: MUT._CLIENT_INFO, + 'User-Agent': conn.USER_AGENT, + } + expected_uri = conn.build_api_url('/rainbow') + http.request.assert_called_once_with( + body=req_data, + headers=expected_headers, + method='GET', + uri=expected_uri, + ) diff --git a/error_reporting/google/cloud/error_reporting/__init__.py b/error_reporting/google/cloud/error_reporting/__init__.py index e6cdf438adb5..bb76fb9b332a 100644 --- a/error_reporting/google/cloud/error_reporting/__init__.py +++ b/error_reporting/google/cloud/error_reporting/__init__.py @@ -15,6 +15,9 @@ """Client library for Stackdriver Error Reporting""" +from pkg_resources import get_distribution +__version__ = get_distribution('google-cloud-error-reporting').version + from google.cloud.error_reporting.client import Client from google.cloud.error_reporting.client import HTTPContext from google.cloud.error_reporting.util import build_flask_context diff --git a/language/google/cloud/language/__init__.py b/language/google/cloud/language/__init__.py index 93e9fd2fc67c..9519b930c333 100644 --- a/language/google/cloud/language/__init__.py +++ b/language/google/cloud/language/__init__.py @@ -15,6 +15,9 @@ """Client library for Google Cloud Natural Language API.""" +from pkg_resources import get_distribution +__version__ = get_distribution('google-cloud-language').version + from google.cloud.language.client import Client from google.cloud.language.document import Document from google.cloud.language.document import Encoding diff --git a/language/google/cloud/language/_http.py b/language/google/cloud/language/_http.py index f419c49bd941..af0ad516677f 100644 --- a/language/google/cloud/language/_http.py +++ b/language/google/cloud/language/_http.py @@ -16,6 +16,11 @@ from google.cloud import _http +from google.cloud.language import __version__ + + +_CLIENT_INFO = _http.CLIENT_INFO_TEMPLATE.format(__version__) + class Connection(_http.JSONConnection): """A connection to Google Cloud Natural Language JSON REST API. @@ -32,3 +37,7 @@ class Connection(_http.JSONConnection): API_URL_TEMPLATE = '{api_base_url}/{api_version}/documents:{path}' """A template for the URL of a particular API call.""" + + _EXTRA_HEADERS = { + _http.CLIENT_INFO_HEADER: _CLIENT_INFO, + } diff --git a/language/unit_tests/test__http.py b/language/unit_tests/test__http.py index 496422ee2c8b..6071c697fd01 100644 --- a/language/unit_tests/test__http.py +++ b/language/unit_tests/test__http.py @@ -14,6 +14,8 @@ import unittest +import mock + class TestConnection(unittest.TestCase): @@ -36,3 +38,33 @@ def test_build_api_url(self): method = 'annotateText' uri += ':' + method self.assertEqual(conn.build_api_url(method), uri) + + def test_extra_headers(self): + from google.cloud import _http as base_http + from google.cloud.language import _http as MUT + + http = mock.Mock(spec=['request']) + response = mock.Mock(status=200, spec=['status']) + data = b'brent-spiner' + http.request.return_value = response, data + client = mock.Mock(_http=http, spec=['_http']) + + conn = self._make_one(client) + req_data = 'req-data-boring' + result = conn.api_request( + 'GET', '/rainbow', data=req_data, expect_json=False) + self.assertEqual(result, data) + + expected_headers = { + 'Content-Length': str(len(req_data)), + 'Accept-Encoding': 'gzip', + base_http.CLIENT_INFO_HEADER: MUT._CLIENT_INFO, + 'User-Agent': conn.USER_AGENT, + } + expected_uri = conn.build_api_url('/rainbow') + http.request.assert_called_once_with( + body=req_data, + headers=expected_headers, + method='GET', + uri=expected_uri, + ) diff --git a/logging/google/cloud/logging/__init__.py b/logging/google/cloud/logging/__init__.py index d83ea4798949..17df46b08bad 100644 --- a/logging/google/cloud/logging/__init__.py +++ b/logging/google/cloud/logging/__init__.py @@ -15,6 +15,9 @@ """Google Stackdriver Logging API wrapper.""" +from pkg_resources import get_distribution +__version__ = get_distribution('google-cloud-logging').version + from google.cloud.logging.client import Client diff --git a/logging/google/cloud/logging/_http.py b/logging/google/cloud/logging/_http.py index e666daae2d3c..2764736b1e39 100644 --- a/logging/google/cloud/logging/_http.py +++ b/logging/google/cloud/logging/_http.py @@ -18,11 +18,16 @@ from google.cloud import _http from google.cloud.iterator import HTTPIterator + +from google.cloud.logging import __version__ from google.cloud.logging._helpers import entry_from_resource from google.cloud.logging.sink import Sink from google.cloud.logging.metric import Metric +_CLIENT_INFO = _http.CLIENT_INFO_TEMPLATE.format(__version__) + + class Connection(_http.JSONConnection): """A connection to Google Stackdriver Logging via the JSON REST API. @@ -39,6 +44,10 @@ class Connection(_http.JSONConnection): API_URL_TEMPLATE = '{api_base_url}/{api_version}{path}' """A template for the URL of a particular API call.""" + _EXTRA_HEADERS = { + _http.CLIENT_INFO_HEADER: _CLIENT_INFO, + } + class _LoggingAPI(object): """Helper mapping logging-related APIs. diff --git a/logging/unit_tests/test__http.py b/logging/unit_tests/test__http.py index b3536d2bf7b3..f0121f9dd319 100644 --- a/logging/unit_tests/test__http.py +++ b/logging/unit_tests/test__http.py @@ -42,6 +42,36 @@ def test_default_url(self): conn = self._make_one(client) self.assertIs(conn._client, client) + def test_extra_headers(self): + from google.cloud import _http as base_http + from google.cloud.logging import _http as MUT + + http = mock.Mock(spec=['request']) + response = mock.Mock(status=200, spec=['status']) + data = b'brent-spiner' + http.request.return_value = response, data + client = mock.Mock(_http=http, spec=['_http']) + + conn = self._make_one(client) + req_data = 'req-data-boring' + result = conn.api_request( + 'GET', '/rainbow', data=req_data, expect_json=False) + self.assertEqual(result, data) + + expected_headers = { + 'Content-Length': str(len(req_data)), + 'Accept-Encoding': 'gzip', + base_http.CLIENT_INFO_HEADER: MUT._CLIENT_INFO, + 'User-Agent': conn.USER_AGENT, + } + expected_uri = conn.build_api_url('/rainbow') + http.request.assert_called_once_with( + body=req_data, + headers=expected_headers, + method='GET', + uri=expected_uri, + ) + class Test_LoggingAPI(unittest.TestCase): diff --git a/monitoring/google/cloud/monitoring/__init__.py b/monitoring/google/cloud/monitoring/__init__.py index 3afbd72baac9..34bfbd4d6414 100644 --- a/monitoring/google/cloud/monitoring/__init__.py +++ b/monitoring/google/cloud/monitoring/__init__.py @@ -14,6 +14,10 @@ """Google Stackdriver Monitoring API wrapper.""" + +from pkg_resources import get_distribution +__version__ = get_distribution('google-cloud-monitoring').version + from google.cloud.monitoring.client import Client from google.cloud.monitoring.group import Group from google.cloud.monitoring.label import LabelDescriptor diff --git a/monitoring/google/cloud/monitoring/_http.py b/monitoring/google/cloud/monitoring/_http.py index 9d6b7c4a48e5..8e00e8b5bc42 100644 --- a/monitoring/google/cloud/monitoring/_http.py +++ b/monitoring/google/cloud/monitoring/_http.py @@ -16,6 +16,11 @@ from google.cloud import _http +from google.cloud.monitoring import __version__ + + +_CLIENT_INFO = _http.CLIENT_INFO_TEMPLATE.format(__version__) + class Connection(_http.JSONConnection): """A connection to Google Stackdriver Monitoring via the JSON REST API. @@ -32,3 +37,7 @@ class Connection(_http.JSONConnection): API_URL_TEMPLATE = '{api_base_url}/{api_version}{path}' """A template for the URL of a particular API call.""" + + _EXTRA_HEADERS = { + _http.CLIENT_INFO_HEADER: _CLIENT_INFO, + } diff --git a/monitoring/unit_tests/test__http.py b/monitoring/unit_tests/test__http.py index 6ec4923bbf78..d73bcc3498ff 100644 --- a/monitoring/unit_tests/test__http.py +++ b/monitoring/unit_tests/test__http.py @@ -14,6 +14,8 @@ import unittest +import mock + class TestConnection(unittest.TestCase): @@ -30,3 +32,33 @@ def test_constructor(self): client = object() connection = self._make_one(client) self.assertIs(connection._client, client) + + def test_extra_headers(self): + from google.cloud import _http as base_http + from google.cloud.monitoring import _http as MUT + + http = mock.Mock(spec=['request']) + response = mock.Mock(status=200, spec=['status']) + data = b'brent-spiner' + http.request.return_value = response, data + client = mock.Mock(_http=http, spec=['_http']) + + conn = self._make_one(client) + req_data = 'req-data-boring' + result = conn.api_request( + 'GET', '/rainbow', data=req_data, expect_json=False) + self.assertEqual(result, data) + + expected_headers = { + 'Content-Length': str(len(req_data)), + 'Accept-Encoding': 'gzip', + base_http.CLIENT_INFO_HEADER: MUT._CLIENT_INFO, + 'User-Agent': conn.USER_AGENT, + } + expected_uri = conn.build_api_url('/rainbow') + http.request.assert_called_once_with( + body=req_data, + headers=expected_headers, + method='GET', + uri=expected_uri, + ) diff --git a/pubsub/google/cloud/pubsub/__init__.py b/pubsub/google/cloud/pubsub/__init__.py index 9c0fcb9d45f6..6e6f29644727 100644 --- a/pubsub/google/cloud/pubsub/__init__.py +++ b/pubsub/google/cloud/pubsub/__init__.py @@ -24,6 +24,9 @@ """ +from pkg_resources import get_distribution +__version__ = get_distribution('google-cloud-pubsub').version + from google.cloud.pubsub.client import Client from google.cloud.pubsub.subscription import Subscription from google.cloud.pubsub.topic import Topic diff --git a/pubsub/google/cloud/pubsub/_http.py b/pubsub/google/cloud/pubsub/_http.py index bd438f72948d..e9538dce22d4 100644 --- a/pubsub/google/cloud/pubsub/_http.py +++ b/pubsub/google/cloud/pubsub/_http.py @@ -22,6 +22,8 @@ from google.cloud import _http from google.cloud.environment_vars import PUBSUB_EMULATOR from google.cloud.iterator import HTTPIterator + +from google.cloud.pubsub import __version__ from google.cloud.pubsub._helpers import subscription_name_from_path from google.cloud.pubsub.subscription import Subscription from google.cloud.pubsub.topic import Topic @@ -30,6 +32,8 @@ PUBSUB_API_HOST = 'pubsub.googleapis.com' """Pub / Sub API request host.""" +_CLIENT_INFO = _http.CLIENT_INFO_TEMPLATE.format(__version__) + class Connection(_http.JSONConnection): """A connection to Google Cloud Pub/Sub via the JSON REST API. @@ -47,6 +51,10 @@ class Connection(_http.JSONConnection): API_URL_TEMPLATE = '{api_base_url}/{api_version}{path}' """A template for the URL of a particular API call.""" + _EXTRA_HEADERS = { + _http.CLIENT_INFO_HEADER: _CLIENT_INFO, + } + def __init__(self, client): super(Connection, self).__init__(client) emulator_host = os.getenv(PUBSUB_EMULATOR) diff --git a/pubsub/unit_tests/test__http.py b/pubsub/unit_tests/test__http.py index 69130536608e..3f94b966dd66 100644 --- a/pubsub/unit_tests/test__http.py +++ b/pubsub/unit_tests/test__http.py @@ -98,6 +98,36 @@ def test_build_api_url_w_base_url_override(self): self.assertEqual(conn.build_api_url('/foo', api_base_url=base_url2), URI) + def test_extra_headers(self): + from google.cloud import _http as base_http + from google.cloud.pubsub import _http as MUT + + http = mock.Mock(spec=['request']) + response = mock.Mock(status=200, spec=['status']) + data = b'brent-spiner' + http.request.return_value = response, data + client = mock.Mock(_http=http, spec=['_http']) + + conn = self._make_one(client) + req_data = 'req-data-boring' + result = conn.api_request( + 'GET', '/rainbow', data=req_data, expect_json=False) + self.assertEqual(result, data) + + expected_headers = { + 'Content-Length': str(len(req_data)), + 'Accept-Encoding': 'gzip', + base_http.CLIENT_INFO_HEADER: MUT._CLIENT_INFO, + 'User-Agent': conn.USER_AGENT, + } + expected_uri = conn.build_api_url('/rainbow') + http.request.assert_called_once_with( + body=req_data, + headers=expected_headers, + method='GET', + uri=expected_uri, + ) + class Test_PublisherAPI(_Base): diff --git a/resource_manager/google/cloud/resource_manager/__init__.py b/resource_manager/google/cloud/resource_manager/__init__.py index 65a45126b52b..e69c0ba25cb2 100644 --- a/resource_manager/google/cloud/resource_manager/__init__.py +++ b/resource_manager/google/cloud/resource_manager/__init__.py @@ -15,6 +15,9 @@ """Google Cloud Resource Manager API wrapper.""" +from pkg_resources import get_distribution +__version__ = get_distribution('google-cloud-resource-manager').version + from google.cloud.resource_manager.client import Client from google.cloud.resource_manager.project import Project diff --git a/resource_manager/google/cloud/resource_manager/_http.py b/resource_manager/google/cloud/resource_manager/_http.py index 803be52d4f9b..af3d058ef921 100644 --- a/resource_manager/google/cloud/resource_manager/_http.py +++ b/resource_manager/google/cloud/resource_manager/_http.py @@ -17,6 +17,11 @@ from google.cloud import _http +from google.cloud.resource_manager import __version__ + + +_CLIENT_INFO = _http.CLIENT_INFO_TEMPLATE.format(__version__) + class Connection(_http.JSONConnection): """A connection to Google Cloud Resource Manager via the JSON REST API. @@ -33,3 +38,7 @@ class Connection(_http.JSONConnection): API_URL_TEMPLATE = '{api_base_url}/{api_version}{path}' """A template for the URL of a particular API call.""" + + _EXTRA_HEADERS = { + _http.CLIENT_INFO_HEADER: _CLIENT_INFO, + } diff --git a/resource_manager/unit_tests/test__http.py b/resource_manager/unit_tests/test__http.py index f9204492eeac..a5e0e4a77666 100644 --- a/resource_manager/unit_tests/test__http.py +++ b/resource_manager/unit_tests/test__http.py @@ -14,6 +14,8 @@ import unittest +import mock + class TestConnection(unittest.TestCase): @@ -47,3 +49,33 @@ def test_build_api_url_w_extra_query_params(self): '/'.join(['', conn.API_VERSION, 'foo'])) parms = dict(parse_qsl(qs)) self.assertEqual(parms['bar'], 'baz') + + def test_extra_headers(self): + from google.cloud import _http as base_http + from google.cloud.resource_manager import _http as MUT + + http = mock.Mock(spec=['request']) + response = mock.Mock(status=200, spec=['status']) + data = b'brent-spiner' + http.request.return_value = response, data + client = mock.Mock(_http=http, spec=['_http']) + + conn = self._make_one(client) + req_data = 'req-data-boring' + result = conn.api_request( + 'GET', '/rainbow', data=req_data, expect_json=False) + self.assertEqual(result, data) + + expected_headers = { + 'Content-Length': str(len(req_data)), + 'Accept-Encoding': 'gzip', + base_http.CLIENT_INFO_HEADER: MUT._CLIENT_INFO, + 'User-Agent': conn.USER_AGENT, + } + expected_uri = conn.build_api_url('/rainbow') + http.request.assert_called_once_with( + body=req_data, + headers=expected_headers, + method='GET', + uri=expected_uri, + ) diff --git a/runtimeconfig/google/cloud/runtimeconfig/__init__.py b/runtimeconfig/google/cloud/runtimeconfig/__init__.py index 1ab5f83d202c..73c8d2bc8283 100644 --- a/runtimeconfig/google/cloud/runtimeconfig/__init__.py +++ b/runtimeconfig/google/cloud/runtimeconfig/__init__.py @@ -14,4 +14,8 @@ """Google Cloud Runtime Configurator API package.""" + +from pkg_resources import get_distribution +__version__ = get_distribution('google-cloud-runtimeconfig').version + from google.cloud.runtimeconfig.client import Client diff --git a/runtimeconfig/google/cloud/runtimeconfig/_http.py b/runtimeconfig/google/cloud/runtimeconfig/_http.py index a3caf3ff7ed0..e70ebfd50241 100644 --- a/runtimeconfig/google/cloud/runtimeconfig/_http.py +++ b/runtimeconfig/google/cloud/runtimeconfig/_http.py @@ -18,6 +18,11 @@ from google.cloud import _http +from google.cloud.runtimeconfig import __version__ + + +_CLIENT_INFO = _http.CLIENT_INFO_TEMPLATE.format(__version__) + class Connection(_http.JSONConnection): """A connection to Google Cloud RuntimeConfig via the JSON REST API. @@ -34,3 +39,7 @@ class Connection(_http.JSONConnection): API_URL_TEMPLATE = '{api_base_url}/{api_version}{path}' """A template for the URL of a particular API call.""" + + _EXTRA_HEADERS = { + _http.CLIENT_INFO_HEADER: _CLIENT_INFO, + } diff --git a/runtimeconfig/unit_tests/test__http.py b/runtimeconfig/unit_tests/test__http.py index 995f609a5a5a..324994bd4ef6 100644 --- a/runtimeconfig/unit_tests/test__http.py +++ b/runtimeconfig/unit_tests/test__http.py @@ -14,6 +14,8 @@ import unittest +import mock + class TestConnection(unittest.TestCase): @@ -30,3 +32,33 @@ def test_default_url(self): client = object() conn = self._make_one(client) self.assertIs(conn._client, client) + + def test_extra_headers(self): + from google.cloud import _http as base_http + from google.cloud.runtimeconfig import _http as MUT + + http = mock.Mock(spec=['request']) + response = mock.Mock(status=200, spec=['status']) + data = b'brent-spiner' + http.request.return_value = response, data + client = mock.Mock(_http=http, spec=['_http']) + + conn = self._make_one(client) + req_data = 'req-data-boring' + result = conn.api_request( + 'GET', '/rainbow', data=req_data, expect_json=False) + self.assertEqual(result, data) + + expected_headers = { + 'Content-Length': str(len(req_data)), + 'Accept-Encoding': 'gzip', + base_http.CLIENT_INFO_HEADER: MUT._CLIENT_INFO, + 'User-Agent': conn.USER_AGENT, + } + expected_uri = conn.build_api_url('/rainbow') + http.request.assert_called_once_with( + body=req_data, + headers=expected_headers, + method='GET', + uri=expected_uri, + ) diff --git a/speech/google/cloud/speech/__init__.py b/speech/google/cloud/speech/__init__.py index 0c4f7542e02f..67b9aaf512fa 100644 --- a/speech/google/cloud/speech/__init__.py +++ b/speech/google/cloud/speech/__init__.py @@ -14,6 +14,10 @@ """Google Cloud Speech API wrapper.""" + +from pkg_resources import get_distribution +__version__ = get_distribution('google-cloud-speech').version + from google.cloud.speech.alternative import Alternative from google.cloud.speech.client import Client from google.cloud.speech.encoding import Encoding diff --git a/speech/google/cloud/speech/_http.py b/speech/google/cloud/speech/_http.py index a4755f6f89ff..3b82a25802ef 100644 --- a/speech/google/cloud/speech/_http.py +++ b/speech/google/cloud/speech/_http.py @@ -20,10 +20,14 @@ from google.cloud._helpers import _to_bytes from google.cloud import _http +from google.cloud.speech import __version__ from google.cloud.speech.result import Result from google.cloud.speech.operation import Operation +_CLIENT_INFO = _http.CLIENT_INFO_TEMPLATE.format(__version__) + + class Connection(_http.JSONConnection): """A connection to Google Cloud Speech JSON REST API. @@ -40,6 +44,10 @@ class Connection(_http.JSONConnection): API_URL_TEMPLATE = '{api_base_url}/{api_version}/{path}' """A template for the URL of a particular API call.""" + _EXTRA_HEADERS = { + _http.CLIENT_INFO_HEADER: _CLIENT_INFO, + } + class HTTPSpeechAPI(object): """Speech API for interacting with the HTTP version of the API. diff --git a/speech/unit_tests/test__http.py b/speech/unit_tests/test__http.py index fec987a07f37..a590a71ab6a5 100644 --- a/speech/unit_tests/test__http.py +++ b/speech/unit_tests/test__http.py @@ -14,6 +14,8 @@ import unittest +import mock + class TestConnection(unittest.TestCase): @@ -36,3 +38,33 @@ def test_build_api_url(self): ]) self.assertEqual(conn.build_api_url(method), uri) + + def test_extra_headers(self): + from google.cloud import _http as base_http + from google.cloud.speech import _http as MUT + + http = mock.Mock(spec=['request']) + response = mock.Mock(status=200, spec=['status']) + data = b'brent-spiner' + http.request.return_value = response, data + client = mock.Mock(_http=http, spec=['_http']) + + conn = self._make_one(client) + req_data = 'req-data-boring' + result = conn.api_request( + 'GET', '/rainbow', data=req_data, expect_json=False) + self.assertEqual(result, data) + + expected_headers = { + 'Content-Length': str(len(req_data)), + 'Accept-Encoding': 'gzip', + base_http.CLIENT_INFO_HEADER: MUT._CLIENT_INFO, + 'User-Agent': conn.USER_AGENT, + } + expected_uri = conn.build_api_url('/rainbow') + http.request.assert_called_once_with( + body=req_data, + headers=expected_headers, + method='GET', + uri=expected_uri, + ) diff --git a/storage/google/cloud/storage/__init__.py b/storage/google/cloud/storage/__init__.py index 10db1a1ce543..3303be5bdd7e 100644 --- a/storage/google/cloud/storage/__init__.py +++ b/storage/google/cloud/storage/__init__.py @@ -30,6 +30,10 @@ machine). """ + +from pkg_resources import get_distribution +__version__ = get_distribution('google-cloud-storage').version + from google.cloud.storage.batch import Batch from google.cloud.storage.blob import Blob from google.cloud.storage.bucket import Bucket diff --git a/storage/google/cloud/storage/_http.py b/storage/google/cloud/storage/_http.py index a1f5f9497dd7..b23096db2253 100644 --- a/storage/google/cloud/storage/_http.py +++ b/storage/google/cloud/storage/_http.py @@ -14,13 +14,12 @@ """Create / interact with Google Cloud Storage connections.""" -from pkg_resources import get_distribution - from google.cloud import _http +from google.cloud.storage import __version__ + -_STORAGE_DIST = get_distribution('google-cloud-storage') -_CLIENT_INFO = _http.CLIENT_INFO_TEMPLATE.format(_STORAGE_DIST.version) +_CLIENT_INFO = _http.CLIENT_INFO_TEMPLATE.format(__version__) class Connection(_http.JSONConnection): diff --git a/translate/google/cloud/translate/__init__.py b/translate/google/cloud/translate/__init__.py index efcb64eff8fe..c1cd5dd440f4 100644 --- a/translate/google/cloud/translate/__init__.py +++ b/translate/google/cloud/translate/__init__.py @@ -14,6 +14,10 @@ """Google Cloud Translation API wrapper.""" + +from pkg_resources import get_distribution +__version__ = get_distribution('google-cloud-translate').version + from google.cloud.translate.client import BASE from google.cloud.translate.client import Client from google.cloud.translate.client import NMT diff --git a/translate/google/cloud/translate/_http.py b/translate/google/cloud/translate/_http.py index 25be55217e32..0c404f2a4a3b 100644 --- a/translate/google/cloud/translate/_http.py +++ b/translate/google/cloud/translate/_http.py @@ -16,6 +16,11 @@ from google.cloud import _http +from google.cloud.translate import __version__ + + +_CLIENT_INFO = _http.CLIENT_INFO_TEMPLATE.format(__version__) + class Connection(_http.JSONConnection): """A connection to Google Cloud Translation API via the JSON REST API. @@ -32,3 +37,7 @@ class Connection(_http.JSONConnection): API_URL_TEMPLATE = '{api_base_url}/language/translate/{api_version}{path}' """A template for the URL of a particular API call.""" + + _EXTRA_HEADERS = { + _http.CLIENT_INFO_HEADER: _CLIENT_INFO, + } diff --git a/translate/unit_tests/test__http.py b/translate/unit_tests/test__http.py index 031fc2f0a3bb..1d7f7b4c6c18 100644 --- a/translate/unit_tests/test__http.py +++ b/translate/unit_tests/test__http.py @@ -14,6 +14,8 @@ import unittest +import mock + class TestConnection(unittest.TestCase): @@ -52,3 +54,33 @@ def test_build_api_url_w_extra_query_params(self): self.assertEqual(path, expected_path) params = parse_qsl(qs) self.assertEqual(params, query_params) + + def test_extra_headers(self): + from google.cloud import _http as base_http + from google.cloud.translate import _http as MUT + + http = mock.Mock(spec=['request']) + response = mock.Mock(status=200, spec=['status']) + data = b'brent-spiner' + http.request.return_value = response, data + client = mock.Mock(_http=http, spec=['_http']) + + conn = self._make_one(client) + req_data = 'req-data-boring' + result = conn.api_request( + 'GET', '/rainbow', data=req_data, expect_json=False) + self.assertEqual(result, data) + + expected_headers = { + 'Content-Length': str(len(req_data)), + 'Accept-Encoding': 'gzip', + base_http.CLIENT_INFO_HEADER: MUT._CLIENT_INFO, + 'User-Agent': conn.USER_AGENT, + } + expected_uri = conn.build_api_url('/rainbow') + http.request.assert_called_once_with( + body=req_data, + headers=expected_headers, + method='GET', + uri=expected_uri, + ) diff --git a/vision/google/cloud/vision/__init__.py b/vision/google/cloud/vision/__init__.py index 8a60ee566968..036cd9c08e53 100644 --- a/vision/google/cloud/vision/__init__.py +++ b/vision/google/cloud/vision/__init__.py @@ -14,4 +14,8 @@ """Google Cloud Vision API package.""" + +from pkg_resources import get_distribution +__version__ = get_distribution('google-cloud-vision').version + from google.cloud.vision.client import Client diff --git a/vision/google/cloud/vision/_http.py b/vision/google/cloud/vision/_http.py index e3a0627c80ac..cd024e7ba3dd 100644 --- a/vision/google/cloud/vision/_http.py +++ b/vision/google/cloud/vision/_http.py @@ -14,12 +14,17 @@ """HTTP Client for interacting with the Google Cloud Vision API.""" + from google.cloud import _http +from google.cloud.vision import __version__ from google.cloud.vision.annotations import Annotations from google.cloud.vision.feature import Feature +_CLIENT_INFO = _http.CLIENT_INFO_TEMPLATE.format(__version__) + + class Connection(_http.JSONConnection): """A connection to Google Cloud Vision via the JSON REST API. @@ -36,6 +41,10 @@ class Connection(_http.JSONConnection): API_URL_TEMPLATE = '{api_base_url}/{api_version}{path}' """A template for the URL of a particular API call.""" + _EXTRA_HEADERS = { + _http.CLIENT_INFO_HEADER: _CLIENT_INFO, + } + class _HTTPVisionAPI(object): """Vision API for interacting with the JSON/HTTP version of Vision diff --git a/vision/unit_tests/test__http.py b/vision/unit_tests/test__http.py index e1fd82e57b19..92b39021fe57 100644 --- a/vision/unit_tests/test__http.py +++ b/vision/unit_tests/test__http.py @@ -38,6 +38,36 @@ def test_default_url(self): conn = self._make_one(client) self.assertEqual(conn._client, client) + def test_extra_headers(self): + from google.cloud import _http as base_http + from google.cloud.vision import _http as MUT + + http = mock.Mock(spec=['request']) + response = mock.Mock(status=200, spec=['status']) + data = b'brent-spiner' + http.request.return_value = response, data + client = mock.Mock(_http=http, spec=['_http']) + + conn = self._make_one(client) + req_data = 'req-data-boring' + result = conn.api_request( + 'GET', '/rainbow', data=req_data, expect_json=False) + self.assertEqual(result, data) + + expected_headers = { + 'Content-Length': str(len(req_data)), + 'Accept-Encoding': 'gzip', + base_http.CLIENT_INFO_HEADER: MUT._CLIENT_INFO, + 'User-Agent': conn.USER_AGENT, + } + expected_uri = conn.build_api_url('/rainbow') + http.request.assert_called_once_with( + body=req_data, + headers=expected_headers, + method='GET', + uri=expected_uri, + ) + class Test_HTTPVisionAPI(unittest.TestCase): def _get_target_class(self):