Skip to content

Commit

Permalink
Adding GCCL header for HTTP APIs. (#3046)
Browse files Browse the repository at this point in the history
  • Loading branch information
dhermes authored and lukesneeringer committed Feb 22, 2017
1 parent 5961058 commit 50b8eef
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
4 changes: 4 additions & 0 deletions packages/google-cloud-vision/google/cloud/vision/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,8 @@

"""Google Cloud Vision API package."""


from pkg_resources import get_distribution
__version__ = get_distribution('google-cloud-vision').version

from google.cloud.vision.client import Client
9 changes: 9 additions & 0 deletions packages/google-cloud-vision/google/cloud/vision/_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,17 @@

"""HTTP Client for interacting with the Google Cloud Vision API."""


from google.cloud import _http

from google.cloud.vision import __version__
from google.cloud.vision.annotations import Annotations
from google.cloud.vision.feature import Feature


_CLIENT_INFO = _http.CLIENT_INFO_TEMPLATE.format(__version__)


class Connection(_http.JSONConnection):
"""A connection to Google Cloud Vision via the JSON REST API.
Expand All @@ -36,6 +41,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 _HTTPVisionAPI(object):
"""Vision API for interacting with the JSON/HTTP version of Vision
Expand Down
30 changes: 30 additions & 0 deletions packages/google-cloud-vision/unit_tests/test__http.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,36 @@ def test_default_url(self):
conn = self._make_one(client)
self.assertEqual(conn._client, client)

def test_extra_headers(self):
from google.cloud import _http as base_http
from google.cloud.vision 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,
)


class Test_HTTPVisionAPI(unittest.TestCase):
def _get_target_class(self):
Expand Down

0 comments on commit 50b8eef

Please sign in to comment.