def decorator(func): func.bottle_route = decorator_kwargs func.bottle_route['path'] = path annotations = getattr(func, "__annotations__", {}) request_args = { name: value for name, value in annotations.items() if isinstance(value, fields.Field) and name != "return" } func.bottle_route['apply'] = use_kwargs(request_args) @functools.wraps(func) def wrapped_func(*args, **kwargs): body = func(*args, **kwargs) if response.status_code == 200 and body is None: response.status = 204 if isinstance(body, StringIO): response.content_type = 'application/json; charset=utf-8' return body.getvalue() else: response.content_type = 'application/json; charset=utf-8' return json.dumps(body) if body is not None else '' return wrapped_func
validate=lambda args: args["value"] > 42, location="form", ), ) def echo_use_args_validated(args): return args @app.route("/echo_ignoring_extra_data", method=["POST"]) def echo_json_ignore_extra_data(): return parser.parse(hello_exclude_schema) @app.route("/echo_use_kwargs", method=["GET"], apply=use_kwargs(hello_args, location="query")) def echo_use_kwargs(name): return {"name": name} @app.route("/echo_multi", method=["GET"]) def echo_multi(): return parser.parse(hello_multiple, request, location="query") @app.route("/echo_multi_form", method=["POST"]) def multi_form(): return parser.parse(hello_multiple, location="form") @app.route("/echo_multi_json", method=["POST"])
hello_args = { 'name': fields.Str(missing='Friend') } @route('/', method='GET', apply=use_args(hello_args)) def index(args): """A welcome page. """ return {'message': 'Welcome, {}!'.format(args['name'])} add_args = { 'x': fields.Float(required=True), 'y': fields.Float(required=True), } @route('/add', method='POST', apply=use_kwargs(add_args)) def add(x, y): """An addition endpoint.""" return {'result': x + y} dateadd_args = { 'value': fields.Date(required=False), 'addend': fields.Int(required=True, validate=validate.Range(min=1)), 'unit': fields.Str(missing='days', validate=validate.OneOf(['minutes', 'days'])) } @route('/dateadd', method='POST', apply=use_kwargs(dateadd_args)) def dateadd(value, addend, unit): """A date adder endpoint.""" value = value or dt.datetime.utcnow() if unit == 'minutes': delta = dt.timedelta(minutes=addend)
@app.route("/echo_query") def echo_query(): return parser.parse(hello_args, request, locations=("query", )) @app.route("/echo_use_args", method=["GET", "POST"]) @use_args(hello_args) def echo_use_args(args): return args @app.route("/echo_use_kwargs", method=["GET", "POST"], apply=use_kwargs(hello_args)) def echo_use_kwargs(name): return {"name": name} @app.route( "/echo_use_args_validated", method=["GET", "POST"], apply=use_args({"value": fields.Int()}, validate=lambda args: args["value"] > 42), ) def echo_use_args_validated(args): return args @app.route("/echo_multi", method=["GET", "POST"])
from webargs.bottleparser import use_args, use_kwargs hello_args = {"name": fields.Str(missing="Friend")} @route("/", method="GET", apply=use_args(hello_args)) def index(args): """A welcome page.""" return {"message": "Welcome, {}!".format(args["name"])} add_args = {"x": fields.Float(required=True), "y": fields.Float(required=True)} @route("/add", method="POST", apply=use_kwargs(add_args)) def add(x, y): """An addition endpoint.""" return {"result": x + y} dateadd_args = { "value": fields.Date(required=False), "addend": fields.Int(required=True, validate=validate.Range(min=1)), "unit": fields.Str(missing="days", validate=validate.OneOf(["minutes", "days"])), } @route("/dateadd", method="POST", apply=use_kwargs(dateadd_args)) def dateadd(value, addend, unit): """A date adder endpoint."""
{"value": fields.Int()}, validate=lambda args: args["value"] > 42, location="form", ), ) def echo_use_args_validated(args): return args @app.route("/echo_ignoring_extra_data", method=["POST"]) def echo_json_ignore_extra_data(): return parser.parse(hello_exclude_schema) @app.route( "/echo_use_kwargs", method=["GET"], apply=use_kwargs(hello_args, location="query") ) def echo_use_kwargs(name): return {"name": name} @app.route("/echo_multi", method=["GET"]) def echo_multi(): return parser.parse(hello_multiple, request, location="query") @app.route("/echo_multi_form", method=["POST"]) def multi_form(): return parser.parse(hello_multiple, location="form")