Skip to content

Commit 30e8e33

Browse files
dhermeslukesneeringer
authored andcommitted
Adding GCCL header for HTTP APIs. (#3046)
1 parent 283fcf4 commit 30e8e33

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

packages/google-cloud-runtimeconfig/google/cloud/runtimeconfig/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,8 @@
1414

1515
"""Google Cloud Runtime Configurator API package."""
1616

17+
18+
from pkg_resources import get_distribution
19+
__version__ = get_distribution('google-cloud-runtimeconfig').version
20+
1721
from google.cloud.runtimeconfig.client import Client

packages/google-cloud-runtimeconfig/google/cloud/runtimeconfig/_http.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@
1818

1919
from google.cloud import _http
2020

21+
from google.cloud.runtimeconfig import __version__
22+
23+
24+
_CLIENT_INFO = _http.CLIENT_INFO_TEMPLATE.format(__version__)
25+
2126

2227
class Connection(_http.JSONConnection):
2328
"""A connection to Google Cloud RuntimeConfig via the JSON REST API.
@@ -34,3 +39,7 @@ class Connection(_http.JSONConnection):
3439

3540
API_URL_TEMPLATE = '{api_base_url}/{api_version}{path}'
3641
"""A template for the URL of a particular API call."""
42+
43+
_EXTRA_HEADERS = {
44+
_http.CLIENT_INFO_HEADER: _CLIENT_INFO,
45+
}

packages/google-cloud-runtimeconfig/unit_tests/test__http.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
import unittest
1616

17+
import mock
18+
1719

1820
class TestConnection(unittest.TestCase):
1921

@@ -30,3 +32,33 @@ def test_default_url(self):
3032
client = object()
3133
conn = self._make_one(client)
3234
self.assertIs(conn._client, client)
35+
36+
def test_extra_headers(self):
37+
from google.cloud import _http as base_http
38+
from google.cloud.runtimeconfig import _http as MUT
39+
40+
http = mock.Mock(spec=['request'])
41+
response = mock.Mock(status=200, spec=['status'])
42+
data = b'brent-spiner'
43+
http.request.return_value = response, data
44+
client = mock.Mock(_http=http, spec=['_http'])
45+
46+
conn = self._make_one(client)
47+
req_data = 'req-data-boring'
48+
result = conn.api_request(
49+
'GET', '/rainbow', data=req_data, expect_json=False)
50+
self.assertEqual(result, data)
51+
52+
expected_headers = {
53+
'Content-Length': str(len(req_data)),
54+
'Accept-Encoding': 'gzip',
55+
base_http.CLIENT_INFO_HEADER: MUT._CLIENT_INFO,
56+
'User-Agent': conn.USER_AGENT,
57+
}
58+
expected_uri = conn.build_api_url('/rainbow')
59+
http.request.assert_called_once_with(
60+
body=req_data,
61+
headers=expected_headers,
62+
method='GET',
63+
uri=expected_uri,
64+
)

0 commit comments

Comments
 (0)