diff --git a/packages/google-cloud-python-speech/google/cloud/speech/__init__.py b/packages/google-cloud-python-speech/google/cloud/speech/__init__.py index 0c4f7542e02f..67b9aaf512fa 100644 --- a/packages/google-cloud-python-speech/google/cloud/speech/__init__.py +++ b/packages/google-cloud-python-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/packages/google-cloud-python-speech/google/cloud/speech/_http.py b/packages/google-cloud-python-speech/google/cloud/speech/_http.py index a4755f6f89ff..3b82a25802ef 100644 --- a/packages/google-cloud-python-speech/google/cloud/speech/_http.py +++ b/packages/google-cloud-python-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/packages/google-cloud-python-speech/unit_tests/test__http.py b/packages/google-cloud-python-speech/unit_tests/test__http.py index fec987a07f37..a590a71ab6a5 100644 --- a/packages/google-cloud-python-speech/unit_tests/test__http.py +++ b/packages/google-cloud-python-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, + )