Skip to content

Commit

Permalink
Merge pull request #3057 from dhermes/move-connection-off-client
Browse files Browse the repository at this point in the history
Isolating Connection objects in _http modules (where relevant).
  • Loading branch information
dhermes authored Feb 24, 2017
2 parents 165cb6b + 7e29a3d commit f66ac9d
Show file tree
Hide file tree
Showing 13 changed files with 115 additions and 89 deletions.
2 changes: 1 addition & 1 deletion error_reporting/google/cloud/error_reporting/_gax.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def make_report_error_api(client):
:returns: An Error Reporting API instance.
"""
channel = make_secure_channel(
client._connection.credentials,
client._credentials,
DEFAULT_USER_AGENT,
report_errors_service_client.ReportErrorsServiceClient.SERVICE_ADDRESS)
gax_client = report_errors_service_client.ReportErrorsServiceClient(
Expand Down
6 changes: 5 additions & 1 deletion error_reporting/unit_tests/test__gax.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ def test_make_report_error_api(self):
from google.cloud.error_reporting import __version__
from google.cloud.error_reporting._gax import make_report_error_api

client = mock.Mock()
client = mock.Mock(
_credentials=mock.sentinel.credentials,
project='prahj-ekt',
spec=['project', '_credentials'],
)

# Mock out the constructor for the GAPIC client.
ServiceClient = report_errors_service_client.ReportErrorsServiceClient
Expand Down
6 changes: 3 additions & 3 deletions logging/google/cloud/logging/_gax.py
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,7 @@ def make_gax_logging_api(client):
:returns: A metrics API instance with the proper credentials.
"""
channel = make_secure_channel(
client._connection.credentials, DEFAULT_USER_AGENT,
client._credentials, DEFAULT_USER_AGENT,
LoggingServiceV2Client.SERVICE_ADDRESS)
generated = LoggingServiceV2Client(
channel=channel, lib_name='gccl', lib_version=__version__)
Expand All @@ -550,7 +550,7 @@ def make_gax_metrics_api(client):
:returns: A metrics API instance with the proper credentials.
"""
channel = make_secure_channel(
client._connection.credentials, DEFAULT_USER_AGENT,
client._credentials, DEFAULT_USER_AGENT,
MetricsServiceV2Client.SERVICE_ADDRESS)
generated = MetricsServiceV2Client(
channel=channel, lib_name='gccl', lib_version=__version__)
Expand All @@ -567,7 +567,7 @@ def make_gax_sinks_api(client):
:returns: A metrics API instance with the proper credentials.
"""
channel = make_secure_channel(
client._connection.credentials, DEFAULT_USER_AGENT,
client._credentials, DEFAULT_USER_AGENT,
ConfigServiceV2Client.SERVICE_ADDRESS)
generated = ConfigServiceV2Client(
channel=channel, lib_name='gccl', lib_version=__version__)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ class BackgroundThreadTransport(Transport):
def __init__(self, client, name):
http = copy.deepcopy(client._http)
self.client = client.__class__(
client.project, client._connection.credentials, http)
client.project, client._credentials, http)
logger = self.client.logger(name)
self.worker = _Worker(logger)

Expand Down
9 changes: 3 additions & 6 deletions logging/unit_tests/test__gax.py
Original file line number Diff line number Diff line change
Expand Up @@ -1113,8 +1113,7 @@ def test_it(self):
from google.cloud.logging._gax import DEFAULT_USER_AGENT

creds = object()
conn = mock.Mock(credentials=creds, spec=['credentials'])
client = mock.Mock(_connection=conn, spec=['_connection'])
client = mock.Mock(_credentials=creds, spec=['_credentials'])
channels = []
channel_args = []
generated_api_kwargs = []
Expand Down Expand Up @@ -1167,8 +1166,7 @@ def test_it(self):
from google.cloud.logging._gax import DEFAULT_USER_AGENT

creds = object()
conn = mock.Mock(credentials=creds, spec=['credentials'])
client = mock.Mock(_connection=conn, spec=['_connection'])
client = mock.Mock(_credentials=creds, spec=['_credentials'])
channels = []
channel_args = []
generated_api_kwargs = []
Expand Down Expand Up @@ -1221,8 +1219,7 @@ def test_it(self):
from google.cloud.logging._gax import DEFAULT_USER_AGENT

creds = object()
conn = mock.Mock(credentials=creds, spec=['credentials'])
client = mock.Mock(_connection=conn, spec=['_connection'])
client = mock.Mock(_credentials=creds, spec=['_credentials'])
channels = []
channel_args = []
generated_api_kwargs = []
Expand Down
2 changes: 1 addition & 1 deletion speech/google/cloud/speech/_gax.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class GAPICSpeechAPI(object):
"""Manage calls through GAPIC wrappers to the Speech API."""
def __init__(self, client=None):
self._client = client
credentials = self._client._connection.credentials
credentials = self._client._credentials
channel = make_secure_channel(
credentials, DEFAULT_USER_AGENT,
SpeechClient.SERVICE_ADDRESS)
Expand Down
2 changes: 1 addition & 1 deletion speech/google/cloud/speech/_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class HTTPSpeechAPI(object):
"""
def __init__(self, client):
self._client = client
self._connection = client._connection
self._connection = Connection(client)

def async_recognize(self, sample, language_code=None,
max_alternatives=None, profanity_filter=None,
Expand Down
3 changes: 0 additions & 3 deletions speech/google/cloud/speech/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
from google.cloud.environment_vars import DISABLE_GRPC

from google.cloud.speech._gax import GAPICSpeechAPI
from google.cloud.speech._http import Connection
from google.cloud.speech._http import HTTPSpeechAPI
from google.cloud.speech.sample import Sample

Expand Down Expand Up @@ -58,8 +57,6 @@ class Client(BaseClient):

def __init__(self, credentials=None, http=None, use_gax=None):
super(Client, self).__init__(credentials=credentials, http=http)
self._connection = Connection(self)

# Save on the actual client class whether we use GAX or not.
if use_gax is None:
self._use_gax = _USE_GAX
Expand Down
9 changes: 7 additions & 2 deletions speech/unit_tests/test__gax.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,16 @@ def test_constructor(self, mocked_stub, mocked_cls, mocked_channel):
from google.cloud.speech import __version__
from google.cloud.speech._gax import OPERATIONS_API_HOST

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

speech_api = self._make_one(mock_client)
self.assertIs(speech_api._client, mock_client)
Expand Down
58 changes: 25 additions & 33 deletions speech/unit_tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,11 @@ def _make_one(self, *args, **kw):
return self._get_target_class()(*args, **kw)

def test_ctor(self):
from google.cloud.speech._http import Connection

creds = _make_credentials()
http = object()
client = self._make_one(credentials=creds, http=http)
self.assertIsInstance(client._connection, Connection)
self.assertTrue(client._connection.credentials is creds)
self.assertTrue(client._connection.http is http)
self.assertTrue(client._credentials is creds)
self.assertTrue(client._http is http)

def test_ctor_use_gax_preset(self):
creds = _make_credentials()
Expand Down Expand Up @@ -153,7 +150,9 @@ def test_sync_recognize_content_with_optional_params_no_gax(self):
}
credentials = _make_credentials()
client = self._make_one(credentials=credentials, use_gax=False)
client._connection = _Connection(RETURNED)
speech_api = client.speech_api
connection = _Connection(RETURNED)
speech_api._connection = connection

encoding = speech.Encoding.FLAC

Expand All @@ -165,8 +164,8 @@ def test_sync_recognize_content_with_optional_params_no_gax(self):
profanity_filter=True,
speech_context=self.HINTS)

self.assertEqual(len(client._connection._requested), 1)
req = client._connection._requested[0]
self.assertEqual(len(connection._requested), 1)
req = connection._requested[0]
self.assertEqual(len(req), 3)
self.assertEqual(req['data'], REQUEST)
self.assertEqual(req['method'], 'POST')
Expand Down Expand Up @@ -199,7 +198,9 @@ 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._connection = _Connection(RETURNED)
speech_api = client.speech_api
connection = _Connection(RETURNED)
speech_api._connection = connection

encoding = speech.Encoding.FLAC

Expand All @@ -208,8 +209,8 @@ def test_sync_recognize_source_uri_without_optional_params_no_gax(self):

response = [i for i in sample.sync_recognize()]

self.assertEqual(len(client._connection._requested), 1)
req = client._connection._requested[0]
self.assertEqual(len(connection._requested), 1)
req = connection._requested[0]
self.assertEqual(len(req), 3)
self.assertEqual(req['data'], REQUEST)
self.assertEqual(req['method'], 'POST')
Expand All @@ -231,7 +232,8 @@ def test_sync_recognize_with_empty_results_no_gax(self):

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

sample = client.sample(source_uri=self.AUDIO_SOURCE_URI,
encoding=speech.Encoding.FLAC,
Expand All @@ -248,8 +250,7 @@ def test_sync_recognize_with_empty_results_gax(self):

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

channel_args = []
channel_obj = object()
Expand Down Expand Up @@ -291,9 +292,7 @@ def test_sync_recognize_with_gax(self):

creds = _make_credentials()
client = self._make_one(credentials=creds, use_gax=True)
client._connection = _Connection()
client._connection.credentials = creds
client._speech_api = None
client._credentials = creds

alternatives = [{
'transcript': 'testing 1 2 3',
Expand Down Expand Up @@ -352,8 +351,7 @@ def test_async_supported_encodings(self):
from google.cloud import speech

credentials = _make_credentials()
client = self._make_one(credentials=credentials)
client._connection = _Connection({})
client = self._make_one(credentials=credentials, use_gax=True)

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

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

sample = client.sample(source_uri=self.AUDIO_SOURCE_URI,
encoding=speech.Encoding.LINEAR16,
Expand All @@ -393,8 +392,7 @@ def test_async_recognize_with_gax(self):
credentials = _make_credentials()
client = self._make_one(credentials=credentials,
use_gax=True)
client._connection = _Connection()
client._connection.credentials = credentials
client._credentials = credentials

channel_args = []
channel_obj = object()
Expand Down Expand Up @@ -434,7 +432,6 @@ def test_streaming_depends_on_gax(self):

credentials = _make_credentials()
client = self._make_one(credentials=credentials, use_gax=False)
client.connection = _Connection()
sample = client.sample(content=self.AUDIO_CONTENT,
encoding=speech.Encoding.LINEAR16,
sample_rate=self.SAMPLE_RATE)
Expand All @@ -453,8 +450,7 @@ def test_streaming_closed_stream(self):
stream = BytesIO(b'Some audio data...')
credentials = _make_credentials()
client = self._make_one(credentials=credentials)
client.connection = _Connection()
client.connection.credentials = credentials
client._credentials = credentials

channel_args = []
channel_obj = object()
Expand Down Expand Up @@ -495,8 +491,7 @@ def test_stream_recognize_interim_results(self):
stream = BytesIO(b'Some audio data...')
credentials = _make_credentials()
client = self._make_one(credentials=credentials)
client.connection = _Connection()
client.connection.credentials = credentials
client._credentials = credentials

alternatives = [{
'transcript': 'testing streaming 1 2 3',
Expand Down Expand Up @@ -576,8 +571,7 @@ def test_stream_recognize(self):
stream = BytesIO(b'Some audio data...')
credentials = _make_credentials()
client = self._make_one(credentials=credentials)
client.connection = _Connection()
client.connection.credentials = credentials
client._credentials = credentials

alternatives = [{
'transcript': 'testing streaming 1 2 3',
Expand Down Expand Up @@ -633,8 +627,7 @@ def test_stream_recognize_no_results(self):
stream = BytesIO(b'Some audio data...')
credentials = _make_credentials()
client = self._make_one(credentials=credentials)
client.connection = _Connection()
client.connection.credentials = credentials
client._credentials = credentials

responses = [_make_streaming_response()]

Expand Down Expand Up @@ -670,8 +663,7 @@ def test_speech_api_with_gax(self):

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

channel_args = []
channel_obj = object()
Expand Down
2 changes: 1 addition & 1 deletion vision/google/cloud/vision/_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class _HTTPVisionAPI(object):

def __init__(self, client):
self._client = client
self._connection = client._connection
self._connection = Connection(client)

def annotate(self, images):
"""Annotate an image to discover it's attributes.
Expand Down
2 changes: 0 additions & 2 deletions vision/google/cloud/vision/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

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

Expand Down Expand Up @@ -66,7 +65,6 @@ def __init__(self, project=None, credentials=None, http=None,
use_gax=None):
super(Client, self).__init__(
project=project, credentials=credentials, http=http)
self._connection = Connection(self)
if use_gax is None:
self._use_gax = _USE_GAX
else:
Expand Down
Loading

0 comments on commit f66ac9d

Please sign in to comment.