diff --git a/packages/google-cloud-resource-manager/google/cloud/resource_manager/__init__.py b/packages/google-cloud-resource-manager/google/cloud/resource_manager/__init__.py index 65a45126b52b..e69c0ba25cb2 100644 --- a/packages/google-cloud-resource-manager/google/cloud/resource_manager/__init__.py +++ b/packages/google-cloud-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/packages/google-cloud-resource-manager/google/cloud/resource_manager/_http.py b/packages/google-cloud-resource-manager/google/cloud/resource_manager/_http.py index 803be52d4f9b..af3d058ef921 100644 --- a/packages/google-cloud-resource-manager/google/cloud/resource_manager/_http.py +++ b/packages/google-cloud-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/packages/google-cloud-resource-manager/unit_tests/test__http.py b/packages/google-cloud-resource-manager/unit_tests/test__http.py index f9204492eeac..a5e0e4a77666 100644 --- a/packages/google-cloud-resource-manager/unit_tests/test__http.py +++ b/packages/google-cloud-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, + )