Return route information on insert & append

This commit is contained in:
Roberto Abdelkader Martínez Pérez
2019-08-21 12:19:48 +02:00
parent 9ae748e5c1
commit c96584efcc
4 changed files with 41 additions and 16 deletions
+29 -6
View File
@@ -270,12 +270,14 @@ def handle_route(entrypoint, command):
# Route Management # # Route Management #
######################################################################## ########################################################################
def get_routes(app): def get_routes(app):
async def _get_routes(request): async def _get_routes(request):
"""Return the list of registered routes.""" """Return the list of registered routes."""
return web.json_response(list(app.router)) return web.json_response(list(app.router))
return _get_routes return _get_routes
def insert_route(app): def insert_route(app):
async def _insert_route(request): async def _insert_route(request):
"""Insert a new Kapow! route.""" """Insert a new Kapow! route."""
@@ -289,7 +291,9 @@ def insert_route(app):
assert index >= 0 assert index >= 0
route = KapowRoute(method=content["method"], route = KapowRoute(method=content["method"],
path=content["url_pattern"], path=content["url_pattern"],
name="ROUTE_" + str(uuid4()).replace('-', '_'), id="ROUTE_" + str(uuid4()).replace('-', '_'),
entrypoint=content["entrypoint"],
command=content["command"],
handler=handle_route(content["entrypoint"], handler=handle_route(content["entrypoint"],
content["command"])) content["command"]))
app.change_routes((app["user_routes"][:index] app.change_routes((app["user_routes"][:index]
@@ -300,9 +304,15 @@ def insert_route(app):
else: else:
app["user_routes"].insert(index, route) app["user_routes"].insert(index, route)
print(f'Route created {content["method"]} {content["url_pattern"]}') print(f'Route created {content["method"]} {content["url_pattern"]}')
return web.json_response(route.name, status=201) return web.json_response({"id": route.id,
"method": route.method,
"url_pattern": route.path,
"entrypoint": route.entrypoint,
"command": route.command,
"index": index}, status=201)
return _insert_route return _insert_route
def append_route(app): def append_route(app):
async def _append_route(request): async def _append_route(request):
"""Append a new Kapow! route.""" """Append a new Kapow! route."""
@@ -314,7 +324,9 @@ def append_route(app):
try: try:
route = KapowRoute(method=content["method"], route = KapowRoute(method=content["method"],
path=content["url_pattern"], path=content["url_pattern"],
name="ROUTE_" + str(uuid4()).replace('-', '_'), id="ROUTE_" + str(uuid4()).replace('-', '_'),
entrypoint=content["entrypoint"],
command=content["command"],
handler=handle_route(content["entrypoint"], handler=handle_route(content["entrypoint"],
content["command"])) content["command"]))
app.change_routes(app["user_routes"] + [route]) app.change_routes(app["user_routes"] + [route])
@@ -323,7 +335,12 @@ def append_route(app):
else: else:
app["user_routes"].append(route) app["user_routes"].append(route)
print(f'Route created {content["method"]} {content["url_pattern"]}') print(f'Route created {content["method"]} {content["url_pattern"]}')
return web.json_response(route.name, status=201) return web.json_response({"id": route.id,
"method": route.method,
"url_pattern": route.path,
"entrypoint": route.entrypoint,
"command": route.command,
"index": len(app["user_routes"])-1}, status=201)
return _append_route return _append_route
@@ -390,7 +407,7 @@ class DynamicApplication(web.Application):
router.add_route(route.method, router.add_route(route.method,
route.path, route.path,
route.handler, route.handler,
name=route.name) name=route.id)
except Exception as exc: except Exception as exc:
raise InvalidRouteError("Invalid route") from exc raise InvalidRouteError("Invalid route") from exc
else: else:
@@ -399,7 +416,13 @@ class DynamicApplication(web.Application):
self._router.freeze() self._router.freeze()
KapowRoute = namedtuple('KapowRoute', ('method', 'path', 'name', 'handler')) KapowRoute = namedtuple('KapowRoute',
('method',
'path',
'id',
'entrypoint',
'command',
'handler'))
async def start_background_tasks(app): async def start_background_tasks(app):
@@ -1,3 +1,4 @@
@wip
Feature: Append new routes in Kapow! server. Feature: Append new routes in Kapow! server.
Append routes allow users to configure the server. New Append routes allow users to configure the server. New
routes are added to the list of existing routes. routes are added to the list of existing routes.
+10 -6
View File
@@ -4,7 +4,7 @@ from functools import singledispatch
def assert_same_type(f): def assert_same_type(f):
def wrapper(a, b): def wrapper(a, b):
if type(a) != type(b): if type(a) != type(b):
raise TypeError("Non-matching types") raise TypeError(f"Non-matching types {a!r} != {b!r}")
return f(a, b) return f(a, b)
return wrapper return wrapper
@@ -12,7 +12,10 @@ def assert_same_type(f):
@singledispatch @singledispatch
@assert_same_type @assert_same_type
def is_subset(model, obj): def is_subset(model, obj):
return model == obj if model == obj:
return True
else:
raise ValueError(f"Non-matching values {model!r} != {obj!r}")
@is_subset.register(dict) @is_subset.register(dict)
@@ -20,16 +23,17 @@ def is_subset(model, obj):
def _(model, obj): def _(model, obj):
for key, value in model.items(): for key, value in model.items():
if key not in obj or not is_subset(value, obj[key]): if key not in obj or not is_subset(value, obj[key]):
return False raise ValueError(f"Non-matching dicts {model!r} != {obj!r}")
return True return True
@is_subset.register(list) @is_subset.register(list)
@assert_same_type @assert_same_type
def _(model, obj): def _(model, obj):
if type(model) != type(obj): if is_subset(set(model), set(obj)):
raise TypeError("Non-matching types") return True
return is_subset(set(model), set(obj)) else:
raise ValueError(f"Non-matching lists {model!r} != {obj!r}")
@is_subset.register(set) @is_subset.register(set)
+1 -4
View File
@@ -134,10 +134,7 @@ def step_impl(context, reason):
@then('I get the following response body') @then('I get the following response body')
def step_impl(context): def step_impl(context):
for row in context.table: assert is_subset(json.loads(context.text), context.response.json())
for name, value in row.items():
assert name in context.response.json(), f"Field {name} not present in {context.response.json()}"
assert set(json.loads(value)) == set(context.response.json()[name])
@then('I get an empty response body') @then('I get an empty response body')