Skip to content

Commit e730b60

Browse files
dhermeslukesneeringer
authored andcommitted
Adding GCCL header for HTTP APIs. (#3046)
1 parent 4dee135 commit e730b60

3 files changed

Lines changed: 45 additions & 0 deletions

File tree

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

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

1515
"""Google Cloud Translation API wrapper."""
1616

17+
18+
from pkg_resources import get_distribution
19+
__version__ = get_distribution('google-cloud-translate').version
20+
1721
from google.cloud.translate.client import BASE
1822
from google.cloud.translate.client import Client
1923
from google.cloud.translate.client import NMT

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

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

1717
from google.cloud import _http
1818

19+
from google.cloud.translate import __version__
20+
21+
22+
_CLIENT_INFO = _http.CLIENT_INFO_TEMPLATE.format(__version__)
23+
1924

2025
class Connection(_http.JSONConnection):
2126
"""A connection to Google Cloud Translation API via the JSON REST API.
@@ -32,3 +37,7 @@ class Connection(_http.JSONConnection):
3237

3338
API_URL_TEMPLATE = '{api_base_url}/language/translate/{api_version}{path}'
3439
"""A template for the URL of a particular API call."""
40+
41+
_EXTRA_HEADERS = {
42+
_http.CLIENT_INFO_HEADER: _CLIENT_INFO,
43+
}

packages/google-cloud-translate/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

@@ -52,3 +54,33 @@ def test_build_api_url_w_extra_query_params(self):
5254
self.assertEqual(path, expected_path)
5355
params = parse_qsl(qs)
5456
self.assertEqual(params, query_params)
57+
58+
def test_extra_headers(self):
59+
from google.cloud import _http as base_http
60+
from google.cloud.translate import _http as MUT
61+
62+
http = mock.Mock(spec=['request'])
63+
response = mock.Mock(status=200, spec=['status'])
64+
data = b'brent-spiner'
65+
http.request.return_value = response, data
66+
client = mock.Mock(_http=http, spec=['_http'])
67+
68+
conn = self._make_one(client)
69+
req_data = 'req-data-boring'
70+
result = conn.api_request(
71+
'GET', '/rainbow', data=req_data, expect_json=False)
72+
self.assertEqual(result, data)
73+
74+
expected_headers = {
75+
'Content-Length': str(len(req_data)),
76+
'Accept-Encoding': 'gzip',
77+
base_http.CLIENT_INFO_HEADER: MUT._CLIENT_INFO,
78+
'User-Agent': conn.USER_AGENT,
79+
}
80+
expected_uri = conn.build_api_url('/rainbow')
81+
http.request.assert_called_once_with(
82+
body=req_data,
83+
headers=expected_headers,
84+
method='GET',
85+
uri=expected_uri,
86+
)

0 commit comments

Comments
 (0)