<s> from datetime import date , datetime <EOL> from functools import partial <EOL> from pyws . errors import BadRequest <EOL> from pyws . functions . args . types . complex import List <EOL> from pyws . response import Response <EOL> from pyws . utils import json <EOL> from pyws . protocols . base import Protocol <EOL> __all__ = ( '<STR_LIT>' , '<STR_LIT>' , ) <EOL> class DateIso8601Encoder ( json . JSONEncoder ) : <EOL> def default ( self , obj ) : <EOL> if isinstance ( obj , ( date , datetime ) ) : <EOL> return obj . isoformat ( ) <EOL> return json . JSONEncoder . default ( self , obj ) <EOL> create_response = partial ( Response , content_type = '<STR_LIT:application/json>' ) <EOL> create_error_response = partial ( create_response , status = Response . STATUS_ERROR ) <EOL> class RestProtocol ( Protocol ) : <EOL> name = '<STR_LIT>' <EOL> def get_function ( self , request ) : <EOL> return request . tail <EOL> def get_arguments ( self , request , arguments ) : <EOL> result = { } <EOL> for field in arguments . fields : <EOL> value = request . GET . get ( field . name ) <EOL> if issubclass ( field . type , List ) : <EOL> result [ field . name ] = value <EOL> elif field . name in request . GET : <EOL> result [ field . name ] = value [ <NUM_LIT:0> ] <EOL> return result <EOL> def get_response ( self , result , name , return_type ) : <EOL> return create_response ( <EOL> json . dumps ( { '<STR_LIT:result>' : result } , cls = DateIso8601Encoder ) ) <EOL> def get_error_response ( self , error ) : <EOL> return create_error_response ( <EOL> json . dumps ( { '<STR_LIT:error>' : self . get_error ( error ) } ) ) <EOL> class JsonProtocol ( RestProtocol ) : <EOL> name = '<STR_LIT>' <EOL> def get_arguments ( self , request , arguments ) : <EOL> try : <EOL> return json . loads ( request . text ) <EOL> except ValueError : <EOL> raise BadRequest ( ) </s>
