Skip to content

Commit

Permalink
Add a starter class for _gax.
Browse files Browse the repository at this point in the history
  • Loading branch information
daspecster committed Dec 27, 2016
1 parent 455eaf6 commit aa5bce5
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 4 deletions.
25 changes: 25 additions & 0 deletions vision/google/cloud/vision/_gax.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Copyright 2016 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

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


class _GAPICVisionAPI(object):
"""Vision API for interacting with the gRPC version of Vision.
:type client: :class:`~google.cloud.core.client.Client`
:param client: Instance of ``Client`` object.
"""
def __init__(self, client=None):
raise NotImplementedError
32 changes: 28 additions & 4 deletions vision/google/cloud/vision/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,20 @@

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

import os

from google.cloud.client import JSONClient
from google.cloud.environment_vars import DISABLE_GRPC

from google.cloud.vision._gax import _GAPICVisionAPI
from google.cloud.vision.connection import Connection
from google.cloud.vision.image import Image
from google.cloud.vision._http import _HTTPVisionAPI


_USE_GAX = not os.getenv(DISABLE_GRPC, False)


class Client(JSONClient):
"""Client to bundle configuration needed for API requests.
Expand All @@ -40,14 +48,25 @@ class Client(JSONClient):
:meth:`~httplib2.Http.request`. If not passed, an
``http`` object is created that is bound to the
``credentials`` for the current object.
:type use_gax: bool
:param use_gax: (Optional) Explicitly specifies whether
to use the gRPC transport (via GAX) or HTTP. If unset,
falls back to the ``GOOGLE_CLOUD_DISABLE_GRPC`` environment
variable
"""
_vision_api_internal = None

def __init__(self, project=None, credentials=None, http=None):
def __init__(self, project=None, credentials=None, http=None,
use_gax=False):
super(Client, self).__init__(
project=project, credentials=credentials, http=http)
self._connection = Connection(
credentials=self._credentials, http=self._http)
if use_gax is None:
self._use_gax = _USE_GAX
else:
self._use_gax = use_gax

def image(self, content=None, filename=None, source_uri=None):
"""Get instance of Image using current client.
Expand All @@ -71,9 +90,14 @@ def image(self, content=None, filename=None, source_uri=None):
def _vision_api(self):
"""Proxy method that handles which transport call Vision Annotate.
:rtype: :class:`~google.cloud.vision._rest._HTTPVisionAPI`
:returns: Instance of ``_HTTPVisionAPI`` used to make requests.
:rtype: :class:`~google.cloud.vision._http._HTTPVisionAPI`
or :class:`~google.cloud.vision._gax._GAPICVisionAPI`
:returns: Instance of ``_HTTPVisionAPI`` or ``_GAPICVisionAPI`` used to
make requests.
"""
if self._vision_api_internal is None:
self._vision_api_internal = _HTTPVisionAPI(self)
if self._use_gax:
self._vision_api_internal = _GAPICVisionAPI(self)
else:
self._vision_api_internal = _HTTPVisionAPI(self)
return self._vision_api_internal
29 changes: 29 additions & 0 deletions vision/unit_tests/test__gax.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Copyright 2016 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import unittest


class TestGAXClient(unittest.TestCase):
def _get_target_class(self):
from google.cloud.vision._gax import _GAPICVisionAPI
return _GAPICVisionAPI

def _make_one(self, *args, **kwargs):
return self._get_target_class()(*args, **kwargs)

def test_gax_not_implemented(self):
client = object()
with self.assertRaises(NotImplementedError):
self._make_one(client=client)
9 changes: 9 additions & 0 deletions vision/unit_tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,15 @@ def test_annotate_with_preset_api(self):
client._vision_api.annotate()
api.annotate.assert_called_once_with()

def test_gax_not_implemented_from_client(self):
credentials = _make_credentials()
client = self._make_one(project=PROJECT, credentials=credentials,
use_gax=None)
client._connection = _Connection()

with self.assertRaises(NotImplementedError):
client._vision_api()

def test_face_annotation(self):
from google.cloud.vision.feature import Feature, FeatureTypes
from unit_tests._fixtures import FACE_DETECTION_RESPONSE
Expand Down

0 comments on commit aa5bce5

Please sign in to comment.