<s> import json <EOL> from flask import Flask , jsonify as J , Response , request <EOL> from flask . views import MethodView <EOL> import marshmallow as ma <EOL> from webargs import fields , ValidationError , missing <EOL> from webargs . flaskparser import parser , use_args , use_kwargs <EOL> class TestAppConfig : <EOL> TESTING = True <EOL> DEBUG = True <EOL> hello_args = { <EOL> '<STR_LIT:name>' : fields . Str ( missing = '<STR_LIT>' , validate = lambda n : len ( n ) >= <NUM_LIT:3> ) , <EOL> } <EOL> hello_multiple = { <EOL> '<STR_LIT:name>' : fields . List ( fields . Str ( ) ) <EOL> } <EOL> class HelloSchema ( ma . Schema ) : <EOL> name = fields . Str ( missing = '<STR_LIT>' , validate = lambda n : len ( n ) >= <NUM_LIT:3> ) <EOL> hello_many_schema = HelloSchema ( strict = True , many = True ) <EOL> app = Flask ( __name__ ) <EOL> app . config . from_object ( TestAppConfig ) <EOL> @ app . route ( '<STR_LIT>' , methods = [ '<STR_LIT:GET>' , '<STR_LIT:POST>' ] ) <EOL> def echo ( ) : <EOL> return J ( parser . parse ( hello_args ) ) <EOL> @ app . route ( '<STR_LIT>' ) <EOL> def echo_query ( ) : <EOL> return J ( parser . parse ( hello_args , request , locations = ( '<STR_LIT>' , ) ) ) <EOL> @ app . route ( '<STR_LIT>' , methods = [ '<STR_LIT:GET>' , '<STR_LIT:POST>' ] ) <EOL> @ use_args ( hello_args ) <EOL> def echo_use_args ( args ) : <EOL> return J ( args ) <EOL> @ app . route ( '<STR_LIT>' , methods = [ '<STR_LIT:GET>' , '<STR_LIT:POST>' ] ) <EOL> @ use_args ( { '<STR_LIT:value>' : fields . Int ( ) } , validate = lambda args : args [ '<STR_LIT:value>' ] > <NUM_LIT> ) <EOL> def echo_use_args_validated ( args ) : <EOL> return J ( args ) <EOL> @ app . route ( '<STR_LIT>' , methods = [ '<STR_LIT:GET>' , '<STR_LIT:POST>' ] ) <EOL> @ use_kwargs ( hello_args ) <EOL> def echo_use_kwargs ( name ) : <EOL> return J ( { '<STR_LIT:name>' : name } ) <EOL> @ app . route ( '<STR_LIT>' , methods = [ '<STR_LIT:GET>' , '<STR_LIT:POST>' ] ) <EOL> def multi ( ) : <EOL> return J ( parser . parse ( hello_multiple ) ) <EOL> @ app . route ( '<STR_LIT>' , methods = [ '<STR_LIT:GET>' , '<STR_LIT:POST>' ] ) <EOL> def many_nested ( ) : <EOL> arguments = parser . parse ( hello_many_schema , locations = ( '<STR_LIT>' , ) ) <EOL> return Response ( json . dumps ( arguments ) , content_type = '<STR_LIT:application/json>' ) <EOL> @ app . route ( '<STR_LIT>' ) <EOL> @ use_args ( { '<STR_LIT:value>' : fields . Int ( ) } ) <EOL> def echo_use_args_with_path ( args , name ) : <EOL> return J ( args ) <EOL> @ app . route ( '<STR_LIT>' ) <EOL> @ use_kwargs ( { '<STR_LIT:value>' : fields . Int ( ) } ) <EOL> def echo_use_kwargs_with_path ( name , value ) : <EOL> return J ( { '<STR_LIT:value>' : value } ) <EOL> @ app . route ( '<STR_LIT>' , methods = [ '<STR_LIT:GET>' , '<STR_LIT:POST>' ] ) <EOL> def error ( ) : <EOL> def always_fail ( value ) : <EOL> raise ValidationError ( '<STR_LIT>' ) <EOL> args = { '<STR_LIT:text>' : fields . Str ( validate = always_fail ) } <EOL> return J ( parser . parse ( args ) ) <EOL> @ app . route ( '<STR_LIT>' , methods = [ '<STR_LIT:GET>' , '<STR_LIT:POST>' ] ) <EOL> def error400 ( ) : <EOL> def always_fail ( value ) : <EOL> raise ValidationError ( '<STR_LIT>' , status_code = <NUM_LIT> ) <EOL> args = { '<STR_LIT:text>' : fields . Str ( validate = always_fail ) } <EOL> return J ( parser . parse ( args ) ) <EOL> @ app . route ( '<STR_LIT>' ) <EOL> def echo_headers ( ) : <EOL> return J ( parser . parse ( hello_args , locations = ( '<STR_LIT>' , ) ) ) <EOL> @ app . route ( '<STR_LIT>' ) <EOL> def echo_cookie ( ) : <EOL> return J ( parser . parse ( hello_args , request , locations = ( '<STR_LIT>' , ) ) ) <EOL> @ app . route ( '<STR_LIT>' , methods = [ '<STR_LIT:POST>' ] ) <EOL> def echo_file ( ) : <EOL> args = { '<STR_LIT>' : fields . Field ( ) } <EOL> result = parser . parse ( args , locations = ( '<STR_LIT>' , ) ) <EOL> fp = result [ '<STR_LIT>' ] <EOL> content = fp . read ( ) . decode ( '<STR_LIT:utf8>' ) <EOL> return J ( { '<STR_LIT>' : content } ) <EOL> @ app . route ( '<STR_LIT>' ) <EOL> def echo_view_arg ( view_arg ) : <EOL> return J ( parser . parse ( { '<STR_LIT>' : fields . Int ( ) } , locations = ( '<STR_LIT>' , ) ) ) <EOL> @ app . route ( '<STR_LIT>' ) <EOL> @ use_args ( { '<STR_LIT>' : fields . Int ( location = '<STR_LIT>' ) } ) <EOL> def echo_view_arg_with_use_args ( args , ** kwargs ) : <EOL> return J ( args ) <EOL> @ app . route ( '<STR_LIT>' , methods = [ '<STR_LIT:POST>' ] ) <EOL> def echo_nested ( ) : <EOL> args = { <EOL> '<STR_LIT:name>' : fields . Nested ( { '<STR_LIT>' : fields . Str ( ) , <EOL> '<STR_LIT>' : fields . Str ( ) } ) <EOL> } <EOL> return J ( parser . parse ( args ) ) <EOL> @ app . route ( '<STR_LIT>' , methods = [ '<STR_LIT:POST>' ] ) <EOL> def echo_nested_many ( ) : <EOL> args = { <EOL> '<STR_LIT>' : fields . Nested ( { '<STR_LIT:id>' : fields . Int ( ) , '<STR_LIT:name>' : fields . Str ( ) } , many = True ) <EOL> } <EOL> return J ( parser . parse ( args ) ) <EOL> class EchoMethodViewUseArgs ( MethodView ) : <EOL> @ use_args ( { '<STR_LIT>' : fields . Int ( ) } ) <EOL> def post ( self , args ) : <EOL> return J ( args ) <EOL> app . add_url_rule ( '<STR_LIT>' , <EOL> view_func = EchoMethodViewUseArgs . as_view ( '<STR_LIT>' ) ) <EOL> class EchoMethodViewUseKwargs ( MethodView ) : <EOL> @ use_kwargs ( { '<STR_LIT>' : fields . Int ( ) } ) <EOL> def post ( self , val ) : <EOL> return J ( { '<STR_LIT>' : val } ) <EOL> app . add_url_rule ( '<STR_LIT>' , <EOL> view_func = EchoMethodViewUseKwargs . as_view ( '<STR_LIT>' ) ) <EOL> @ app . route ( '<STR_LIT>' , methods = [ '<STR_LIT>' ] ) <EOL> @ use_kwargs ( { '<STR_LIT:username>' : fields . Str ( ) , '<STR_LIT:password>' : fields . Str ( ) } ) <EOL> def echo_use_kwargs_missing ( username , password ) : <EOL> assert password is missing <EOL> return J ( { '<STR_LIT:username>' : username } ) <EOL> @ app . errorhandler ( <NUM_LIT> ) <EOL> def handle_validation_error ( err ) : <EOL> exc = err . data [ '<STR_LIT>' ] <EOL> return J ( { '<STR_LIT>' : exc . messages } ) , <NUM_LIT> </s>
