Skip to content

Commit

Permalink
Speech Client Library Header (#3048)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukesneeringer authored Feb 22, 2017
1 parent 94e5e05 commit 2c608f9
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 21 deletions.
10 changes: 8 additions & 2 deletions speech/google/cloud/speech/_gax.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
from google.cloud._helpers import make_secure_stub
from google.cloud._http import DEFAULT_USER_AGENT

from google.cloud.speech import __version__
from google.cloud.speech.operation import Operation
from google.cloud.speech.result import Result

Expand All @@ -45,12 +46,17 @@ def __init__(self, client=None):
channel = make_secure_channel(
credentials, DEFAULT_USER_AGENT,
SpeechClient.SERVICE_ADDRESS)
self._gapic_api = SpeechClient(channel=channel)
self._gapic_api = SpeechClient(
channel=channel,
lib_name='gccl',
lib_version=__version__,
)
self._operations_stub = make_secure_stub(
credentials,
DEFAULT_USER_AGENT,
operations_grpc.OperationsStub,
OPERATIONS_API_HOST)
OPERATIONS_API_HOST,
)

def async_recognize(self, sample, language_code=None,
max_alternatives=None, profanity_filter=None,
Expand Down
4 changes: 2 additions & 2 deletions speech/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@
REQUIREMENTS = [
'google-cloud-core >= 0.23.0, < 0.24dev',
'grpcio >= 1.0.2, < 2.0dev',
'gapic-google-cloud-speech-v1beta1 >= 0.15.0, < 0.16dev',
'gapic-google-cloud-speech-v1beta1 >= 0.15.1, < 0.16dev',
]

setup(
name='google-cloud-speech',
version='0.22.0',
version='0.23.0',
description='Python Client for Google Cloud Speech',
long_description=README,
namespace_packages=[
Expand Down
65 changes: 65 additions & 0 deletions speech/unit_tests/test__gax.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,71 @@

import unittest

import mock


def _make_credentials():
import google.auth.credentials

return mock.Mock(spec=google.auth.credentials.Credentials)


class TestGAPICSpeechAPI(unittest.TestCase):

@staticmethod
def _get_target_class():
from google.cloud.speech._gax import GAPICSpeechAPI

return GAPICSpeechAPI

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

@mock.patch(
'google.cloud._helpers.make_secure_channel',
return_value=mock.sentinel.channel)
@mock.patch(
'google.cloud.gapic.speech.v1beta1.speech_client.SpeechClient',
SERVICE_ADDRESS='hey.you.guys')
@mock.patch(
'google.cloud._helpers.make_secure_stub',
return_value=mock.sentinel.stub)
def test_constructor(self, mocked_stub, mocked_cls, mocked_channel):
from google.longrunning import operations_grpc
from google.cloud._http import DEFAULT_USER_AGENT
from google.cloud.speech import __version__
from google.cloud.speech._gax import OPERATIONS_API_HOST

mock_cnxn = mock.Mock(
credentials=_make_credentials(),
spec=['credentials'],
)
mock_client = mock.Mock(_connection=mock_cnxn, spec=['_connection'])

speech_api = self._make_one(mock_client)
self.assertIs(speech_api._client, mock_client)
self.assertIs(
speech_api._gapic_api,
mocked_cls.return_value,
)

mocked_stub.assert_called_once_with(
mock_cnxn.credentials,
DEFAULT_USER_AGENT,
operations_grpc.OperationsStub,
OPERATIONS_API_HOST,
)
mocked_cls.assert_called_once_with(
channel=mock.sentinel.channel,
lib_name='gccl',
lib_version=__version__,
)
mocked_channel.assert_called_once_with(
mock_cnxn.credentials,
DEFAULT_USER_AGENT,
mocked_cls.SERVICE_ADDRESS,
)


class TestSpeechGAXMakeRequests(unittest.TestCase):
SAMPLE_RATE = 16000
Expand Down
38 changes: 21 additions & 17 deletions speech/unit_tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,9 +258,9 @@ def make_channel(*args):
channel_args.append(args)
return channel_obj

def speech_api(channel=None):
def speech_api(channel=None, **kwargs):
return _MockGAPICSpeechAPI(response=_make_sync_response(),
channel=channel)
channel=channel, **kwargs)

host = 'foo.apis.invalid'
speech_api.SERVICE_ADDRESS = host
Expand Down Expand Up @@ -311,10 +311,10 @@ def make_channel(*args):
channel_args.append(args)
return channel_obj

def speech_api(channel=None):
def speech_api(channel=None, **kwargs):
return _MockGAPICSpeechAPI(
response=_make_sync_response(result),
channel=channel)
channel=channel, **kwargs)

host = 'foo.apis.invalid'
speech_api.SERVICE_ADDRESS = host
Expand Down Expand Up @@ -407,8 +407,8 @@ def make_channel(*args):
encoding=speech.Encoding.LINEAR16,
sample_rate=self.SAMPLE_RATE)

def speech_api(channel=None):
return _MockGAPICSpeechAPI(channel=channel)
def speech_api(channel=None, **kwargs):
return _MockGAPICSpeechAPI(channel=channel, **kwargs)

host = 'foo.apis.invalid'
speech_api.SERVICE_ADDRESS = host
Expand Down Expand Up @@ -463,8 +463,8 @@ def make_channel(*args):
channel_args.append(args)
return channel_obj

def speech_api(channel=None):
return _MockGAPICSpeechAPI(channel=channel)
def speech_api(channel=None, **kwargs):
return _MockGAPICSpeechAPI(channel=channel, **kwargs)

host = 'foo.apis.invalid'
speech_api.SERVICE_ADDRESS = host
Expand Down Expand Up @@ -522,8 +522,9 @@ def make_channel(*args):
channel_args.append(args)
return channel_obj

def speech_api(channel=None):
return _MockGAPICSpeechAPI(channel=channel, response=responses)
def speech_api(channel=None, **kwargs):
return _MockGAPICSpeechAPI(channel=channel, response=responses,
**kwargs)

host = 'foo.apis.invalid'
speech_api.SERVICE_ADDRESS = host
Expand Down Expand Up @@ -599,8 +600,9 @@ def make_channel(*args):
channel_args.append(args)
return channel_obj

def speech_api(channel=None):
return _MockGAPICSpeechAPI(channel=channel, response=responses)
def speech_api(channel=None, **kwargs):
return _MockGAPICSpeechAPI(channel=channel, response=responses,
**kwargs)

host = 'foo.apis.invalid'
speech_api.SERVICE_ADDRESS = host
Expand Down Expand Up @@ -643,8 +645,9 @@ def make_channel(*args):
channel_args.append(args)
return channel_obj

def speech_api(channel=None):
return _MockGAPICSpeechAPI(channel=channel, response=responses)
def speech_api(channel=None, **kwargs):
return _MockGAPICSpeechAPI(channel=channel, response=responses,
**kwargs)

host = 'foo.apis.invalid'
speech_api.SERVICE_ADDRESS = host
Expand Down Expand Up @@ -677,8 +680,8 @@ def make_channel(*args):
channel_args.append(args)
return channel_obj

def speech_api(channel=None):
return _MockGAPICSpeechAPI(channel=channel)
def speech_api(channel=None, **kwargs):
return _MockGAPICSpeechAPI(channel=channel, **kwargs)

host = 'foo.apis.invalid'
speech_api.SERVICE_ADDRESS = host
Expand Down Expand Up @@ -720,9 +723,10 @@ class _MockGAPICSpeechAPI(object):

SERVICE_ADDRESS = 'foo.apis.invalid'

def __init__(self, response=None, channel=None):
def __init__(self, response=None, channel=None, **kwargs):
self._response = response
self._channel = channel
self._kwargs = kwargs

def async_recognize(self, config, audio):
from google.gapic.longrunning.operations_client import OperationsClient
Expand Down

0 comments on commit 2c608f9

Please sign in to comment.