<s> import json <EOL> from django . http import HttpResponse <EOL> from django . views . generic import View <EOL> import marshmallow as ma <EOL> from webargs import fields , ValidationError <EOL> from webargs . djangoparser import parser , use_args , use_kwargs <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> def json_response ( data , ** kwargs ) : <EOL> return HttpResponse ( <EOL> json . dumps ( data ) , <EOL> content_type = '<STR_LIT:application/json>' , <EOL> ** kwargs <EOL> ) <EOL> def echo ( request ) : <EOL> try : <EOL> args = parser . parse ( hello_args , request ) <EOL> except ValidationError as err : <EOL> return json_response ( err . messages , status = err . status_code ) <EOL> return json_response ( args ) <EOL> def echo_query ( request ) : <EOL> return json_response ( parser . parse ( hello_args , request , locations = ( '<STR_LIT>' , ) ) ) <EOL> @ use_args ( hello_args ) <EOL> def echo_use_args ( request , args ) : <EOL> return json_response ( args ) <EOL> @ use_kwargs ( hello_args ) <EOL> def echo_use_kwargs ( request , name ) : <EOL> return json_response ( { '<STR_LIT:name>' : name } ) <EOL> def echo_multi ( request ) : <EOL> return json_response ( parser . parse ( hello_multiple , request ) ) <EOL> def echo_many_schema ( request ) : <EOL> try : <EOL> return json_response ( parser . parse ( hello_many_schema , request , locations = ( '<STR_LIT>' , ) ) ) <EOL> except ValidationError as err : <EOL> return json_response ( err . messages , status = err . status_code ) <EOL> @ use_args ( { '<STR_LIT:value>' : fields . Int ( ) } ) <EOL> def echo_use_args_with_path_param ( request , args , name ) : <EOL> return json_response ( args ) <EOL> @ use_kwargs ( { '<STR_LIT:value>' : fields . Int ( ) } ) <EOL> def echo_use_kwargs_with_path_param ( request , value , name ) : <EOL> return json_response ( { '<STR_LIT:value>' : value } ) <EOL> def always_error ( request ) : <EOL> def always_fail ( value ) : <EOL> raise ValidationError ( '<STR_LIT>' ) <EOL> argmap = { '<STR_LIT:text>' : fields . Str ( validate = always_fail ) } <EOL> try : <EOL> return parser . parse ( argmap , request ) <EOL> except ValidationError as err : <EOL> return json_response ( err . messages , status = err . status_code ) <EOL> def error400 ( request ) : <EOL> def always_fail ( value ) : <EOL> raise ValidationError ( '<STR_LIT>' , status_code = <NUM_LIT> ) <EOL> argmap = { '<STR_LIT:text>' : fields . Str ( validate = always_fail ) } <EOL> try : <EOL> return parser . parse ( argmap , request ) <EOL> except ValidationError as err : <EOL> return json_response ( err . messages , status = err . status_code ) <EOL> def echo_headers ( request ) : <EOL> return json_response ( parser . parse ( hello_args , request , locations = ( '<STR_LIT>' , ) ) ) <EOL> def echo_cookie ( request ) : <EOL> return json_response ( parser . parse ( hello_args , request , locations = ( '<STR_LIT>' , ) ) ) <EOL> def echo_file ( request ) : <EOL> args = { '<STR_LIT>' : fields . Field ( ) } <EOL> result = parser . parse ( args , request , locations = ( '<STR_LIT>' , ) ) <EOL> myfile = result [ '<STR_LIT>' ] <EOL> content = myfile . read ( ) . decode ( '<STR_LIT:utf8>' ) <EOL> return json_response ( { '<STR_LIT>' : content } ) <EOL> def echo_nested ( request ) : <EOL> argmap = { <EOL> '<STR_LIT:name>' : fields . Nested ( { '<STR_LIT>' : fields . Str ( ) , <EOL> '<STR_LIT>' : fields . Str ( ) } ) <EOL> } <EOL> return json_response ( parser . parse ( argmap , request ) ) <EOL> def echo_nested_many ( request ) : <EOL> argmap = { <EOL> '<STR_LIT>' : fields . Nested ( { '<STR_LIT:id>' : fields . Int ( ) , '<STR_LIT:name>' : fields . Str ( ) } , many = True ) <EOL> } <EOL> return json_response ( parser . parse ( argmap , request ) ) <EOL> class EchoCBV ( View ) : <EOL> def get ( self , request ) : <EOL> try : <EOL> args = parser . parse ( hello_args , self . request ) <EOL> except ValidationError as err : <EOL> return json_response ( err . messages , status = err . status_code ) <EOL> return json_response ( args ) <EOL> post = get <EOL> class EchoUseArgsCBV ( View ) : <EOL> @ use_args ( hello_args ) <EOL> def get ( self , request , args ) : <EOL> return json_response ( args ) <EOL> post = get <EOL> class EchoUseArgsWithParamCBV ( View ) : <EOL> @ use_args ( hello_args ) <EOL> def get ( self , request , args , pid ) : <EOL> return json_response ( args ) <EOL> post = get </s>
