From 4a058020e8b2ecf87c3d0e95999f9ec73ad13497 Mon Sep 17 00:00:00 2001 From: Danny Hermes Date: Thu, 15 Dec 2016 12:11:54 -0800 Subject: [PATCH] Removing most (direct) connection usage in Logging. --- logging/google/cloud/logging/_gax.py | 6 ++-- logging/google/cloud/logging/_http.py | 29 +++++++++---------- .../handlers/transports/background_thread.py | 6 ++-- .../transports/test_background_thread.py | 12 ++------ logging/unit_tests/test__gax.py | 18 ++---------- logging/unit_tests/test__http.py | 8 ++--- logging/unit_tests/test_client.py | 17 ++++++----- 7 files changed, 38 insertions(+), 58 deletions(-) diff --git a/logging/google/cloud/logging/_gax.py b/logging/google/cloud/logging/_gax.py index 0ffd46dd3fcb..7ddadba01d06 100644 --- a/logging/google/cloud/logging/_gax.py +++ b/logging/google/cloud/logging/_gax.py @@ -532,7 +532,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) return _LoggingAPI(generated, client) @@ -548,7 +548,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) return _MetricsAPI(generated, client) @@ -564,7 +564,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) return _SinksAPI(generated, client) diff --git a/logging/google/cloud/logging/_http.py b/logging/google/cloud/logging/_http.py index 8d9eccc819d5..8056689235db 100644 --- a/logging/google/cloud/logging/_http.py +++ b/logging/google/cloud/logging/_http.py @@ -12,7 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -"""Create / interact with Stackdriver Logging connections.""" +"""Interact with Stackdriver Logging via JSON-over-HTTP.""" import functools @@ -67,7 +67,7 @@ class _LoggingAPI(object): def __init__(self, client): self._client = client - self._connection = client._connection + self.api_request = client._connection.api_request def list_entries(self, projects, filter_=None, order_by=None, page_size=None, page_token=None): @@ -161,8 +161,7 @@ def write_entries(self, entries, logger_name=None, resource=None, if labels is not None: data['labels'] = labels - self._connection.api_request(method='POST', path='/entries:write', - data=data) + self.api_request(method='POST', path='/entries:write', data=data) def logger_delete(self, project, logger_name): """API call: delete all entries in a logger via a DELETE request @@ -177,7 +176,7 @@ def logger_delete(self, project, logger_name): :param logger_name: name of logger containing the log entries to delete """ path = '/projects/%s/logs/%s' % (project, logger_name) - self._connection.api_request(method='DELETE', path=path) + self.api_request(method='DELETE', path=path) class _SinksAPI(object): @@ -191,7 +190,7 @@ class _SinksAPI(object): """ def __init__(self, client): self._client = client - self._connection = client._connection + self.api_request = client._connection.api_request def list_sinks(self, project, page_size=None, page_token=None): """List sinks for the project associated with this client. @@ -253,7 +252,7 @@ def sink_create(self, project, sink_name, filter_, destination): 'filter': filter_, 'destination': destination, } - self._connection.api_request(method='POST', path=target, data=data) + self.api_request(method='POST', path=target, data=data) def sink_get(self, project, sink_name): """API call: retrieve a sink resource. @@ -271,7 +270,7 @@ def sink_get(self, project, sink_name): :returns: The JSON sink object returned from the API. """ target = '/projects/%s/sinks/%s' % (project, sink_name) - return self._connection.api_request(method='GET', path=target) + return self.api_request(method='GET', path=target) def sink_update(self, project, sink_name, filter_, destination): """API call: update a sink resource. @@ -299,7 +298,7 @@ def sink_update(self, project, sink_name, filter_, destination): 'filter': filter_, 'destination': destination, } - self._connection.api_request(method='PUT', path=target, data=data) + self.api_request(method='PUT', path=target, data=data) def sink_delete(self, project, sink_name): """API call: delete a sink resource. @@ -314,7 +313,7 @@ def sink_delete(self, project, sink_name): :param sink_name: the name of the sink """ target = '/projects/%s/sinks/%s' % (project, sink_name) - self._connection.api_request(method='DELETE', path=target) + self.api_request(method='DELETE', path=target) class _MetricsAPI(object): @@ -328,7 +327,7 @@ class _MetricsAPI(object): """ def __init__(self, client): self._client = client - self._connection = client._connection + self.api_request = client._connection.api_request def list_metrics(self, project, page_size=None, page_token=None): """List metrics for the project associated with this client. @@ -389,7 +388,7 @@ def metric_create(self, project, metric_name, filter_, description=None): 'filter': filter_, 'description': description, } - self._connection.api_request(method='POST', path=target, data=data) + self.api_request(method='POST', path=target, data=data) def metric_get(self, project, metric_name): """API call: retrieve a metric resource. @@ -407,7 +406,7 @@ def metric_get(self, project, metric_name): :returns: The JSON metric object returned from the API. """ target = '/projects/%s/metrics/%s' % (project, metric_name) - return self._connection.api_request(method='GET', path=target) + return self.api_request(method='GET', path=target) def metric_update(self, project, metric_name, filter_, description): """API call: update a metric resource. @@ -434,7 +433,7 @@ def metric_update(self, project, metric_name, filter_, description): 'filter': filter_, 'description': description, } - self._connection.api_request(method='PUT', path=target, data=data) + self.api_request(method='PUT', path=target, data=data) def metric_delete(self, project, metric_name): """API call: delete a metric resource. @@ -449,7 +448,7 @@ def metric_delete(self, project, metric_name): :param metric_name: the name of the metric. """ target = '/projects/%s/metrics/%s' % (project, metric_name) - self._connection.api_request(method='DELETE', path=target) + self.api_request(method='DELETE', path=target) def _item_to_entry(iterator, resource, loggers): diff --git a/logging/google/cloud/logging/handlers/transports/background_thread.py b/logging/google/cloud/logging/handlers/transports/background_thread.py index c090474a540b..9c8ea85c937a 100644 --- a/logging/google/cloud/logging/handlers/transports/background_thread.py +++ b/logging/google/cloud/logging/handlers/transports/background_thread.py @@ -150,9 +150,9 @@ class BackgroundThreadTransport(Transport): """ def __init__(self, client, name): - http = copy.deepcopy(client._connection.http) - self.client = client.__class__(client.project, - client._connection.credentials, http) + http = copy.deepcopy(client._http) + self.client = client.__class__( + client.project, client._credentials, http) logger = self.client.logger(name) self.worker = _Worker(logger) diff --git a/logging/unit_tests/handlers/transports/test_background_thread.py b/logging/unit_tests/handlers/transports/test_background_thread.py index 5ca76a2f68c3..a21302f251d7 100644 --- a/logging/unit_tests/handlers/transports/test_background_thread.py +++ b/logging/unit_tests/handlers/transports/test_background_thread.py @@ -157,13 +157,6 @@ def commit(self): del self.entries[:] -class _Connection(object): - - def __init__(self): - self.http = None - self.credentials = object() - - class _Logger(object): def __init__(self, name): @@ -178,9 +171,8 @@ class _Client(object): def __init__(self, project, http=None, credentials=None): self.project = project - self.http = http - self.credentials = credentials - self._connection = _Connection() + self._http = http + self._credentials = credentials def logger(self, name): # pylint: disable=unused-argument self._logger = _Logger(name) diff --git a/logging/unit_tests/test__gax.py b/logging/unit_tests/test__gax.py index 4d269236e3e6..e2f158ffd0cc 100644 --- a/logging/unit_tests/test__gax.py +++ b/logging/unit_tests/test__gax.py @@ -1085,7 +1085,7 @@ def test_it(self): from google.cloud.logging._gax import DEFAULT_USER_AGENT creds = object() - client = _Client(creds) + client = mock.Mock(_credentials=creds) channels = [] channel_args = [] channel_obj = object() @@ -1130,7 +1130,7 @@ def test_it(self): from google.cloud.logging._gax import DEFAULT_USER_AGENT creds = object() - client = _Client(creds) + client = mock.Mock(_credentials=creds) channels = [] channel_args = [] channel_obj = object() @@ -1175,7 +1175,7 @@ def test_it(self): from google.cloud.logging._gax import DEFAULT_USER_AGENT creds = object() - client = _Client(creds) + client = mock.Mock(_credentials=creds) channels = [] channel_args = [] channel_obj = object() @@ -1324,15 +1324,3 @@ def delete_log_metric(self, metric_name, options=None): raise GaxError('error') if self._log_metric_not_found: raise GaxError('notfound', self._make_grpc_not_found()) - - -class _Connection(object): - - def __init__(self, credentials): - self.credentials = credentials - - -class _Client(object): - - def __init__(self, credentials): - self._connection = _Connection(credentials) diff --git a/logging/unit_tests/test__http.py b/logging/unit_tests/test__http.py index 6fe8c825feef..bfc8d7981e46 100644 --- a/logging/unit_tests/test__http.py +++ b/logging/unit_tests/test__http.py @@ -58,11 +58,11 @@ def _make_one(self, *args, **kw): return self._get_target_class()(*args, **kw) def test_ctor(self): - connection = object() + connection = _Connection() client = _Client(connection) api = self._make_one(client) - self.assertIs(api._connection, connection) self.assertIs(api._client, client) + self.assertEqual(api.api_request, connection.api_request) @staticmethod def _make_timestamp(): @@ -308,11 +308,11 @@ def _make_one(self, *args, **kw): return self._get_target_class()(*args, **kw) def test_ctor(self): - connection = _make_credentials() + connection = _Connection() client = _Client(connection) api = self._make_one(client) - self.assertIs(api._connection, connection) self.assertIs(api._client, client) + self.assertEqual(api.api_request, connection.api_request) def test_list_sinks_no_paging(self): import six diff --git a/logging/unit_tests/test_client.py b/logging/unit_tests/test_client.py index 5e48f7b95367..0e215ad1f510 100644 --- a/logging/unit_tests/test_client.py +++ b/logging/unit_tests/test_client.py @@ -52,11 +52,12 @@ def test_logging_api_wo_gax(self): client = self._make_one(self.PROJECT, credentials=_make_credentials(), use_gax=False) - conn = client._connection = object() + + conn = client._connection = _Connection() api = client.logging_api self.assertIsInstance(api, _LoggingAPI) - self.assertIs(api._connection, conn) + self.assertEqual(api.api_request, conn.api_request) # API instance is cached again = client.logging_api self.assertIs(again, api) @@ -106,11 +107,11 @@ def test_sinks_api_wo_gax(self): self.PROJECT, credentials=_make_credentials(), use_gax=False) - conn = client._connection = object() + conn = client._connection = _Connection() api = client.sinks_api self.assertIsInstance(api, _SinksAPI) - self.assertIs(api._connection, conn) + self.assertEqual(api.api_request, conn.api_request) # API instance is cached again = client.sinks_api self.assertIs(again, api) @@ -146,11 +147,11 @@ def test_metrics_api_wo_gax(self): self.PROJECT, credentials=_make_credentials(), use_gax=False) - conn = client._connection = object() + conn = client._connection = _Connection() api = client.metrics_api self.assertIsInstance(api, _MetricsAPI) - self.assertIs(api._connection, conn) + self.assertEqual(api.api_request, conn.api_request) # API instance is cached again = client.metrics_api self.assertIs(again, api) @@ -600,7 +601,7 @@ def test_get_default_handler_general(self): credentials=credentials, use_gax=False) handler = client.get_default_handler() - deepcopy.assert_called_once_with(client._connection.http) + deepcopy.assert_called_once_with(client._http) self.assertIsInstance(handler, CloudLoggingHandler) @@ -620,7 +621,7 @@ def test_setup_logging(self): credentials=credentials, use_gax=False) client.setup_logging() - deepcopy.assert_called_once_with(client._connection.http) + deepcopy.assert_called_once_with(client._http) setup_logging.assert_called()