Skip to content

Commit 8d36b33

Browse files
committed
Merge pull request #1164 from tseaver/search-client
Add search connection / client
2 parents 04034b5 + 8d86dbf commit 8d36b33

File tree

5 files changed

+191
-0
lines changed

5 files changed

+191
-0
lines changed

gcloud/search/__init__.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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 Search API wrapper."""
16+
17+
from gcloud.search.client import Client
18+
from gcloud.search.connection import Connection
19+
20+
21+
SCOPE = Connection.SCOPE

gcloud/search/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 search API."""
16+
17+
18+
from gcloud.client import JSONClient
19+
from gcloud.search.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 zone. 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/search/connection.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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 search connections."""
16+
17+
from gcloud import connection as base_connection
18+
19+
20+
class Connection(base_connection.JSONConnection):
21+
"""A connection to Google Cloud Search via the JSON REST API."""
22+
23+
API_BASE_URL = 'https://cloudsearch.googleapis.com'
24+
"""The base of the API call URL."""
25+
26+
API_VERSION = 'v1'
27+
"""The version of the API, used in building the API call's URL."""
28+
29+
API_URL_TEMPLATE = '{api_base_url}/{api_version}{path}'
30+
"""A template for the URL of a particular API call."""
31+
32+
SCOPE = ('https://www.googleapis.com/auth/ndev.cloudsearch',)
33+
"""The scopes required for authenticating as a Cloud Search consumer."""

gcloud/search/test_client.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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.search.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+
from gcloud.search.connection import Connection
29+
PROJECT = 'PROJECT'
30+
creds = _Credentials()
31+
http = object()
32+
client = self._makeOne(project=PROJECT, credentials=creds, http=http)
33+
self.assertTrue(isinstance(client.connection, Connection))
34+
self.assertTrue(client.connection.credentials is creds)
35+
self.assertTrue(client.connection.http is http)
36+
37+
38+
class _Credentials(object):
39+
40+
_scopes = None
41+
42+
@staticmethod
43+
def create_scoped_required():
44+
return True
45+
46+
def create_scoped(self, scope):
47+
self._scopes = scope
48+
return self

gcloud/search/test_connection.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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.search.connection import Connection
22+
return Connection
23+
24+
def _makeOne(self, *args, **kw):
25+
return self._getTargetClass()(*args, **kw)
26+
27+
def test_build_api_url_no_extra_query_params(self):
28+
conn = self._makeOne()
29+
URI = '/'.join([
30+
conn.API_BASE_URL,
31+
conn.API_VERSION,
32+
'foo',
33+
])
34+
self.assertEqual(conn.build_api_url('/foo'), URI)
35+
36+
def test_build_api_url_w_extra_query_params(self):
37+
from six.moves.urllib.parse import parse_qsl
38+
from six.moves.urllib.parse import urlsplit
39+
conn = self._makeOne()
40+
uri = conn.build_api_url('/foo', {'bar': 'baz'})
41+
scheme, netloc, path, qs, _ = urlsplit(uri)
42+
self.assertEqual('%s://%s' % (scheme, netloc), conn.API_BASE_URL)
43+
self.assertEqual(path,
44+
'/'.join(['', conn.API_VERSION, 'foo']))
45+
parms = dict(parse_qsl(qs))
46+
self.assertEqual(parms['bar'], 'baz')

0 commit comments

Comments
 (0)