Skip to content

Commit

Permalink
Renaming http argument(s) as _http. (#3235)
Browse files Browse the repository at this point in the history
  • Loading branch information
dhermes authored and lukesneeringer committed Mar 30, 2017
1 parent 6ff7de0 commit ddef81b
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 41 deletions.
44 changes: 24 additions & 20 deletions google-cloud-speech/google/cloud/speech/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,44 +24,48 @@
from google.cloud.speech.sample import Sample


_USE_GAX = not os.getenv(DISABLE_GRPC, False)
_USE_GRPC = not os.getenv(DISABLE_GRPC, False)


class Client(BaseClient):
"""Client to bundle configuration needed for API requests.
:type credentials: :class:`~google.auth.credentials.Credentials`
:param credentials: (Optional) The OAuth2 Credentials to use for this
client. If not passed (and if no ``http`` object is
client. If not passed (and if no ``_http`` object is
passed), falls back to the default inferred from the
environment.
:type http: :class:`~httplib2.Http`
:param http: (Optional) HTTP object to make requests. Can be any object
that defines ``request()`` with the same interface as
: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
:type _http: :class:`~httplib2.Http`
:param _http: (Optional) HTTP object to make requests. Can be any object
that defines ``request()`` with the same interface as
:meth:`~httplib2.Http.request`. If not passed, an
``_http`` object is created that is bound to the
``credentials`` for the current object.
This parameter should be considered private, and could
change in the future.
:type _use_grpc: bool
:param _use_grpc: (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.
This parameter should be considered private, and could
change in the future.
"""

SCOPE = ('https://www.googleapis.com/auth/cloud-platform',)
"""The scopes required for authenticating as an API consumer."""

_speech_api = None

def __init__(self, credentials=None, http=None, use_gax=None):
super(Client, self).__init__(credentials=credentials, http=http)
def __init__(self, credentials=None, _http=None, _use_grpc=None):
super(Client, self).__init__(credentials=credentials, _http=_http)
# Save on the actual client class whether we use GAX or not.
if use_gax is None:
self._use_gax = _USE_GAX
if _use_grpc is None:
self._use_grpc = _USE_GRPC
else:
self._use_gax = use_gax
self._use_grpc = _use_grpc

def sample(self, content=None, source_uri=None, stream=None, encoding=None,
sample_rate=None):
Expand Down Expand Up @@ -104,7 +108,7 @@ def sample(self, content=None, source_uri=None, stream=None, encoding=None,
def speech_api(self):
"""Helper for speech-related API calls."""
if self._speech_api is None:
if self._use_gax:
if self._use_grpc:
self._speech_api = GAPICSpeechAPI(self)
else:
self._speech_api = HTTPSpeechAPI(self)
Expand Down
2 changes: 1 addition & 1 deletion google-cloud-speech/google/cloud/speech/sample.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ def streaming_recognize(self, language_code=None,
:raises: EnvironmentError if gRPC is not available.
"""
if not self._client._use_gax:
if not self._client._use_grpc:
raise EnvironmentError('gRPC is required to use this API.')

api = self._client.speech_api
Expand Down
10 changes: 5 additions & 5 deletions google-cloud-speech/tests/system.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,12 @@ class Config(object):
"""
CLIENT = None
TEST_BUCKET = None
USE_GAX = True
USE_GRPC = True


def setUpModule():
Config.CLIENT = speech.Client()
Config.USE_GAX = Config.CLIENT._use_gax
Config.USE_GRPC = Config.CLIENT._use_grpc
# Now create a bucket for GCS stored content.
storage_client = storage.Client()
bucket_name = 'new' + unique_resource_id()
Expand Down Expand Up @@ -195,15 +195,15 @@ def test_async_recognize_gcs_file(self):
self._check_results(alternatives, 2)

def test_stream_recognize(self):
if not Config.USE_GAX:
if not Config.USE_GRPC:
self.skipTest('gRPC is required for Speech Streaming Recognize.')

with open(AUDIO_FILE, 'rb') as file_obj:
for results in self._make_streaming_request(file_obj):
self._check_results(results.alternatives)

def test_stream_recognize_interim_results(self):
if not Config.USE_GAX:
if not Config.USE_GRPC:
self.skipTest('gRPC is required for Speech Streaming Recognize.')

# These extra words are interim_results that the API returns as it's
Expand All @@ -223,7 +223,7 @@ def test_stream_recognize_interim_results(self):
self._check_results(responses[-1].alternatives)

def test_stream_recognize_single_utterance(self):
if not Config.USE_GAX:
if not Config.USE_GRPC:
self.skipTest('gRPC is required for Speech Streaming Recognize.')

with open(AUDIO_FILE, 'rb') as file_obj:
Expand Down
30 changes: 15 additions & 15 deletions google-cloud-speech/tests/unit/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,15 @@ def _make_one(self, *args, **kw):
def test_ctor(self):
creds = _make_credentials()
http = object()
client = self._make_one(credentials=creds, http=http)
client = self._make_one(credentials=creds, _http=http)
self.assertTrue(client._credentials is creds)
self.assertTrue(client._http is http)

def test_ctor_use_gax_preset(self):
def test_ctor_use_grpc_preset(self):
creds = _make_credentials()
http = object()
client = self._make_one(credentials=creds, http=http, use_gax=True)
self.assertTrue(client._use_gax)
client = self._make_one(credentials=creds, _http=http, _use_grpc=True)
self.assertTrue(client._use_grpc)

def test_create_sample_from_client(self):
from google.cloud import speech
Expand Down Expand Up @@ -148,7 +148,7 @@ def test_sync_recognize_content_with_optional_params_no_gax(self):
}
}
credentials = _make_credentials()
client = self._make_one(credentials=credentials, use_gax=False)
client = self._make_one(credentials=credentials, _use_grpc=False)
speech_api = client.speech_api
connection = _Connection(SYNC_RECOGNIZE_RESPONSE)
speech_api._connection = connection
Expand Down Expand Up @@ -196,7 +196,7 @@ def test_sync_recognize_source_uri_without_optional_params_no_gax(self):
},
}
credentials = _make_credentials()
client = self._make_one(credentials=credentials, use_gax=False)
client = self._make_one(credentials=credentials, _use_grpc=False)
speech_api = client.speech_api
connection = _Connection(SYNC_RECOGNIZE_RESPONSE)
speech_api._connection = connection
Expand Down Expand Up @@ -233,7 +233,7 @@ def test_sync_recognize_with_empty_results_no_gax(self):
from tests.unit._fixtures import SYNC_RECOGNIZE_EMPTY_RESPONSE

credentials = _make_credentials()
client = self._make_one(credentials=credentials, use_gax=False)
client = self._make_one(credentials=credentials, _use_grpc=False)
speech_api = client.speech_api
speech_api._connection = _Connection(SYNC_RECOGNIZE_EMPTY_RESPONSE)

Expand All @@ -251,7 +251,7 @@ def test_sync_recognize_with_empty_results_gax(self):
from google.cloud.speech import _gax

credentials = _make_credentials()
client = self._make_one(credentials=credentials, use_gax=True)
client = self._make_one(credentials=credentials, _use_grpc=True)
client._credentials = credentials

channel_args = []
Expand Down Expand Up @@ -293,7 +293,7 @@ def test_sync_recognize_with_gax(self):
from google.cloud.speech import _gax

creds = _make_credentials()
client = self._make_one(credentials=creds, use_gax=True)
client = self._make_one(credentials=creds, _use_grpc=True)
client._credentials = creds

alternatives = [{
Expand Down Expand Up @@ -354,7 +354,7 @@ def test_async_supported_encodings(self):
from google.cloud import speech

credentials = _make_credentials()
client = self._make_one(credentials=credentials, use_gax=True)
client = self._make_one(credentials=credentials, _use_grpc=True)

sample = client.sample(
source_uri=self.AUDIO_SOURCE_URI, encoding=speech.Encoding.FLAC,
Expand All @@ -370,7 +370,7 @@ def test_async_recognize_no_gax(self):
RETURNED = ASYNC_RECOGNIZE_RESPONSE

credentials = _make_credentials()
client = self._make_one(credentials=credentials, use_gax=False)
client = self._make_one(credentials=credentials, _use_grpc=False)
speech_api = client.speech_api
speech_api._connection = _Connection(RETURNED)

Expand All @@ -393,7 +393,7 @@ def test_async_recognize_with_gax(self):
from google.cloud.speech.operation import Operation

credentials = _make_credentials()
client = self._make_one(credentials=credentials, use_gax=True)
client = self._make_one(credentials=credentials, _use_grpc=True)
client._credentials = credentials

channel_args = []
Expand Down Expand Up @@ -433,7 +433,7 @@ def test_streaming_depends_on_gax(self):
from google.cloud import speech

credentials = _make_credentials()
client = self._make_one(credentials=credentials, use_gax=False)
client = self._make_one(credentials=credentials, _use_grpc=False)
sample = client.sample(
content=self.AUDIO_CONTENT, encoding=speech.Encoding.LINEAR16,
sample_rate=self.SAMPLE_RATE)
Expand Down Expand Up @@ -673,7 +673,7 @@ def test_speech_api_with_gax(self):
from google.cloud.speech import _gax

creds = _make_credentials()
client = self._make_one(credentials=creds, use_gax=True)
client = self._make_one(credentials=creds, _use_grpc=True)
client._credentials = creds

channel_args = []
Expand Down Expand Up @@ -705,7 +705,7 @@ def test_speech_api_without_gax(self):
from google.cloud.speech._http import HTTPSpeechAPI

creds = _make_credentials()
client = self._make_one(credentials=creds, use_gax=False)
client = self._make_one(credentials=creds, _use_grpc=False)
self.assertIsNone(client._speech_api)
self.assertIsInstance(client.speech_api, HTTPSpeechAPI)
self.assertIsInstance(client.speech_api._connection, Connection)
Expand Down

0 comments on commit ddef81b

Please sign in to comment.