Skip to content

Commit 1634604

Browse files
dhermeslukesneeringer
authored andcommitted
Adding GCCL header for HTTP APIs. (#3046)
1 parent e90b561 commit 1634604

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
"""Google Stackdriver Logging API wrapper."""
1616

1717

18+
from pkg_resources import get_distribution
19+
__version__ = get_distribution('google-cloud-logging').version
20+
1821
from google.cloud.logging.client import Client
1922

2023

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

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

1919
from google.cloud import _http
2020
from google.cloud.iterator import HTTPIterator
21+
22+
from google.cloud.logging import __version__
2123
from google.cloud.logging._helpers import entry_from_resource
2224
from google.cloud.logging.sink import Sink
2325
from google.cloud.logging.metric import Metric
2426

2527

28+
_CLIENT_INFO = _http.CLIENT_INFO_TEMPLATE.format(__version__)
29+
30+
2631
class Connection(_http.JSONConnection):
2732
"""A connection to Google Stackdriver Logging via the JSON REST API.
2833
@@ -39,6 +44,10 @@ class Connection(_http.JSONConnection):
3944
API_URL_TEMPLATE = '{api_base_url}/{api_version}{path}'
4045
"""A template for the URL of a particular API call."""
4146

47+
_EXTRA_HEADERS = {
48+
_http.CLIENT_INFO_HEADER: _CLIENT_INFO,
49+
}
50+
4251

4352
class _LoggingAPI(object):
4453
"""Helper mapping logging-related APIs.

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,36 @@ def test_default_url(self):
4242
conn = self._make_one(client)
4343
self.assertIs(conn._client, client)
4444

45+
def test_extra_headers(self):
46+
from google.cloud import _http as base_http
47+
from google.cloud.logging import _http as MUT
48+
49+
http = mock.Mock(spec=['request'])
50+
response = mock.Mock(status=200, spec=['status'])
51+
data = b'brent-spiner'
52+
http.request.return_value = response, data
53+
client = mock.Mock(_http=http, spec=['_http'])
54+
55+
conn = self._make_one(client)
56+
req_data = 'req-data-boring'
57+
result = conn.api_request(
58+
'GET', '/rainbow', data=req_data, expect_json=False)
59+
self.assertEqual(result, data)
60+
61+
expected_headers = {
62+
'Content-Length': str(len(req_data)),
63+
'Accept-Encoding': 'gzip',
64+
base_http.CLIENT_INFO_HEADER: MUT._CLIENT_INFO,
65+
'User-Agent': conn.USER_AGENT,
66+
}
67+
expected_uri = conn.build_api_url('/rainbow')
68+
http.request.assert_called_once_with(
69+
body=req_data,
70+
headers=expected_headers,
71+
method='GET',
72+
uri=expected_uri,
73+
)
74+
4575

4676
class Test_LoggingAPI(unittest.TestCase):
4777

0 commit comments

Comments
 (0)