Merge pull request #243 from dutradda/add_remove_route_method

created methods to remove a route from api/router
This commit is contained in:
Eli Uriegas
2016-12-30 09:39:57 -06:00
committed by GitHub
3 changed files with 132 additions and 1 deletions

View File

@@ -23,6 +23,10 @@ class RouteExists(Exception):
pass
class RouteDoesNotExist(Exception):
pass
class Router:
"""
Router supports basic routing with parameters and method checks
@@ -109,6 +113,23 @@ class Router:
else:
self.routes_static[uri] = route
def remove(self, uri, clean_cache=True):
try:
route = self.routes_all.pop(uri)
except KeyError:
raise RouteDoesNotExist("Route was not registered: {}".format(uri))
if route in self.routes_always_check:
self.routes_always_check.remove(route)
elif url_hash(uri) in self.routes_dynamic \
and route in self.routes_dynamic[url_hash(uri)]:
self.routes_dynamic[url_hash(uri)].remove(route)
else:
self.routes_static.pop(uri)
if clean_cache:
self._get.cache_clear()
def get(self, request):
"""
Gets a request handler based on the URL of the request, or raises an

View File

@@ -80,6 +80,9 @@ class Sanic:
self.route(uri=uri, methods=methods)(handler)
return handler
def remove_route(self, uri, clean_cache=True):
self.router.remove(uri, clean_cache)
# Decorator
def exception(self, *exceptions):
"""