def setup_routing(app: bottle.Bottle): # Status app.route('/health', 'GET', status.health_check) # Translator app.route('/fetch', 'POST', translator.fetch, apply=use_args(translator.fetch.args))
def add_route( app: bottle.Bottle, path: str, method: str, handler: Callable, apply: list = None ): """Helper for avoiding explicitly pushing use_args function to Bottle app's middleware list """ if apply is None: apply = [] if hasattr(handler, "args"): apply.append(use_args(handler.args)) app.route(path, method, handler, apply=apply)
def echo_json_or_form(): return parser.parse(hello_args, location="json_or_form") @app.route("/echo_use_args", method=["GET"]) @use_args(hello_args, location="query") def echo_use_args(args): return args @app.route( "/echo_use_args_validated", method=["POST"], apply=use_args( {"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"))
$ http GET :5001/ name==Ada $ http POST :5001/add x=40 y=2 $ http POST :5001/dateadd value=1973-04-10 addend=63 $ http POST :5001/dateadd value=2014-10-23 addend=525600 unit=minutes """ import datetime as dt from bottle import route, run, error, response from webargs import fields, validate 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 = {
@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"]) def echo_multi(): return parser.parse(hello_multiple, request) @app.route("/echo_many_schema", method=["GET", "POST"]) def echo_many_schema(): arguments = parser.parse(hello_many_schema, request, locations=("json", )) return HTTPResponse(body=json.dumps(arguments), content_type="application/json")
$ http GET :5001/ name==Ada $ http POST :5001/add x=40 y=2 $ http POST :5001/dateadd value=1973-04-10 addend=63 $ http POST :5001/dateadd value=2014-10-23 addend=525600 unit=minutes """ import datetime as dt from bottle import route, run, error, response from webargs import fields, validate 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 = {