Skip to content

Commit 57e8424

Browse files
committed
Add logging connection and client.
1 parent f2becb0 commit 57e8424

5 files changed

Lines changed: 264 additions & 0 deletions

File tree

gcloud/logging/__init__.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Copyright 2015 Google Inc. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"""Google Cloud Logging API wrapper.
16+
"""
17+
18+
from gcloud.logging.client import Client
19+
from gcloud.logging.connection import Connection
20+
21+
22+
SCOPE = Connection.SCOPE

gcloud/logging/client.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Copyright 2015 Google Inc. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"""Client for interacting with the Google Cloud Pub/Sub API."""
16+
17+
18+
from gcloud.client import JSONClient
19+
from gcloud.logging.connection import Connection
20+
21+
22+
class Client(JSONClient):
23+
"""Client to bundle configuration needed for API requests.
24+
25+
:type project: string
26+
:param project: the project which the client acts on behalf of. Will be
27+
passed when creating a topic. If not passed,
28+
falls back to the default inferred from the environment.
29+
30+
:type credentials: :class:`oauth2client.client.OAuth2Credentials` or
31+
:class:`NoneType`
32+
:param credentials: The OAuth2 Credentials to use for the connection
33+
owned by this client. If not passed (and if no ``http``
34+
object is passed), falls back to the default inferred
35+
from the environment.
36+
37+
:type http: :class:`httplib2.Http` or class that defines ``request()``.
38+
:param http: An optional HTTP object to make requests. If not passed, an
39+
``http`` object is created that is bound to the
40+
``credentials`` for the current object.
41+
"""
42+
43+
_connection_class = Connection

gcloud/logging/connection.py

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# Copyright 2015 Google Inc. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"""Create / interact with gcloud logging connections."""
16+
17+
from gcloud import connection as base_connection
18+
19+
20+
class Connection(base_connection.JSONConnection):
21+
"""A connection to Google Cloud Pubsub via the JSON REST API.
22+
23+
:type credentials: :class:`oauth2client.client.OAuth2Credentials`
24+
:param credentials: (Optional) The OAuth2 Credentials to use for this
25+
connection.
26+
27+
:type http: :class:`httplib2.Http` or class that defines ``request()``.
28+
:param http: (Optional) HTTP object to make requests.
29+
30+
:type api_base_url: string
31+
:param api_base_url: The base of the API call URL. Defaults to the value
32+
:attr:`Connection.API_BASE_URL`.
33+
"""
34+
35+
API_BASE_URL = 'https://logging.googleapis.com'
36+
"""The base of the API call URL."""
37+
38+
API_VERSION = 'v2beta1'
39+
"""The version of the API, used in building the API call's URL."""
40+
41+
API_URL_TEMPLATE = '{api_base_url}/{api_version}{path}'
42+
"""A template for the URL of a particular API call."""
43+
44+
SCOPE = ('https://www.googleapis.com/auth/logging.read',
45+
'https://www.googleapis.com/auth/logging.write',
46+
'https://www.googleapis.com/auth/logging.admin',
47+
'https://www.googleapis.com/auth/cloud-platform')
48+
"""The scopes required for authenticating as a Cloud Pub/Sub consumer."""
49+
50+
def __init__(self, credentials=None, http=None, api_base_url=None):
51+
super(Connection, self).__init__(credentials=credentials, http=http)
52+
if api_base_url is None:
53+
api_base_url = self.__class__.API_BASE_URL
54+
self.api_base_url = api_base_url
55+
56+
def build_api_url(self, path, query_params=None,
57+
api_base_url=None, api_version=None):
58+
"""Construct an API url given a few components, some optional.
59+
60+
Typically, you shouldn't need to use this method.
61+
62+
:type path: string
63+
:param path: The path to the resource.
64+
65+
:type query_params: dict
66+
:param query_params: A dictionary of keys and values to insert into
67+
the query string of the URL.
68+
69+
:type api_base_url: string
70+
:param api_base_url: The base URL for the API endpoint.
71+
Typically you won't have to provide this.
72+
73+
:type api_version: string
74+
:param api_version: The version of the API to call.
75+
Typically you shouldn't provide this and instead
76+
use the default for the library.
77+
78+
:rtype: string
79+
:returns: The URL assembled from the pieces provided.
80+
"""
81+
if api_base_url is None:
82+
api_base_url = self.api_base_url
83+
return super(Connection, self.__class__).build_api_url(
84+
path, query_params=query_params,
85+
api_base_url=api_base_url, api_version=api_version)

gcloud/logging/test_client.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Copyright 2015 Google Inc. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import unittest2
16+
17+
18+
class TestClient(unittest2.TestCase):
19+
20+
def _getTargetClass(self):
21+
from gcloud.logging.client import Client
22+
return Client
23+
24+
def _makeOne(self, *args, **kw):
25+
return self._getTargetClass()(*args, **kw)
26+
27+
def test_ctor(self):
28+
PROJECT = 'PROJECT'
29+
CREDS = _Credentials()
30+
CLIENT_OBJ = self._makeOne(project=PROJECT, credentials=CREDS)
31+
32+
33+
class _Credentials(object):
34+
35+
_scopes = None
36+
37+
@staticmethod
38+
def create_scoped_required():
39+
return True
40+
41+
def create_scoped(self, scope):
42+
self._scopes = scope
43+
return self

gcloud/logging/test_connection.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# Copyright 2015 Google Inc. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import unittest2
16+
17+
18+
class TestConnection(unittest2.TestCase):
19+
20+
def _getTargetClass(self):
21+
from gcloud.logging.connection import Connection
22+
return Connection
23+
24+
def _makeOne(self, *args, **kw):
25+
return self._getTargetClass()(*args, **kw)
26+
27+
def test_default_url(self):
28+
conn = self._makeOne()
29+
klass = self._getTargetClass()
30+
self.assertEqual(conn.api_base_url, klass.API_BASE_URL)
31+
32+
def test_custom_url_from_constructor(self):
33+
HOST = object()
34+
conn = self._makeOne(api_base_url=HOST)
35+
36+
klass = self._getTargetClass()
37+
self.assertNotEqual(conn.api_base_url, klass.API_BASE_URL)
38+
self.assertEqual(conn.api_base_url, HOST)
39+
40+
def test_build_api_url_no_extra_query_params(self):
41+
conn = self._makeOne()
42+
URI = '/'.join([
43+
conn.API_BASE_URL,
44+
conn.API_VERSION,
45+
'foo',
46+
])
47+
self.assertEqual(conn.build_api_url('/foo'), URI)
48+
49+
def test_build_api_url_w_extra_query_params(self):
50+
from six.moves.urllib.parse import parse_qsl
51+
from six.moves.urllib.parse import urlsplit
52+
conn = self._makeOne()
53+
uri = conn.build_api_url('/foo', {'bar': 'baz'})
54+
scheme, netloc, path, qs, _ = urlsplit(uri)
55+
self.assertEqual('%s://%s' % (scheme, netloc), conn.API_BASE_URL)
56+
self.assertEqual(path,
57+
'/'.join(['', conn.API_VERSION, 'foo']))
58+
parms = dict(parse_qsl(qs))
59+
self.assertEqual(parms['bar'], 'baz')
60+
61+
def test_build_api_url_w_base_url_override(self):
62+
base_url1 = 'api-base-url1'
63+
base_url2 = 'api-base-url2'
64+
conn = self._makeOne(api_base_url=base_url1)
65+
URI = '/'.join([
66+
base_url2,
67+
conn.API_VERSION,
68+
'foo',
69+
])
70+
self.assertEqual(
71+
conn.build_api_url('/foo', api_base_url=base_url2), URI)

0 commit comments

Comments
 (0)