Skip to content

Commit

Permalink
Merge pull request #82 from xsnippet/v1-routes
Browse files Browse the repository at this point in the history
Version API via URL instead of HTTP header
  • Loading branch information
malor committed Apr 22, 2018
2 parents 8306dec + 205183b commit 7f672c1
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 200 deletions.
135 changes: 68 additions & 67 deletions tests/resources/test_snippets.py

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions tests/resources/test_syntaxes.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

async def test_get_syntaxes_default_conf(testapp):
resp = await testapp.get(
'/syntaxes',
'/v1/syntaxes',
headers={
'Accept': 'application/json',
}
Expand All @@ -30,7 +30,7 @@ async def test_get_syntaxes_overriden_conf(testapp, testconf, syntaxes, expected
testconf['snippet']['syntaxes'] = syntaxes

resp = await testapp.get(
'/syntaxes',
'/v1/syntaxes',
headers={
'Accept': 'application/json',
}
Expand All @@ -43,7 +43,7 @@ async def test_get_syntaxes_overriden_conf_no_syntaxes(testapp, testconf):
testconf['snippet'].pop('syntaxes', None)

resp = await testapp.get(
'/syntaxes',
'/v1/syntaxes',
headers={
'Accept': 'application/json',
}
Expand All @@ -57,7 +57,7 @@ async def test_get_syntaxes_unsupported_method(testapp, method):
func = getattr(testapp, method)

resp = await func(
'/syntaxes',
'/v1/syntaxes',
headers={
'Accept': 'application/json',
}
Expand All @@ -68,7 +68,7 @@ async def test_get_syntaxes_unsupported_method(testapp, method):

async def test_get_syntaxes_unsupported_accept_type(testapp):
resp = await testapp.get(
'/syntaxes',
'/v1/syntaxes',
headers={
'Accept': 'application/xml',
}
Expand Down
15 changes: 15 additions & 0 deletions tests/test_application.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

import pytest

from xsnippet.api.application import create_app


@pytest.mark.parametrize('name, value', [
('Accept', 'application/json'),
Expand All @@ -27,3 +29,16 @@ async def test_http_vary_header(name, value, testapp):

assert name in parts
await resp.release()


async def test_deprecated_routes(testapp):
app = create_app()

def _extract_path(route):
info = route.resource.get_info()
return info.get('path') or info.get('formatter')
routes = {_extract_path(route): route.handler for route in app.router.routes()}

assert routes['/snippets'] is routes['/v1/snippets']
assert routes['/snippets/{id}'] is routes['/v1/snippets/{id}']
assert routes['/syntaxes'] is routes['/v1/syntaxes']
76 changes: 0 additions & 76 deletions tests/test_router.py

This file was deleted.

11 changes: 3 additions & 8 deletions xsnippet/api/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import aiohttp.web
import picobox

from . import database, router, routes, middlewares
from . import database, routes, middlewares


async def _inject_vary_header(request, response):
Expand Down Expand Up @@ -58,16 +58,11 @@ def create_app(conf, db):
:rtype: :class:`aiohttp.web.Application`
"""

v1 = aiohttp.web.UrlDispatcher()
v1.add_routes(routes.v1)

# We need to import all the resources in order to evaluate @endpoint
# decorator, so they can be collected and passed to VersionRouter.
app = aiohttp.web.Application(
middlewares=[
middlewares.auth.auth(conf['auth']),
],
router=router.VersionRouter({'1.0': v1}, default='1.0'))
])
app.router.add_routes(routes.v1)
app.on_startup.append(functools.partial(database.setup, db=db))

# We need to respond with Vary header time to time in order to avoid
Expand Down
44 changes: 0 additions & 44 deletions xsnippet/api/router.py

This file was deleted.

8 changes: 8 additions & 0 deletions xsnippet/api/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@


v1 = [
web.route('*', '/v1/snippets', resources.Snippets),
web.route('*', '/v1/snippets/{id}', resources.Snippet),
web.route('*', '/v1/syntaxes', resources.Syntaxes),

# These routes are what we had before during era of API versioning through
# HTTP header. Nowadays we prefer versioning through HTTP URI, but we want
# to be good guys and provide these routes for a while and avoid breaking
# the world.
web.route('*', '/snippets', resources.Snippets),
web.route('*', '/snippets/{id}', resources.Snippet),
web.route('*', '/syntaxes', resources.Syntaxes),
Expand Down

0 comments on commit 7f672c1

Please sign in to comment.