Skip to content

Commit 88daa08

Browse files
committed
Merge pull request #1820 from tseaver/logging-grpc_prep
Factor API-related calls into service-specific helpers.
2 parents 503cd32 + b0ae6a4 commit 88daa08

File tree

10 files changed

+1653
-848
lines changed

10 files changed

+1653
-848
lines changed

gcloud/logging/client.py

Lines changed: 44 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@
1414

1515
"""Client for interacting with the Google Cloud Logging API."""
1616

17-
1817
from gcloud.client import JSONClient
1918
from gcloud.logging.connection import Connection
19+
from gcloud.logging.connection import _LoggingAPI
20+
from gcloud.logging.connection import _MetricsAPI
21+
from gcloud.logging.connection import _SinksAPI
2022
from gcloud.logging.entries import ProtobufEntry
2123
from gcloud.logging.entries import StructEntry
2224
from gcloud.logging.entries import TextEntry
@@ -47,6 +49,41 @@ class Client(JSONClient):
4749
"""
4850

4951
_connection_class = Connection
52+
_logging_api = _sinks_api = _metrics_api = None
53+
54+
@property
55+
def logging_api(self):
56+
"""Helper for logging-related API calls.
57+
58+
See:
59+
https://cloud.google.com/logging/docs/api/ref_v2beta1/rest/v2beta1/entries
60+
https://cloud.google.com/logging/docs/api/ref_v2beta1/rest/v2beta1/projects.logs
61+
"""
62+
if self._logging_api is None:
63+
self._logging_api = _LoggingAPI(self.connection)
64+
return self._logging_api
65+
66+
@property
67+
def sinks_api(self):
68+
"""Helper for log sink-related API calls.
69+
70+
See:
71+
https://cloud.google.com/logging/docs/api/ref_v2beta1/rest/v2beta1/projects.sinks
72+
"""
73+
if self._sinks_api is None:
74+
self._sinks_api = _SinksAPI(self.connection)
75+
return self._sinks_api
76+
77+
@property
78+
def metrics_api(self):
79+
"""Helper for log metric-related API calls.
80+
81+
See:
82+
https://cloud.google.com/logging/docs/api/ref_v2beta1/rest/v2beta1/projects.metrics
83+
"""
84+
if self._metrics_api is None:
85+
self._metrics_api = _MetricsAPI(self.connection)
86+
return self._metrics_api
5087

5188
def logger(self, name):
5289
"""Creates a logger bound to the current client.
@@ -120,22 +157,9 @@ def list_entries(self, projects=None, filter_=None, order_by=None,
120157
if projects is None:
121158
projects = [self.project]
122159

123-
params = {'projectIds': projects}
124-
125-
if filter_ is not None:
126-
params['filter'] = filter_
127-
128-
if order_by is not None:
129-
params['orderBy'] = order_by
130-
131-
if page_size is not None:
132-
params['pageSize'] = page_size
133-
134-
if page_token is not None:
135-
params['pageToken'] = page_token
136-
137-
resp = self.connection.api_request(method='POST', path='/entries:list',
138-
data=params)
160+
resp = self.logging_api.list_entries(
161+
projects=projects, filter_=filter_, order_by=order_by,
162+
page_size=page_size, page_token=page_token)
139163
loggers = {}
140164
entries = [self._entry_from_resource(resource, loggers)
141165
for resource in resp.get('entries', ())]
@@ -181,17 +205,7 @@ def list_sinks(self, page_size=None, page_token=None):
181205
more sinks can be retrieved with another call (pass that
182206
value as ``page_token``).
183207
"""
184-
params = {}
185-
186-
if page_size is not None:
187-
params['pageSize'] = page_size
188-
189-
if page_token is not None:
190-
params['pageToken'] = page_token
191-
192-
path = '/projects/%s/sinks' % (self.project,)
193-
resp = self.connection.api_request(method='GET', path=path,
194-
query_params=params)
208+
resp = self.sinks_api.list_sinks(self.project, page_size, page_token)
195209
sinks = [Sink.from_api_repr(resource, self)
196210
for resource in resp.get('sinks', ())]
197211
return sinks, resp.get('nextPageToken')
@@ -235,17 +249,8 @@ def list_metrics(self, page_size=None, page_token=None):
235249
more metrics can be retrieved with another call (pass that
236250
value as ``page_token``).
237251
"""
238-
params = {}
239-
240-
if page_size is not None:
241-
params['pageSize'] = page_size
242-
243-
if page_token is not None:
244-
params['pageToken'] = page_token
245-
246-
path = '/projects/%s/metrics' % (self.project,)
247-
resp = self.connection.api_request(method='GET', path=path,
248-
query_params=params)
252+
resp = self.metrics_api.list_metrics(
253+
self.project, page_size, page_token)
249254
metrics = [Metric.from_api_repr(resource, self)
250255
for resource in resp.get('metrics', ())]
251256
return metrics, resp.get('nextPageToken')

0 commit comments

Comments
 (0)