"""Bottle request argument parsing module.Example: :: from bottle import route, run from marshmallow import fields from webargs.bottleparser import use_args hello_args = {"name": fields.Str(load_default="World")} @route("/", method="GET", apply=use_args(hello_args)) def index(args): return "Hello " + args["name"] if __name__ == "__main__": run(debug=True)"""importbottlefromwebargsimportcore
[docs]classBottleParser(core.Parser[bottle.Request]):"""Bottle.py request argument parser."""def_handle_invalid_json_error(self,error,req,*args,**kwargs):raisebottle.HTTPError(status=400,body={"json":["Invalid JSON body."]},exception=error)def_raw_load_json(self,req):"""Read a json payload from the request."""try:data=req.jsonexceptAttributeError:returncore.missingexceptbottle.HTTPErroraserr:iferr.body=="Invalid JSON":self._handle_invalid_json_error(err,req)else:raise# unfortunately, bottle does not distinguish between an empty body, "",# and a body containing the valid JSON value null, "null"# so these can't be properly disambiguated# as our best-effort solution, treat None as missing and ignore the# (admittedly unusual) "null" case# see: https://github.com/bottlepy/bottle/issues/1160ifdataisNone:returncore.missingreturndata
[docs]defload_querystring(self,req,schema):"""Return query params from the request as a MultiDictProxy."""returnself._makeproxy(req.query,schema)
[docs]defload_form(self,req,schema):"""Return form values from the request as a MultiDictProxy."""# For consistency with other parsers' behavior, don't attempt to# parse if content-type is mismatched.# TODO: Make this check more specificifcore.is_json(req.content_type):returncore.missingreturnself._makeproxy(req.forms,schema)
[docs]defload_headers(self,req,schema):"""Return headers from the request as a MultiDictProxy."""returnself._makeproxy(req.headers,schema)
[docs]defload_cookies(self,req,schema):"""Return cookies from the request."""returnreq.cookies
[docs]defload_files(self,req,schema):"""Return files from the request as a MultiDictProxy."""returnself._makeproxy(req.files,schema)
[docs]defhandle_error(self,error,req,schema,*,error_status_code,error_headers):"""Handles errors during parsing. Aborts the current request with a 400 error. """status_code=error_status_codeorself.DEFAULT_VALIDATION_STATUSraisebottle.HTTPError(status=status_code,body=error.messages,headers=error_headers,exception=error,)
[docs]defget_default_request(self):"""Override to use bottle's thread-local request object by default."""returnbottle.request