From 50aa873696eba1708049ea67dd52a2a129b5908b Mon Sep 17 00:00:00 2001 From: Danny Hermes Date: Wed, 22 Feb 2017 07:40:10 -0800 Subject: [PATCH] Adding GCCL header for HTTP APIs. (#3046) --- .../google/cloud/language/__init__.py | 3 ++ .../google/cloud/language/_http.py | 9 ++++++ .../unit_tests/test__http.py | 32 +++++++++++++++++++ 3 files changed, 44 insertions(+) diff --git a/packages/google-cloud-language/google/cloud/language/__init__.py b/packages/google-cloud-language/google/cloud/language/__init__.py index 93e9fd2fc67c..9519b930c333 100644 --- a/packages/google-cloud-language/google/cloud/language/__init__.py +++ b/packages/google-cloud-language/google/cloud/language/__init__.py @@ -15,6 +15,9 @@ """Client library for Google Cloud Natural Language API.""" +from pkg_resources import get_distribution +__version__ = get_distribution('google-cloud-language').version + from google.cloud.language.client import Client from google.cloud.language.document import Document from google.cloud.language.document import Encoding diff --git a/packages/google-cloud-language/google/cloud/language/_http.py b/packages/google-cloud-language/google/cloud/language/_http.py index f419c49bd941..af0ad516677f 100644 --- a/packages/google-cloud-language/google/cloud/language/_http.py +++ b/packages/google-cloud-language/google/cloud/language/_http.py @@ -16,6 +16,11 @@ from google.cloud import _http +from google.cloud.language import __version__ + + +_CLIENT_INFO = _http.CLIENT_INFO_TEMPLATE.format(__version__) + class Connection(_http.JSONConnection): """A connection to Google Cloud Natural Language JSON REST API. @@ -32,3 +37,7 @@ class Connection(_http.JSONConnection): API_URL_TEMPLATE = '{api_base_url}/{api_version}/documents:{path}' """A template for the URL of a particular API call.""" + + _EXTRA_HEADERS = { + _http.CLIENT_INFO_HEADER: _CLIENT_INFO, + } diff --git a/packages/google-cloud-language/unit_tests/test__http.py b/packages/google-cloud-language/unit_tests/test__http.py index 496422ee2c8b..6071c697fd01 100644 --- a/packages/google-cloud-language/unit_tests/test__http.py +++ b/packages/google-cloud-language/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): method = 'annotateText' uri += ':' + method self.assertEqual(conn.build_api_url(method), uri) + + def test_extra_headers(self): + from google.cloud import _http as base_http + from google.cloud.language 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, + )