Fixes route overloading for dynamic routes
Addresses #353, now dynamic routes work alongside our newly minted overloaded routes! Also fixed an unintended side effect where methods were still being passed in as None for `Sanic.add_route`.
This commit is contained in:
parent
3d790a71a0
commit
41c52487ee
|
@ -210,29 +210,40 @@ class Router:
|
||||||
url = host + url
|
url = host + url
|
||||||
# Check against known static routes
|
# Check against known static routes
|
||||||
route = self.routes_static.get(url)
|
route = self.routes_static.get(url)
|
||||||
|
method_not_supported = InvalidUsage(
|
||||||
|
'Method {} not allowed for URL {}'.format(
|
||||||
|
method, url), status_code=405)
|
||||||
if route:
|
if route:
|
||||||
|
if route.methods and method not in route.methods:
|
||||||
|
raise method_not_supported
|
||||||
match = route.pattern.match(url)
|
match = route.pattern.match(url)
|
||||||
else:
|
else:
|
||||||
|
route_found = False
|
||||||
# Move on to testing all regex routes
|
# Move on to testing all regex routes
|
||||||
for route in self.routes_dynamic[url_hash(url)]:
|
for route in self.routes_dynamic[url_hash(url)]:
|
||||||
match = route.pattern.match(url)
|
match = route.pattern.match(url)
|
||||||
if match:
|
route_found |= match is not None
|
||||||
|
# Do early method checking
|
||||||
|
if match and method in route.methods:
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
# Lastly, check against all regex routes that cannot be hashed
|
# Lastly, check against all regex routes that cannot be hashed
|
||||||
for route in self.routes_always_check:
|
for route in self.routes_always_check:
|
||||||
match = route.pattern.match(url)
|
match = route.pattern.match(url)
|
||||||
if match:
|
route_found |= match is not None
|
||||||
|
# Do early method checking
|
||||||
|
if match and method in route.methods:
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
|
# Route was found but the methods didn't match
|
||||||
|
if route_found:
|
||||||
|
raise method_not_supported
|
||||||
raise NotFound('Requested URL {} not found'.format(url))
|
raise NotFound('Requested URL {} not found'.format(url))
|
||||||
|
|
||||||
if route.methods and method not in route.methods:
|
|
||||||
raise InvalidUsage(
|
|
||||||
'Method {} not allowed for URL {}'.format(
|
|
||||||
method, url), status_code=405)
|
|
||||||
|
|
||||||
kwargs = {p.name: p.cast(value)
|
kwargs = {p.name: p.cast(value)
|
||||||
for value, p
|
for value, p
|
||||||
in zip(match.groups(1), route.parameters)}
|
in zip(match.groups(1), route.parameters)}
|
||||||
return route.handler, [], kwargs
|
route_handler = route.handler
|
||||||
|
if hasattr(route_handler, 'handlers'):
|
||||||
|
route_handler = route_handler.handlers[method]
|
||||||
|
return route_handler, [], kwargs
|
||||||
|
|
|
@ -90,7 +90,7 @@ class Sanic:
|
||||||
def patch(self, uri, host=None):
|
def patch(self, uri, host=None):
|
||||||
return self.route(uri, methods=["PATCH"], host=host)
|
return self.route(uri, methods=["PATCH"], host=host)
|
||||||
|
|
||||||
def add_route(self, handler, uri, methods=None, host=None):
|
def add_route(self, handler, uri, methods=frozenset({'GET'}), host=None):
|
||||||
"""
|
"""
|
||||||
A helper method to register class instance or
|
A helper method to register class instance or
|
||||||
functions as a handler to the application url
|
functions as a handler to the application url
|
||||||
|
|
Loading…
Reference in New Issue
Block a user