Skip to content

Commit

Permalink
Merge pull request googleapis#2651 from dhermes/logging-iterators-2
Browse files Browse the repository at this point in the history
Passing Client through to low-level logging API objects.
  • Loading branch information
dhermes authored Oct 31, 2016
2 parents 4c161aa + 038b996 commit e25245b
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 67 deletions.
12 changes: 10 additions & 2 deletions logging/google/cloud/logging/_gax.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,13 @@ class _SinksAPI(object):
:type gax_api:
:class:`google.logging.v2.config_service_v2_api.ConfigServiceV2Api`
:param gax_api: API object used to make GAX requests.
:type client: :class:`~google.cloud.logging.client.Client`
:param client: The client that owns this API object.
"""
def __init__(self, gax_api):
def __init__(self, gax_api, client):
self._gax_api = gax_api
self._client = client

def list_sinks(self, project, page_size=0, page_token=None):
"""List sinks for the project associated with this client.
Expand Down Expand Up @@ -291,9 +295,13 @@ class _MetricsAPI(object):
:type gax_api:
:class:`google.logging.v2.metrics_service_v2_api.MetricsServiceV2Api`
:param gax_api: API object used to make GAX requests.
:type client: :class:`~google.cloud.logging.client.Client`
:param client: The client that owns this API object.
"""
def __init__(self, gax_api):
def __init__(self, gax_api, client):
self._gax_api = gax_api
self._client = client

def list_metrics(self, project, page_size=0, page_token=None):
"""List metrics for the project associated with this client.
Expand Down
8 changes: 4 additions & 4 deletions logging/google/cloud/logging/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,9 @@ def sinks_api(self):
if self._sinks_api is None:
if _USE_GAX:
generated = GeneratedSinksAPI()
self._sinks_api = GAXSinksAPI(generated)
self._sinks_api = GAXSinksAPI(generated, self)
else:
self._sinks_api = JSONSinksAPI(self.connection)
self._sinks_api = JSONSinksAPI(self)
return self._sinks_api

@property
Expand All @@ -128,9 +128,9 @@ def metrics_api(self):
if self._metrics_api is None:
if _USE_GAX:
generated = GeneratedMetricsAPI()
self._metrics_api = GAXMetricsAPI(generated)
self._metrics_api = GAXMetricsAPI(generated, self)
else:
self._metrics_api = JSONMetricsAPI(self.connection)
self._metrics_api = JSONMetricsAPI(self)
return self._metrics_api

def logger(self, name):
Expand Down
18 changes: 10 additions & 8 deletions logging/google/cloud/logging/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,11 +184,12 @@ class _SinksAPI(object):
See:
https://cloud.google.com/logging/docs/api/reference/rest/v2/projects.sinks
:type connection: :class:`google.cloud.logging.connection.Connection`
:param connection: the connection used to make API requests.
:type client: :class:`~google.cloud.logging.client.Client`
:param client: The client used to make API requests.
"""
def __init__(self, connection):
self._connection = connection
def __init__(self, client):
self._client = client
self._connection = client.connection

def list_sinks(self, project, page_size=None, page_token=None):
"""List sinks for the project associated with this client.
Expand Down Expand Up @@ -323,11 +324,12 @@ class _MetricsAPI(object):
See:
https://cloud.google.com/logging/docs/api/reference/rest/v2/projects.metrics
:type connection: :class:`google.cloud.logging.connection.Connection`
:param connection: the connection used to make API requests.
:type client: :class:`~google.cloud.logging.client.Client`
:param client: The client used to make API requests.
"""
def __init__(self, connection):
self._connection = connection
def __init__(self, client):
self._client = client
self._connection = client.connection

def list_metrics(self, project, page_size=None, page_token=None):
"""List metrics for the project associated with this client.
Expand Down
62 changes: 32 additions & 30 deletions logging/unit_tests/test__gax.py
Original file line number Diff line number Diff line change
Expand Up @@ -606,8 +606,10 @@ def _getTargetClass(self):

def test_ctor(self):
gax_api = _GAXSinksAPI()
api = self._makeOne(gax_api)
client = object()
api = self._makeOne(gax_api, client)
self.assertIs(api._gax_api, gax_api)
self.assertIs(api._client, client)

def test_list_sinks_no_paging(self):
from google.gax import INITIAL_PAGE
Expand All @@ -625,7 +627,7 @@ def test_list_sinks_no_paging(self):
filter=self.FILTER)
response = _GAXPageIterator([sink_pb], page_token=TOKEN)
gax_api = _GAXSinksAPI(_list_sinks_response=response)
api = self._makeOne(gax_api)
api = self._makeOne(gax_api, None)

sinks, token = api.list_sinks(self.PROJECT)

Expand Down Expand Up @@ -653,7 +655,7 @@ def test_list_sinks_w_paging(self):
filter=self.FILTER)
response = _GAXPageIterator([sink_pb])
gax_api = _GAXSinksAPI(_list_sinks_response=response)
api = self._makeOne(gax_api)
api = self._makeOne(gax_api, None)

sinks, token = api.list_sinks(
self.PROJECT, page_size=PAGE_SIZE, page_token=TOKEN)
Expand All @@ -669,7 +671,7 @@ def test_list_sinks_w_paging(self):
def test_sink_create_error(self):
from google.gax.errors import GaxError
gax_api = _GAXSinksAPI(_random_gax_error=True)
api = self._makeOne(gax_api)
api = self._makeOne(gax_api, None)

with self.assertRaises(GaxError):
api.sink_create(
Expand All @@ -679,7 +681,7 @@ def test_sink_create_error(self):
def test_sink_create_conflict(self):
from google.cloud.exceptions import Conflict
gax_api = _GAXSinksAPI(_create_sink_conflict=True)
api = self._makeOne(gax_api)
api = self._makeOne(gax_api, None)

with self.assertRaises(Conflict):
api.sink_create(
Expand All @@ -689,7 +691,7 @@ def test_sink_create_conflict(self):
def test_sink_create_ok(self):
from google.logging.v2.logging_config_pb2 import LogSink
gax_api = _GAXSinksAPI()
api = self._makeOne(gax_api)
api = self._makeOne(gax_api, None)

api.sink_create(
self.PROJECT, self.SINK_NAME, self.FILTER, self.DESTINATION_URI)
Expand All @@ -706,15 +708,15 @@ def test_sink_create_ok(self):
def test_sink_get_error(self):
from google.cloud.exceptions import NotFound
gax_api = _GAXSinksAPI()
api = self._makeOne(gax_api)
api = self._makeOne(gax_api, None)

with self.assertRaises(NotFound):
api.sink_get(self.PROJECT, self.SINK_NAME)

def test_sink_get_miss(self):
from google.gax.errors import GaxError
gax_api = _GAXSinksAPI(_random_gax_error=True)
api = self._makeOne(gax_api)
api = self._makeOne(gax_api, None)

with self.assertRaises(GaxError):
api.sink_get(self.PROJECT, self.SINK_NAME)
Expand All @@ -731,7 +733,7 @@ def test_sink_get_hit(self):
destination=self.DESTINATION_URI,
filter=self.FILTER)
gax_api = _GAXSinksAPI(_get_sink_response=sink_pb)
api = self._makeOne(gax_api)
api = self._makeOne(gax_api, None)

response = api.sink_get(self.PROJECT, self.SINK_NAME)

Expand All @@ -744,7 +746,7 @@ def test_sink_get_hit(self):
def test_sink_update_error(self):
from google.gax.errors import GaxError
gax_api = _GAXSinksAPI(_random_gax_error=True)
api = self._makeOne(gax_api)
api = self._makeOne(gax_api, None)

with self.assertRaises(GaxError):
api.sink_update(
Expand All @@ -754,7 +756,7 @@ def test_sink_update_error(self):
def test_sink_update_miss(self):
from google.cloud.exceptions import NotFound
gax_api = _GAXSinksAPI()
api = self._makeOne(gax_api)
api = self._makeOne(gax_api, None)

with self.assertRaises(NotFound):
api.sink_update(
Expand All @@ -768,7 +770,7 @@ def test_sink_update_hit(self):
destination=self.DESTINATION_URI,
filter=self.FILTER)
gax_api = _GAXSinksAPI(_update_sink_response=response)
api = self._makeOne(gax_api)
api = self._makeOne(gax_api, None)

api.sink_update(
self.PROJECT, self.SINK_NAME, self.FILTER, self.DESTINATION_URI)
Expand All @@ -785,22 +787,22 @@ def test_sink_update_hit(self):
def test_sink_delete_error(self):
from google.gax.errors import GaxError
gax_api = _GAXSinksAPI(_random_gax_error=True)
api = self._makeOne(gax_api)
api = self._makeOne(gax_api, None)

with self.assertRaises(GaxError):
api.sink_delete(self.PROJECT, self.SINK_NAME)

def test_sink_delete_miss(self):
from google.cloud.exceptions import NotFound
gax_api = _GAXSinksAPI(_sink_not_found=True)
api = self._makeOne(gax_api)
api = self._makeOne(gax_api, None)

with self.assertRaises(NotFound):
api.sink_delete(self.PROJECT, self.SINK_NAME)

def test_sink_delete_hit(self):
gax_api = _GAXSinksAPI()
api = self._makeOne(gax_api)
api = self._makeOne(gax_api, None)

api.sink_delete(self.PROJECT, self.SINK_NAME)

Expand All @@ -821,7 +823,7 @@ def _getTargetClass(self):

def test_ctor(self):
gax_api = _GAXMetricsAPI()
api = self._makeOne(gax_api)
api = self._makeOne(gax_api, None)
self.assertIs(api._gax_api, gax_api)

def test_list_metrics_no_paging(self):
Expand All @@ -840,7 +842,7 @@ def test_list_metrics_no_paging(self):
filter=self.FILTER)
response = _GAXPageIterator([metric_pb], page_token=TOKEN)
gax_api = _GAXMetricsAPI(_list_log_metrics_response=response)
api = self._makeOne(gax_api)
api = self._makeOne(gax_api, None)

metrics, token = api.list_metrics(self.PROJECT)

Expand Down Expand Up @@ -868,7 +870,7 @@ def test_list_metrics_w_paging(self):
filter=self.FILTER)
response = _GAXPageIterator([metric_pb])
gax_api = _GAXMetricsAPI(_list_log_metrics_response=response)
api = self._makeOne(gax_api)
api = self._makeOne(gax_api, None)

metrics, token = api.list_metrics(
self.PROJECT, page_size=PAGE_SIZE, page_token=TOKEN)
Expand All @@ -884,7 +886,7 @@ def test_list_metrics_w_paging(self):
def test_metric_create_error(self):
from google.gax.errors import GaxError
gax_api = _GAXMetricsAPI(_random_gax_error=True)
api = self._makeOne(gax_api)
api = self._makeOne(gax_api, None)

with self.assertRaises(GaxError):
api.metric_create(
Expand All @@ -894,7 +896,7 @@ def test_metric_create_error(self):
def test_metric_create_conflict(self):
from google.cloud.exceptions import Conflict
gax_api = _GAXMetricsAPI(_create_log_metric_conflict=True)
api = self._makeOne(gax_api)
api = self._makeOne(gax_api, None)

with self.assertRaises(Conflict):
api.metric_create(
Expand All @@ -904,7 +906,7 @@ def test_metric_create_conflict(self):
def test_metric_create_ok(self):
from google.logging.v2.logging_metrics_pb2 import LogMetric
gax_api = _GAXMetricsAPI()
api = self._makeOne(gax_api)
api = self._makeOne(gax_api, None)

api.metric_create(
self.PROJECT, self.METRIC_NAME, self.FILTER, self.DESCRIPTION)
Expand All @@ -921,15 +923,15 @@ def test_metric_create_ok(self):
def test_metric_get_error(self):
from google.cloud.exceptions import NotFound
gax_api = _GAXMetricsAPI()
api = self._makeOne(gax_api)
api = self._makeOne(gax_api, None)

with self.assertRaises(NotFound):
api.metric_get(self.PROJECT, self.METRIC_NAME)

def test_metric_get_miss(self):
from google.gax.errors import GaxError
gax_api = _GAXMetricsAPI(_random_gax_error=True)
api = self._makeOne(gax_api)
api = self._makeOne(gax_api, None)

with self.assertRaises(GaxError):
api.metric_get(self.PROJECT, self.METRIC_NAME)
Expand All @@ -946,7 +948,7 @@ def test_metric_get_hit(self):
description=self.DESCRIPTION,
filter=self.FILTER)
gax_api = _GAXMetricsAPI(_get_log_metric_response=metric_pb)
api = self._makeOne(gax_api)
api = self._makeOne(gax_api, None)

response = api.metric_get(self.PROJECT, self.METRIC_NAME)

Expand All @@ -959,7 +961,7 @@ def test_metric_get_hit(self):
def test_metric_update_error(self):
from google.gax.errors import GaxError
gax_api = _GAXMetricsAPI(_random_gax_error=True)
api = self._makeOne(gax_api)
api = self._makeOne(gax_api, None)

with self.assertRaises(GaxError):
api.metric_update(
Expand All @@ -969,7 +971,7 @@ def test_metric_update_error(self):
def test_metric_update_miss(self):
from google.cloud.exceptions import NotFound
gax_api = _GAXMetricsAPI()
api = self._makeOne(gax_api)
api = self._makeOne(gax_api, None)

with self.assertRaises(NotFound):
api.metric_update(
Expand All @@ -983,7 +985,7 @@ def test_metric_update_hit(self):
description=self.DESCRIPTION,
filter=self.FILTER)
gax_api = _GAXMetricsAPI(_update_log_metric_response=response)
api = self._makeOne(gax_api)
api = self._makeOne(gax_api, None)

api.metric_update(
self.PROJECT, self.METRIC_NAME, self.FILTER, self.DESCRIPTION)
Expand All @@ -1000,22 +1002,22 @@ def test_metric_update_hit(self):
def test_metric_delete_error(self):
from google.gax.errors import GaxError
gax_api = _GAXMetricsAPI(_random_gax_error=True)
api = self._makeOne(gax_api)
api = self._makeOne(gax_api, None)

with self.assertRaises(GaxError):
api.metric_delete(self.PROJECT, self.METRIC_NAME)

def test_metric_delete_miss(self):
from google.cloud.exceptions import NotFound
gax_api = _GAXMetricsAPI(_log_metric_not_found=True)
api = self._makeOne(gax_api)
api = self._makeOne(gax_api, None)

with self.assertRaises(NotFound):
api.metric_delete(self.PROJECT, self.METRIC_NAME)

def test_metric_delete_hit(self):
gax_api = _GAXMetricsAPI()
api = self._makeOne(gax_api)
api = self._makeOne(gax_api, None)

api.metric_delete(self.PROJECT, self.METRIC_NAME)

Expand Down
8 changes: 6 additions & 2 deletions logging/unit_tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,9 @@ def _generated_api(*args, **kw):

class _GaxSinksAPI(object):

def __init__(self, _wrapped):
def __init__(self, _wrapped, client):
self._wrapped = _wrapped
self.client = client

creds = _Credentials()
client = self._makeOne(project=self.PROJECT, credentials=creds)
Expand All @@ -143,6 +144,7 @@ def __init__(self, _wrapped):

self.assertIsInstance(api, _GaxSinksAPI)
self.assertIs(api._wrapped, wrapped)
self.assertIs(api.client, client)
# API instance is cached
again = client.sinks_api
self.assertIs(again, api)
Expand Down Expand Up @@ -176,8 +178,9 @@ def _generated_api(*args, **kw):

class _GaxMetricsAPI(object):

def __init__(self, _wrapped):
def __init__(self, _wrapped, client):
self._wrapped = _wrapped
self.client = client

creds = _Credentials()
client = self._makeOne(project=self.PROJECT, credentials=creds)
Expand All @@ -190,6 +193,7 @@ def __init__(self, _wrapped):

self.assertIsInstance(api, _GaxMetricsAPI)
self.assertIs(api._wrapped, wrapped)
self.assertIs(api.client, client)
# API instance is cached
again = client.metrics_api
self.assertIs(again, api)
Expand Down
Loading

0 comments on commit e25245b

Please sign in to comment.