<s> from flask import request , make_response , jsonify <EOL> from flask . views import MethodView <EOL> from views import echo <EOL> class JSONRPCView ( MethodView ) : <EOL> """<STR_LIT>""" <EOL> request_fields = { "<STR_LIT>" : [ "<STR_LIT>" , "<STR_LIT>" ] , <EOL> "<STR_LIT>" : [ "<STR_LIT:id>" , "<STR_LIT>" ] } <EOL> def options ( self ) : <EOL> """<STR_LIT>""" <EOL> resp = make_response ( "<STR_LIT>" , <NUM_LIT:200> ) <EOL> resp . headers [ "<STR_LIT>" ] = request . headers . get ( "<STR_LIT>" , "<STR_LIT:*>" ) <EOL> resp . headers [ "<STR_LIT>" ] = "<STR_LIT>" <EOL> resp . headers [ "<STR_LIT>" ] = <NUM_LIT> * <NUM_LIT> <EOL> resp . headers [ "<STR_LIT>" ] = "<STR_LIT:Content-Type>" <EOL> return resp <EOL> def post ( self ) : <EOL> """<STR_LIT>""" <EOL> if ( not "<STR_LIT:id>" in request . json ) : <EOL> return <EOL> self . json_response = { "<STR_LIT>" : "<STR_LIT>" } <EOL> self . error = self . _is_jsonrpc_request ( ) <EOL> if ( not self . error ) : <EOL> self . error = self . _check_method_and_params ( ) <EOL> self . json_response [ "<STR_LIT:id>" ] = request . json [ "<STR_LIT:id>" ] <EOL> if ( self . error ) : <EOL> self . json_response [ "<STR_LIT:error>" ] = self . error <EOL> return self . make_post_response ( ) <EOL> self . echo ( ) <EOL> return self . make_post_response ( ) <EOL> def make_post_response ( self ) : <EOL> resp = jsonify ( self . json_response ) <EOL> resp . headers [ "<STR_LIT>" ] = request . headers . get ( "<STR_LIT>" , "<STR_LIT:*>" ) <EOL> return resp <EOL> def _is_jsonrpc_request ( self ) : <EOL> """<STR_LIT>""" <EOL> if ( not request . content_type == "<STR_LIT>" ) : <EOL> msg = "<STR_LIT>" <EOL> msg += "<STR_LIT>" <EOL> return { "<STR_LIT:code>" : - <NUM_LIT> , "<STR_LIT:message>" : msg } <EOL> for f in self . request_fields [ "<STR_LIT>" ] : <EOL> if ( not f in request . json ) : <EOL> msg = "<STR_LIT>" <EOL> msg += "<STR_LIT>" <EOL> return { "<STR_LIT:code>" : - <NUM_LIT> , "<STR_LIT:message>" : msg } <EOL> if ( request . json [ "<STR_LIT>" ] != "<STR_LIT>" ) : <EOL> msg = "<STR_LIT>" <EOL> return { "<STR_LIT:code>" : - <NUM_LIT> , "<STR_LIT:message>" : msg } <EOL> return { } <EOL> def _check_method_and_params ( self ) : <EOL> """<STR_LIT>""" <EOL> self . method = request . json [ "<STR_LIT>" ] . lower ( ) <EOL> if ( self . method not in self . rpc_methods ) : <EOL> msg = "<STR_LIT>" <EOL> return { "<STR_LIT:code>" : - <NUM_LIT> , "<STR_LIT:message>" : msg % ( request . json [ "<STR_LIT>" ] ) } <EOL> self . params = request . json . get ( "<STR_LIT>" ) <EOL> if ( not self . params ) : <EOL> msg = "<STR_LIT>" <EOL> return { "<STR_LIT:code>" : - <NUM_LIT> , "<STR_LIT:message>" : msg } <EOL> return None <EOL> def echo ( self ) : <EOL> raise NotImplementedError ( ) <EOL> class JSONEchoView ( JSONRPCView ) : <EOL> """<STR_LIT>""" <EOL> rpc_methods = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> def echo ( self ) : <EOL> rv = echo ( self . method , self . params [ <NUM_LIT:0> ] ) <EOL> if ( isinstance ( rv , dict ) ) : <EOL> self . json_response [ "<STR_LIT:error>" ] = rv <EOL> elif ( rv ) : <EOL> self . json_response [ "<STR_LIT:result>" ] = rv </s>
