Skip to content

Commit

Permalink
Removing most (direct) connection usage in Logging.
Browse files Browse the repository at this point in the history
  • Loading branch information
dhermes committed Dec 15, 2016
1 parent d8d3475 commit f699caf
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 58 deletions.
6 changes: 3 additions & 3 deletions logging/google/cloud/logging/_gax.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
29 changes: 14 additions & 15 deletions logging/google/cloud/logging/_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand All @@ -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):
Expand All @@ -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.
Expand Down Expand Up @@ -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.
Expand All @@ -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.
Expand Down Expand Up @@ -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.
Expand All @@ -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):
Expand All @@ -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.
Expand Down Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
12 changes: 2 additions & 10 deletions logging/unit_tests/handlers/transports/test_background_thread.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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)
Expand Down
18 changes: 3 additions & 15 deletions logging/unit_tests/test__gax.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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)
8 changes: 4 additions & 4 deletions logging/unit_tests/test__http.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down Expand Up @@ -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
Expand Down
17 changes: 9 additions & 8 deletions logging/unit_tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)

Expand All @@ -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()

Expand Down

0 comments on commit f699caf

Please sign in to comment.