Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 6e2a184

Browse filesBrowse files
dhermeslukesneeringer
authored andcommitted
Adding GCCL header for HTTP APIs. (#3046)
1 parent 40bc923 commit 6e2a184
Copy full SHA for 6e2a184

File tree

Expand file treeCollapse file tree

3 files changed

+44
-0
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

3 files changed

+44
-0
lines changed
Open diff view settings
Collapse file

‎packages/google-cloud-speech/google/cloud/speech/__init__.py‎

Copy file name to clipboardExpand all lines: packages/google-cloud-speech/google/cloud/speech/__init__.py
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414

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

17+
18+
from pkg_resources import get_distribution
19+
__version__ = get_distribution('google-cloud-speech').version
20+
1721
from google.cloud.speech.alternative import Alternative
1822
from google.cloud.speech.client import Client
1923
from google.cloud.speech.encoding import Encoding
Collapse file

‎packages/google-cloud-speech/google/cloud/speech/_http.py‎

Copy file name to clipboardExpand all lines: packages/google-cloud-speech/google/cloud/speech/_http.py
+8Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,14 @@
2020
from google.cloud._helpers import _to_bytes
2121
from google.cloud import _http
2222

23+
from google.cloud.speech import __version__
2324
from google.cloud.speech.result import Result
2425
from google.cloud.speech.operation import Operation
2526

2627

28+
_CLIENT_INFO = _http.CLIENT_INFO_TEMPLATE.format(__version__)
29+
30+
2731
class Connection(_http.JSONConnection):
2832
"""A connection to Google Cloud Speech JSON REST API.
2933
@@ -40,6 +44,10 @@ class Connection(_http.JSONConnection):
4044
API_URL_TEMPLATE = '{api_base_url}/{api_version}/{path}'
4145
"""A template for the URL of a particular API call."""
4246

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

4452
class HTTPSpeechAPI(object):
4553
"""Speech API for interacting with the HTTP version of the API.
Collapse file

‎packages/google-cloud-speech/unit_tests/test__http.py‎

Copy file name to clipboardExpand all lines: packages/google-cloud-speech/unit_tests/test__http.py
+32Lines 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

@@ -36,3 +38,33 @@ def test_build_api_url(self):
3638
])
3739

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

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.