Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GAPIC Header Consistency: Vision #3050

Merged
merged 9 commits into from
Feb 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions vision/google/cloud/vision/_gax.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
"""GAX Client for interacting with the Google Cloud Vision API."""

from google.cloud.gapic.vision.v1 import image_annotator_client
from google.cloud.grpc.vision.v1 import image_annotator_pb2
from google.cloud.proto.vision.v1 import image_annotator_pb2

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


Expand All @@ -28,7 +29,9 @@ class _GAPICVisionAPI(object):
"""
def __init__(self, client=None):
self._client = client
self._annotator_client = image_annotator_client.ImageAnnotatorClient()
self._annotator_client = image_annotator_client.ImageAnnotatorClient(
credentials=client._credentials, lib_name='gccl',
lib_version=__version__)

def annotate(self, images):
"""Annotate images through GAX.
Expand Down Expand Up @@ -64,7 +67,7 @@ def _to_gapic_feature(feature):
:param feature: Local ``Feature`` class to be converted to gRPC ``Feature``
instance.

:rtype: :class:`~google.cloud.grpc.vision.v1.image_annotator_pb2.Feature`
:rtype: :class:`~google.cloud.proto.vision.v1.image_annotator_pb2.Feature`
:returns: gRPC ``Feature`` converted from
:class:`~google.cloud.vision.feature.Feature`.
"""
Expand All @@ -79,7 +82,7 @@ def _to_gapic_image(image):
:type image: :class:`~google.cloud.vision.image.Image`
:param image: Local ``Image`` class to be converted to gRPC ``Image``.

:rtype: :class:`~google.cloud.grpc.vision.v1.image_annotator_pb2.Image`
:rtype: :class:`~google.cloud.proto.vision.v1.image_annotator_pb2.Image`
:returns: gRPC ``Image`` converted from
:class:`~google.cloud.vision.image.Image`.
"""
Expand Down
12 changes: 6 additions & 6 deletions vision/google/cloud/vision/annotations.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def from_api_repr(cls, response):
def from_pb(cls, response):
"""Factory: construct an instance of ``Annotations`` from protobuf.

:type response: :class:`~google.cloud.grpc.vision.v1.\
:type response: :class:`~google.cloud.proto.vision.v1.\
image_annotator_pb2.AnnotateImageResponse`
:param response: ``AnnotateImageResponse`` from protobuf call.

Expand All @@ -113,7 +113,7 @@ def from_pb(cls, response):
def _process_image_annotations(image):
"""Helper for processing annotation types from protobuf.

:type image: :class:`~google.cloud.grpc.vision.v1.image_annotator_pb2.\
:type image: :class:`~google.cloud.proto.vision.v1.image_annotator_pb2.\
AnnotateImageResponse`
:param image: ``AnnotateImageResponse`` from protobuf.

Expand All @@ -137,7 +137,7 @@ def _make_entity_from_pb(annotations):
"""Create an entity from a protobuf response.

:type annotations:
:class:`~google.cloud.grpc.vision.v1.image_annotator_pb2.EntityAnnotation`
:class:`~google.cloud.proto.vision.v1.image_annotator_pb2.EntityAnnotation`
:param annotations: protobuf instance of ``EntityAnnotation``.

:rtype: list
Expand All @@ -150,7 +150,7 @@ def _make_faces_from_pb(faces):
"""Create face objects from a protobuf response.

:type faces:
:class:`~google.cloud.grpc.vision.v1.image_annotator_pb2.FaceAnnotation`
:class:`~google.cloud.proto.vision.v1.image_annotator_pb2.FaceAnnotation`
:param faces: Protobuf instance of ``FaceAnnotation``.

:rtype: list
Expand All @@ -162,7 +162,7 @@ def _make_faces_from_pb(faces):
def _make_image_properties_from_pb(image_properties):
"""Create ``ImageProperties`` object from a protobuf response.

:type image_properties: :class:`~google.cloud.grpc.vision.v1.\
:type image_properties: :class:`~google.cloud.proto.vision.v1.\
image_annotator_pb2.ImagePropertiesAnnotation`
:param image_properties: Protobuf instance of
``ImagePropertiesAnnotation``.
Expand All @@ -176,7 +176,7 @@ def _make_image_properties_from_pb(image_properties):
def _make_safe_search_from_pb(safe_search):
"""Create ``SafeSearchAnnotation`` object from a protobuf response.

:type safe_search: :class:`~google.cloud.grpc.vision.v1.\
:type safe_search: :class:`~google.cloud.proto.vision.v1.\
image_annotator_pb2.SafeSearchAnnotation`
:param safe_search: Protobuf instance of ``SafeSearchAnnotation``.

Expand Down
4 changes: 2 additions & 2 deletions vision/google/cloud/vision/color.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def from_api_repr(cls, image_properties):
def from_pb(cls, image_properties):
"""Factory: construct ``ImagePropertiesAnnotation`` from a response.

:type image_properties: :class:`~google.cloud.grpc.vision.v1.\
:type image_properties: :class:`~google.cloud.proto.vision.v1.\
image_annotator_pb2.ImageProperties`
:param image_properties: Protobuf response from Vision API with image
properties data.
Expand Down Expand Up @@ -196,7 +196,7 @@ def from_api_repr(cls, color_information):
def from_pb(cls, color_information):
"""Factory: construct ``ColorInformation`` for a color.

:type color_information: :class:`~google.cloud.grpc.vision.v1.\
:type color_information: :class:`~google.cloud.proto.vision.v1.\
image_annotator_pb2.ColorInfo`
:param color_information: Color data with extra meta information.

Expand Down
2 changes: 1 addition & 1 deletion vision/google/cloud/vision/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def from_api_repr(cls, response):
def from_pb(cls, response):
"""Factory: construct entity from Vision gRPC response.

:type response: :class:`~google.cloud.grpc.vision.v1.\
:type response: :class:`~google.cloud.proto.vision.v1.\
image_annotator_pb2.AnnotateImageResponse`
:param response: gRPC response from Vision API with entity data.

Expand Down
10 changes: 5 additions & 5 deletions vision/google/cloud/vision/face.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def from_api_repr(cls, angle):
def from_pb(cls, angle):
"""Factory: convert protobuf Angle object to local Angle object.

:type angle: :class:`~google.cloud.grpc.vision.v1.\
:type angle: :class:`~google.cloud.proto.vision.v1.\
image_annotator_pb2.FaceAnnotation`
:param angle: Protobuf ``FaceAnnotation`` response with angle data.

Expand Down Expand Up @@ -126,7 +126,7 @@ def from_api_repr(cls, emotions):
def from_pb(cls, emotions):
"""Factory: construct ``Emotions`` from Vision API response.

:type emotions: :class:`~google.cloud.grpc.vision.v1.\
:type emotions: :class:`~google.cloud.proto.vision.v1.\
image_annotator_pb2.FaceAnnotation`
:param emotions: Response dictionary representing a face with emotions.

Expand Down Expand Up @@ -225,7 +225,7 @@ def from_api_repr(cls, face):
def from_pb(cls, face):
"""Factory: construct an instance of a Face from an protobuf response

:type face: :class:`~google.cloud.grpc.vision.v1.\
:type face: :class:`~google.cloud.proto.vision.v1.\
image_annotator_pb2.AnnotateImageResponse`
:param face: ``AnnotateImageResponse`` from gRPC call.

Expand Down Expand Up @@ -397,7 +397,7 @@ def from_api_repr(cls, face):
def from_pb(cls, face):
"""Factory: construct image properties from image.

:type face: :class:`~google.cloud.grpc.vision.v1.image_annotator_pb2.\
:type face: :class:`~google.cloud.proto.vision.v1.image_annotator_pb2.\
FaceAnnotation`
:param face: Protobuf instace of `Face`.

Expand Down Expand Up @@ -508,7 +508,7 @@ def from_api_repr(cls, landmark):
def from_pb(cls, landmark):
"""Factory: construct an instance of a Landmark from a response.

:type landmark: :class:`~google.cloud.grpc.vision.v1.\
:type landmark: :class:`~google.cloud.proto.vision.v1.\
image_annotator_pb.FaceAnnotation.Landmark`
:param landmark: Landmark representation from Vision API.

Expand Down
2 changes: 1 addition & 1 deletion vision/google/cloud/vision/geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def from_api_repr(cls, vertices):
def from_pb(cls, vertices):
"""Factory: construct BoundsBase instance from a protobuf response.

:type vertices: :class:`~google.cloud.grpc.vision.v1.\
:type vertices: :class:`~google.cloud.proto.vision.v1.\
geometry_pb2.BoundingPoly`
:param vertices: List of vertices.

Expand Down
2 changes: 1 addition & 1 deletion vision/google/cloud/vision/likelihood.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

from enum import Enum

from google.cloud.grpc.vision.v1 import image_annotator_pb2
from google.cloud.proto.vision.v1 import image_annotator_pb2


def _get_pb_likelihood(likelihood):
Expand Down
4 changes: 2 additions & 2 deletions vision/google/cloud/vision/safe_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ def from_api_repr(cls, response):
def from_pb(cls, image):
"""Factory: construct SafeSearchAnnotation from Vision API response.

:type image: :class:`~google.cloud.grpc.vision.v1.image_annotator_pb2.\
SafeSearchAnnotation`
:type image: :class:`~google.cloud.proto.vision.v1.\
image_annotator_pb2.SafeSearchAnnotation`
:param image: Protobuf response from Vision API with safe search data.

:rtype: :class:`~google.cloud.vision.safe_search.SafeSearchAnnotation`
Expand Down
4 changes: 2 additions & 2 deletions vision/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@
REQUIREMENTS = [
'enum34',
'google-cloud-core >= 0.23.0, < 0.24dev',
'gapic-google-cloud-vision-v1 >= 0.14.0, < 0.15dev',
'gapic-google-cloud-vision-v1 >= 0.15.0, < 0.16dev',
]

setup(
name='google-cloud-vision',
version='0.22.0',
version='0.23.0',
description='Python Client for Google Cloud Vision',
long_description=README,
namespace_packages=[
Expand Down
62 changes: 55 additions & 7 deletions vision/unit_tests/test__gax.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
import mock


def _make_credentials():
import google.auth.credentials
return mock.Mock(spec=google.auth.credentials.Credentials)


class TestGAXClient(unittest.TestCase):
def _get_target_class(self):
from google.cloud.vision._gax import _GAPICVisionAPI
Expand All @@ -32,12 +37,55 @@ def test_ctor(self):
api = self._make_one(client)
self.assertIs(api._client, client)

def test_gapic_credentials(self):
from google.cloud.gapic.vision.v1.image_annotator_client import (
ImageAnnotatorClient)
from google.cloud.vision import Client

# Mock the GAPIC ImageAnnotatorClient, whose arguments we
# want to check.
with mock.patch.object(ImageAnnotatorClient, '__init__') as iac:
iac.return_value = None

# Create the GAX client.
credentials = _make_credentials()
client = Client(credentials=credentials, project='foo')
self._make_one(client=client)

# Assert that the GAPIC constructor was called once, and
# that the credentials were sent.
iac.assert_called_once()
_, _, kwargs = iac.mock_calls[0]
self.assertIs(kwargs['credentials'], credentials)

def test_kwarg_lib_name(self):
from google.cloud.gapic.vision.v1.image_annotator_client import (
ImageAnnotatorClient)
from google.cloud.vision import __version__
from google.cloud.vision import Client

# Mock the GAPIC ImageAnnotatorClient, whose arguments we
# want to check.
with mock.patch.object(ImageAnnotatorClient, '__init__') as iac:
iac.return_value = None

# Create the GAX client.
client = Client(credentials=_make_credentials(), project='foo')
self._make_one(client=client)

# Assert that the GAPIC constructor was called once, and
# that lib_name and lib_version were sent.
iac.assert_called_once()
_, _, kwargs = iac.mock_calls[0]
self.assertEqual(kwargs['lib_name'], 'gccl')
self.assertEqual(kwargs['lib_version'], __version__)

def test_annotation(self):
from google.cloud.vision.feature import Feature
from google.cloud.vision.feature import FeatureTypes
from google.cloud.vision.image import Image

client = mock.Mock(spec_set=[])
client = mock.Mock(spec_set=['_credentials'])
feature = Feature(FeatureTypes.LABEL_DETECTION, 5)
image_content = b'abc 1 2 3'
image = Image(client, content=image_content)
Expand All @@ -64,7 +112,7 @@ def test_annotate_no_results(self):
from google.cloud.vision.feature import FeatureTypes
from google.cloud.vision.image import Image

client = mock.Mock(spec_set=[])
client = mock.Mock(spec_set=['_credentials'])
feature = Feature(FeatureTypes.LABEL_DETECTION, 5)
image_content = b'abc 1 2 3'
image = Image(client, content=image_content)
Expand All @@ -87,13 +135,13 @@ def test_annotate_no_results(self):
gax_api._annotator_client.batch_annotate_images.assert_called()

def test_annotate_multiple_results(self):
from google.cloud.grpc.vision.v1 import image_annotator_pb2
from google.cloud.proto.vision.v1 import image_annotator_pb2
from google.cloud.vision.annotations import Annotations
from google.cloud.vision.feature import Feature
from google.cloud.vision.feature import FeatureTypes
from google.cloud.vision.image import Image

client = mock.Mock(spec_set=[])
client = mock.Mock(spec_set=['_credentials'])
feature = Feature(FeatureTypes.LABEL_DETECTION, 5)
image_content = b'abc 1 2 3'
image = Image(client, content=image_content)
Expand Down Expand Up @@ -128,7 +176,7 @@ def _call_fut(self, feature):
def test__to_gapic_feature(self):
from google.cloud.vision.feature import Feature
from google.cloud.vision.feature import FeatureTypes
from google.cloud.grpc.vision.v1 import image_annotator_pb2
from google.cloud.proto.vision.v1 import image_annotator_pb2

feature = Feature(FeatureTypes.LABEL_DETECTION, 5)
feature_pb = self._call_fut(feature)
Expand All @@ -144,7 +192,7 @@ def _call_fut(self, image):

def test__to_gapic_image_content(self):
from google.cloud.vision.image import Image
from google.cloud.grpc.vision.v1 import image_annotator_pb2
from google.cloud.proto.vision.v1 import image_annotator_pb2

image_content = b'abc 1 2 3'
client = object()
Expand All @@ -155,7 +203,7 @@ def test__to_gapic_image_content(self):

def test__to_gapic_image_uri(self):
from google.cloud.vision.image import Image
from google.cloud.grpc.vision.v1 import image_annotator_pb2
from google.cloud.proto.vision.v1 import image_annotator_pb2

image_uri = 'gs://1234/34.jpg'
client = object()
Expand Down
12 changes: 6 additions & 6 deletions vision/unit_tests/test_annotations.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@


def _make_pb_entity():
from google.cloud.grpc.vision.v1 import geometry_pb2
from google.cloud.grpc.vision.v1 import image_annotator_pb2
from google.cloud.proto.vision.v1 import geometry_pb2
from google.cloud.proto.vision.v1 import image_annotator_pb2
from google.type import latlng_pb2

description = 'testing 1 2 3'
Expand Down Expand Up @@ -78,7 +78,7 @@ def test_unsupported_http_annotation(self):
def test_from_pb(self):
from google.cloud.vision.likelihood import Likelihood
from google.cloud.vision.safe_search import SafeSearchAnnotation
from google.cloud.grpc.vision.v1 import image_annotator_pb2
from google.cloud.proto.vision.v1 import image_annotator_pb2

image_response = image_annotator_pb2.AnnotateImageResponse()
annotations = self._make_one().from_pb(image_response)
Expand Down Expand Up @@ -131,7 +131,7 @@ def _call_fut(self, annotations):
return _make_faces_from_pb(annotations)

def test_it(self):
from google.cloud.grpc.vision.v1 import image_annotator_pb2
from google.cloud.proto.vision.v1 import image_annotator_pb2
from google.cloud.vision.face import Face

faces_pb = [image_annotator_pb2.FaceAnnotation()]
Expand All @@ -147,7 +147,7 @@ def _call_fut(self, annotations):
return _make_image_properties_from_pb(annotations)

def test_it(self):
from google.cloud.grpc.vision.v1 import image_annotator_pb2
from google.cloud.proto.vision.v1 import image_annotator_pb2
from google.protobuf.wrappers_pb2 import FloatValue
from google.type.color_pb2 import Color

Expand Down Expand Up @@ -178,7 +178,7 @@ def _call_fut(self, image):
return _process_image_annotations(image)

def test_it(self):
from google.cloud.grpc.vision.v1 import image_annotator_pb2
from google.cloud.proto.vision.v1 import image_annotator_pb2

description = 'testing 1 2 3'
locale = 'US'
Expand Down
4 changes: 2 additions & 2 deletions vision/unit_tests/test_color.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def _get_target_class():
return ImagePropertiesAnnotation

def test_image_properties_annotation_from_pb(self):
from google.cloud.grpc.vision.v1 import image_annotator_pb2
from google.cloud.proto.vision.v1 import image_annotator_pb2
from google.protobuf.wrappers_pb2 import FloatValue
from google.type.color_pb2 import Color

Expand All @@ -121,7 +121,7 @@ def test_image_properties_annotation_from_pb(self):
self.assertEqual(image_properties.colors[0].color.alpha, 1.0)

def test_empty_image_properties_annotation_from_pb(self):
from google.cloud.grpc.vision.v1 import image_annotator_pb2
from google.cloud.proto.vision.v1 import image_annotator_pb2

image_properties_pb = image_annotator_pb2.ImageProperties()

Expand Down
Loading