From b766aca05ef41e35c531a467f73d15b625324709 Mon Sep 17 00:00:00 2001 From: Jamie Lennox Date: Tue, 4 Feb 2014 13:11:43 +1000 Subject: [PATCH] Add version routes to KDS Add the first real endpoints to the KDS server starting with the version discovery endpoints. Change-Id: If359a683e1c9614855d7c20e9f8009c2198a05f7 --- kite/api/root.py | 11 +++++++++- kite/api/v1/__init__.py | 0 kite/api/v1/controllers/__init__.py | 18 +++++++++++++++++ kite/api/v1/controllers/controller.py | 29 +++++++++++++++++++++++++++ kite/tests/api/test.py | 14 ++++++++++--- kite/tests/api/v1/__init__.py | 0 kite/tests/api/v1/base.py | 29 +++++++++++++++++++++++++++ kite/tests/api/v1/test.py | 28 ++++++++++++++++++++++++++ 8 files changed, 125 insertions(+), 4 deletions(-) create mode 100644 kite/api/v1/__init__.py create mode 100644 kite/api/v1/controllers/__init__.py create mode 100644 kite/api/v1/controllers/controller.py create mode 100644 kite/tests/api/v1/__init__.py create mode 100644 kite/tests/api/v1/base.py create mode 100644 kite/tests/api/v1/test.py diff --git a/kite/api/root.py b/kite/api/root.py index 6ede1e3..be3e092 100644 --- a/kite/api/root.py +++ b/kite/api/root.py @@ -12,9 +12,18 @@ import pecan +from kite.api.v1 import controllers + class RootController(object): + v1 = controllers.Controller() + @pecan.expose('json') def index(self): - return {'hello': 'world'} + pecan.response.status = 300 + return { + 'versions': [ + self.v1.version_info(), + ] + } diff --git a/kite/api/v1/__init__.py b/kite/api/v1/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/kite/api/v1/controllers/__init__.py b/kite/api/v1/controllers/__init__.py new file mode 100644 index 0000000..4087af0 --- /dev/null +++ b/kite/api/v1/controllers/__init__.py @@ -0,0 +1,18 @@ +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + + +from kite.api.v1.controllers import controller + +Controller = controller.Controller + +__all__ = ['Controller'] diff --git a/kite/api/v1/controllers/controller.py b/kite/api/v1/controllers/controller.py new file mode 100644 index 0000000..cee4c47 --- /dev/null +++ b/kite/api/v1/controllers/controller.py @@ -0,0 +1,29 @@ +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +import pecan + + +class Controller(object): + """Version 1 API controller root.""" + + @staticmethod + def version_info(): + return {'status': 'stable', + 'id': 'v1.0', + 'links': [{ + 'href': '%s/v1/' % pecan.request.host_url, + 'rel': 'self'}]} + + @pecan.expose('json') + def index(self): + return {'version': self.version_info()} diff --git a/kite/tests/api/test.py b/kite/tests/api/test.py index fa74d3c..790a6c9 100644 --- a/kite/tests/api/test.py +++ b/kite/tests/api/test.py @@ -15,6 +15,14 @@ from kite.tests.api import base class SimpleTest(base.BaseTestCase): - def test_simple(self): - resp = self.get("/") - self.assertEqual(resp.json['hello'], 'world') + def test_version(self): + resp = self.get('/') + versions = resp.json['versions'] + self.assertEqual(resp.status_code, 300) + + host = 'http://localhost' # webtest default + + self.assertEqual(versions[0]['status'], 'stable') + self.assertEqual(versions[0]['id'], 'v1.0') + self.assertEqual(versions[0]['links'][0]['href'], '%s/v1/' % host) + self.assertEqual(versions[0]['links'][0]['rel'], 'self') diff --git a/kite/tests/api/v1/__init__.py b/kite/tests/api/v1/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/kite/tests/api/v1/base.py b/kite/tests/api/v1/base.py new file mode 100644 index 0000000..bbf5492 --- /dev/null +++ b/kite/tests/api/v1/base.py @@ -0,0 +1,29 @@ +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +from kite.tests.api import base + + +def v1_url(*args): + return base.urljoin('v1', *args) + + +class BaseTestCase(base.BaseTestCase): + + def get(self, url, *args, **kwargs): + return super(BaseTestCase, self).get(v1_url(url), *args, **kwargs) + + def post(self, url, *args, **kwargs): + return super(BaseTestCase, self).post(v1_url(url), *args, **kwargs) + + def put(self, url, *args, **kwargs): + return super(BaseTestCase, self).put(v1_url(url), *args, **kwargs) diff --git a/kite/tests/api/v1/test.py b/kite/tests/api/v1/test.py new file mode 100644 index 0000000..007fb4e --- /dev/null +++ b/kite/tests/api/v1/test.py @@ -0,0 +1,28 @@ +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +from kite.tests.api.v1 import base + + +class TestVersion(base.BaseTestCase): + + def test_versions(self): + resp = self.get('/') + version = resp.json['version'] + self.assertEqual(resp.status_code, 200) + + host = 'http://localhost' # webtest default + + self.assertEqual(version['id'], 'v1.0') + self.assertEqual(version['status'], 'stable') + self.assertEqual(version['links'][0]['href'], '%s/v1/' % host) + self.assertEqual(version['links'][0]['rel'], 'self')