Skip to content

Commit aa5bce5

Browse files
committed
Add a starter class for _gax.
1 parent 455eaf6 commit aa5bce5

File tree

4 files changed

+91
-4
lines changed

4 files changed

+91
-4
lines changed

vision/google/cloud/vision/_gax.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Copyright 2016 Google Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"""GAX Client for interacting with the Google Cloud Vision API."""
16+
17+
18+
class _GAPICVisionAPI(object):
19+
"""Vision API for interacting with the gRPC version of Vision.
20+
21+
:type client: :class:`~google.cloud.core.client.Client`
22+
:param client: Instance of ``Client`` object.
23+
"""
24+
def __init__(self, client=None):
25+
raise NotImplementedError

vision/google/cloud/vision/client.py

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,20 @@
1414

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

17+
import os
18+
1719
from google.cloud.client import JSONClient
20+
from google.cloud.environment_vars import DISABLE_GRPC
21+
22+
from google.cloud.vision._gax import _GAPICVisionAPI
1823
from google.cloud.vision.connection import Connection
1924
from google.cloud.vision.image import Image
2025
from google.cloud.vision._http import _HTTPVisionAPI
2126

2227

28+
_USE_GAX = not os.getenv(DISABLE_GRPC, False)
29+
30+
2331
class Client(JSONClient):
2432
"""Client to bundle configuration needed for API requests.
2533
@@ -40,14 +48,25 @@ class Client(JSONClient):
4048
:meth:`~httplib2.Http.request`. If not passed, an
4149
``http`` object is created that is bound to the
4250
``credentials`` for the current object.
51+
52+
:type use_gax: bool
53+
:param use_gax: (Optional) Explicitly specifies whether
54+
to use the gRPC transport (via GAX) or HTTP. If unset,
55+
falls back to the ``GOOGLE_CLOUD_DISABLE_GRPC`` environment
56+
variable
4357
"""
4458
_vision_api_internal = None
4559

46-
def __init__(self, project=None, credentials=None, http=None):
60+
def __init__(self, project=None, credentials=None, http=None,
61+
use_gax=False):
4762
super(Client, self).__init__(
4863
project=project, credentials=credentials, http=http)
4964
self._connection = Connection(
5065
credentials=self._credentials, http=self._http)
66+
if use_gax is None:
67+
self._use_gax = _USE_GAX
68+
else:
69+
self._use_gax = use_gax
5170

5271
def image(self, content=None, filename=None, source_uri=None):
5372
"""Get instance of Image using current client.
@@ -71,9 +90,14 @@ def image(self, content=None, filename=None, source_uri=None):
7190
def _vision_api(self):
7291
"""Proxy method that handles which transport call Vision Annotate.
7392
74-
:rtype: :class:`~google.cloud.vision._rest._HTTPVisionAPI`
75-
:returns: Instance of ``_HTTPVisionAPI`` used to make requests.
93+
:rtype: :class:`~google.cloud.vision._http._HTTPVisionAPI`
94+
or :class:`~google.cloud.vision._gax._GAPICVisionAPI`
95+
:returns: Instance of ``_HTTPVisionAPI`` or ``_GAPICVisionAPI`` used to
96+
make requests.
7697
"""
7798
if self._vision_api_internal is None:
78-
self._vision_api_internal = _HTTPVisionAPI(self)
99+
if self._use_gax:
100+
self._vision_api_internal = _GAPICVisionAPI(self)
101+
else:
102+
self._vision_api_internal = _HTTPVisionAPI(self)
79103
return self._vision_api_internal

vision/unit_tests/test__gax.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Copyright 2016 Google Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import unittest
16+
17+
18+
class TestGAXClient(unittest.TestCase):
19+
def _get_target_class(self):
20+
from google.cloud.vision._gax import _GAPICVisionAPI
21+
return _GAPICVisionAPI
22+
23+
def _make_one(self, *args, **kwargs):
24+
return self._get_target_class()(*args, **kwargs)
25+
26+
def test_gax_not_implemented(self):
27+
client = object()
28+
with self.assertRaises(NotImplementedError):
29+
self._make_one(client=client)

vision/unit_tests/test_client.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,15 @@ def test_annotate_with_preset_api(self):
5555
client._vision_api.annotate()
5656
api.annotate.assert_called_once_with()
5757

58+
def test_gax_not_implemented_from_client(self):
59+
credentials = _make_credentials()
60+
client = self._make_one(project=PROJECT, credentials=credentials,
61+
use_gax=None)
62+
client._connection = _Connection()
63+
64+
with self.assertRaises(NotImplementedError):
65+
client._vision_api()
66+
5867
def test_face_annotation(self):
5968
from google.cloud.vision.feature import Feature, FeatureTypes
6069
from unit_tests._fixtures import FACE_DETECTION_RESPONSE

0 commit comments

Comments
 (0)