You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
43 lines
1.0 KiB
43 lines
1.0 KiB
# -*- encoding: utf-8 -*-
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
from werkzeug.exceptions import BadRequest
|
|
|
|
|
|
class requires(object):
|
|
"""Verify that the request URL contains and can parse the parameter.
|
|
|
|
.. code-block:: python
|
|
|
|
@requires(int, "id")
|
|
def view(..., id):
|
|
assert isinstance(id, int)
|
|
|
|
Returns a 400 Bad Request that contains a specific error message.
|
|
"""
|
|
|
|
def __init__(self, type, param):
|
|
self.param = param
|
|
self.type = type
|
|
|
|
def __call__(self, func):
|
|
def dec(cls, env, req, *args, **kwargs):
|
|
|
|
if self.param not in req.args:
|
|
raise BadRequest("missing %s query" % self.param)
|
|
|
|
try:
|
|
kwargs[self.param] = self.type(req.args[self.param])
|
|
except TypeError:
|
|
raise BadRequest("invalid type for %s, expected %s" % (self.param, self.type))
|
|
|
|
return func(cls, env, req, *args, **kwargs)
|
|
|
|
return dec
|
|
|
|
from .api import API
|
|
from .info import Info
|
|
|
|
__all__ = ["requires", "API", "Info"]
|