Source code for webargs.webapp2parser

# -*- coding: utf-8 -*-
"""Webapp2 request argument parsing module.

Example: ::

    import webapp2

    from marshmallow import fields
    from webargs.webobparser import use_args

    hello_args = {
        'name': fields.Str(missing='World')
    }

    class MainPage(webapp2.RequestHandler):

        @use_args(hello_args)
        def get_args(self, args):
            self.response.write('Hello, {name}!'.format(name=args['name']))

        @use_kwargs(hello_args)
        def get_kwargs(self, name=None):
            self.response.write('Hello, {name}!'.format(name=name))

    app = webapp2.WSGIApplication([
        webapp2.Route(r'/hello', MainPage, handler_method='get_args'),
        webapp2.Route(r'/hello_dict', MainPage, handler_method='get_kwargs'),
    ], debug=True)
"""
from webargs import core
import webapp2
import webapp2_extras.json
import webob.multidict


[docs]class Webapp2Parser(core.Parser): """webapp2 request argument parser."""
[docs] def parse_json(self, req, name, field): """Pull a json value from the request.""" try: json_data = webapp2_extras.json.decode(req.body) except ValueError: return core.missing return core.get_value(json_data, name, field, allow_many_nested=True)
[docs] def parse_querystring(self, req, name, field): """Pull a querystring value from the request.""" return core.get_value(req.GET, name, field)
[docs] def parse_form(self, req, name, field): """Pull a form value from the request.""" return core.get_value(req.POST, name, field)
[docs] def parse_cookies(self, req, name, field): """Pull the value from the cookiejar.""" return core.get_value(req.cookies, name, field)
[docs] def parse_headers(self, req, name, field): """Pull a value from the header data.""" return core.get_value(req.headers, name, field)
[docs] def parse_files(self, req, name, field): """Pull a file from the request.""" files = ((k, v) for k, v in req.POST.items() if hasattr(v, "file")) return core.get_value(webob.multidict.MultiDict(files), name, field)
[docs] def get_default_request(self): return webapp2.get_request()
parser = Webapp2Parser() use_args = parser.use_args use_kwargs = parser.use_kwargs