From 1a76a7792f21f10eb063f8f58cc303d6bd6df881 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20HUBSCHER?= Date: Mon, 7 Aug 2017 12:29:00 +0200 Subject: [PATCH] Expose /v1/__api__ endpoint with the OAS spec. Fixes #20 --- MANIFEST.in | 1 + api.yaml => pollbot/api.yaml | 0 pollbot/app.py | 3 ++- pollbot/views/utilities.py | 12 ++++++++++++ tests/test_oas.py | 2 +- tests/test_views.py | 12 ++++++++++++ 6 files changed, 28 insertions(+), 2 deletions(-) rename api.yaml => pollbot/api.yaml (100%) create mode 100644 pollbot/views/utilities.py diff --git a/MANIFEST.in b/MANIFEST.in index 7b23013..079cbc7 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -1,2 +1,3 @@ include *.rst LICENSE Makefile tox.ini include dev-requirements.txt +include pollbot/api.yaml diff --git a/api.yaml b/pollbot/api.yaml similarity index 100% rename from api.yaml rename to pollbot/api.yaml diff --git a/pollbot/app.py b/pollbot/app.py index 1b9d9df..763b21a 100644 --- a/pollbot/app.py +++ b/pollbot/app.py @@ -1,10 +1,11 @@ from aiohttp import web -from .views import home, release +from .views import home, release, utilities def get_app(loop=None): app = web.Application(loop=loop) app.router.add_get('/', home.redirect) app.router.add_get('/v1/', home.index) + app.router.add_get('/v1/__api__', utilities.oas_spec) app.router.add_get('/v1/firefox/{version}', release.info) return app diff --git a/pollbot/views/utilities.py b/pollbot/views/utilities.py new file mode 100644 index 0000000..dcfe0ac --- /dev/null +++ b/pollbot/views/utilities.py @@ -0,0 +1,12 @@ +import os.path + +from aiohttp import web +import ruamel.yaml as yaml + +HERE = os.path.dirname(__file__) + + +async def oas_spec(request): + with open(os.path.join(HERE, "..", "api.yaml")) as stream: + oas_spec = yaml.load(stream) + return web.json_response(oas_spec) diff --git a/tests/test_oas.py b/tests/test_oas.py index 5bac44d..b9afd92 100644 --- a/tests/test_oas.py +++ b/tests/test_oas.py @@ -6,7 +6,7 @@ def test_oas_spec(): - with open(os.path.join(HERE, "..", "api.yaml"), 'r') as stream: + with open(os.path.join(HERE, "..", "pollbot", "api.yaml"), 'r') as stream: oas_spec = yaml.load(stream) # example for swagger spec v2.0 validate_spec(oas_spec) diff --git a/tests/test_views.py b/tests/test_views.py index 1e82b54..3e37356 100644 --- a/tests/test_views.py +++ b/tests/test_views.py @@ -1,7 +1,12 @@ +import os.path import pytest +import ruamel.yaml as yaml + from pollbot import __version__ as pollbot_version, HTTP_API_VERSION from pollbot.app import get_app +HERE = os.path.dirname(__file__) + @pytest.fixture def cli(loop, test_client): @@ -13,6 +18,13 @@ async def test_home_redirects_to_v1(cli): assert resp.status == 302 +async def test_oas_spec(cli): + with open(os.path.join(HERE, "..", "pollbot", "api.yaml"), 'r') as stream: + oas_spec = yaml.load(stream) + resp = await cli.get("/v1/__api__") + assert await resp.json() == oas_spec + + async def test_home_body(cli): resp = await cli.get("/v1/") assert resp.status == 200