Refactor get
This commit is contained in:
parent
e25e1c0e4b
commit
baf1ce95b1
|
@ -6,10 +6,10 @@ Route = namedtuple('Route', ['handler', 'methods', 'pattern', 'parameters'])
|
||||||
Parameter = namedtuple('Parameter', ['name', 'cast'])
|
Parameter = namedtuple('Parameter', ['name', 'cast'])
|
||||||
|
|
||||||
REGEX_TYPES = {
|
REGEX_TYPES = {
|
||||||
'string': (None, r'[^/]+'),
|
'string': (str, r'[^/]+'),
|
||||||
'int': (int, r'\d+'),
|
'int': (int, r'\d+'),
|
||||||
'number': (float, r'[0-9\\.]+'),
|
'number': (float, r'[0-9\\.]+'),
|
||||||
'alpha': (None, r'[A-Za-z]+'),
|
'alpha': (str, r'[A-Za-z]+'),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -61,7 +61,7 @@ class Router:
|
||||||
if ':' in parameter_name:
|
if ':' in parameter_name:
|
||||||
parameter_name, parameter_pattern = parameter_name.split(':', 1)
|
parameter_name, parameter_pattern = parameter_name.split(':', 1)
|
||||||
|
|
||||||
default = (None, parameter_pattern)
|
default = (str, parameter_pattern)
|
||||||
# Pull from pre-configured types
|
# Pull from pre-configured types
|
||||||
parameter_type, parameter_pattern = REGEX_TYPES.get(parameter_pattern, default)
|
parameter_type, parameter_pattern = REGEX_TYPES.get(parameter_pattern, default)
|
||||||
parameters.append(Parameter(name=parameter_name, cast=parameter_type))
|
parameters.append(Parameter(name=parameter_name, cast=parameter_type))
|
||||||
|
@ -82,27 +82,18 @@ class Router:
|
||||||
:param request: Request object
|
:param request: Request object
|
||||||
:return: handler, arguments, keyword arguments
|
:return: handler, arguments, keyword arguments
|
||||||
"""
|
"""
|
||||||
|
|
||||||
route = None
|
route = None
|
||||||
args = []
|
for route in self.routes:
|
||||||
kwargs = {}
|
match = route.pattern.match(request.url)
|
||||||
for _route in self.routes:
|
|
||||||
match = _route.pattern.match(request.url)
|
|
||||||
if match:
|
if match:
|
||||||
for index, parameter in enumerate(_route.parameters, start=1):
|
|
||||||
value = match.group(index)
|
|
||||||
if parameter.cast:
|
|
||||||
kwargs[parameter.name] = parameter.cast(value)
|
|
||||||
else:
|
|
||||||
kwargs[parameter.name] = value
|
|
||||||
route = _route
|
|
||||||
break
|
break
|
||||||
|
else:
|
||||||
|
raise NotFound('Requested URL {} not found'.format(request.url))
|
||||||
|
|
||||||
if route:
|
|
||||||
if route.methods and request.method not in route.methods:
|
if route.methods and request.method not in route.methods:
|
||||||
raise InvalidUsage(
|
raise InvalidUsage(
|
||||||
'Method {} not allowed for URL {}'.format(
|
'Method {} not allowed for URL {}'.format(
|
||||||
request.method, request.url), status_code=405)
|
request.method, request.url), status_code=405)
|
||||||
return route.handler, args, kwargs
|
|
||||||
else:
|
kwargs = {p.name: p.cast(value) for value, p in zip(match.groups(1), route.parameters)}
|
||||||
raise NotFound('Requested URL {} not found'.format(request.url))
|
return route.handler, [], kwargs
|
||||||
|
|
Loading…
Reference in New Issue
Block a user