<s> """<STR_LIT>""" <EOL> from twisted . web import resource <EOL> from twisted . web import server <EOL> from twisted . internet . defer import maybeDeferred <EOL> from twisted . internet . defer import DeferredList <EOL> from twisted . internet . defer import succeed <EOL> import jsonrpc <EOL> class JSONRPCServer ( resource . Resource ) : <EOL> """<STR_LIT>""" <EOL> isLeaf = <NUM_LIT:1> <EOL> def _getRequestContent ( self , request ) : <EOL> """<STR_LIT>""" <EOL> request . content . seek ( <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> request_json = request . content . read ( ) <EOL> request_content = jsonrpc . decodeRequest ( request_json ) <EOL> return request_content <EOL> def _parseError ( self , request ) : <EOL> """<STR_LIT>""" <EOL> response = jsonrpc . parseError ( ) <EOL> self . _sendResponse ( response , request ) <EOL> def _callMethod ( self , request_dict ) : <EOL> """<STR_LIT>""" <EOL> function = getattr ( self , '<STR_LIT>' % request_dict [ '<STR_LIT>' ] , None ) <EOL> if callable ( function ) : <EOL> if '<STR_LIT>' in request_dict : <EOL> if isinstance ( request_dict [ '<STR_LIT>' ] , dict ) : <EOL> d = maybeDeferred ( function , ** request_dict [ '<STR_LIT>' ] ) <EOL> else : <EOL> d = maybeDeferred ( function , * request_dict [ '<STR_LIT>' ] ) <EOL> else : <EOL> d = maybeDeferred ( function ) <EOL> return d <EOL> else : <EOL> msg = '<STR_LIT>' % request_dict [ '<STR_LIT>' ] <EOL> raise jsonrpc . JSONRPCError ( msg , jsonrpc . METHOD_NOT_FOUND , <EOL> id_ = request_dict [ '<STR_LIT:id>' ] , <EOL> version = request_dict [ '<STR_LIT>' ] ) <EOL> def render ( self , request ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> request_content = self . _getRequestContent ( request ) <EOL> except jsonrpc . JSONRPCError : <EOL> self . _parseError ( request ) <EOL> return server . NOT_DONE_YET <EOL> is_batch = True <EOL> if not isinstance ( request_content , list ) : <EOL> request_content = [ request_content ] <EOL> is_batch = False <EOL> dl = [ ] <EOL> for request_dict in request_content : <EOL> d = succeed ( request_dict ) <EOL> d . addCallback ( jsonrpc . verifyMethodCall ) <EOL> d . addCallback ( self . _callMethod ) <EOL> d . addBoth ( jsonrpc . prepareMethodResponse , request_dict [ '<STR_LIT:id>' ] , <EOL> request_dict [ '<STR_LIT>' ] ) <EOL> dl . append ( d ) <EOL> dl = DeferredList ( dl , consumeErrors = True ) <EOL> dl . addBoth ( self . _cbFinishRequest , request , is_batch ) <EOL> return server . NOT_DONE_YET <EOL> def _cbFinishRequest ( self , results , request , is_batch ) : <EOL> """<STR_LIT>""" <EOL> method_responses = [ ] <EOL> for ( success , result ) in results : <EOL> if result is not None : <EOL> method_responses . append ( result ) <EOL> if not is_batch and len ( method_responses ) == <NUM_LIT:1> : <EOL> method_responses = method_responses [ <NUM_LIT:0> ] <EOL> response = jsonrpc . prepareCallResponse ( method_responses ) <EOL> self . _sendResponse ( response , request ) <EOL> def _sendResponse ( self , response , request ) : <EOL> """<STR_LIT>""" <EOL> if response != '<STR_LIT>' : <EOL> request . setHeader ( '<STR_LIT:Content-Type>' , '<STR_LIT:application/json>' ) <EOL> request . setHeader ( '<STR_LIT>' , str ( len ( response ) ) ) <EOL> request . write ( response ) <EOL> request . finish ( ) <EOL> def EncodingJSONRPCServer ( server ) : <EOL> """<STR_LIT>""" <EOL> from twisted . web . resource import EncodingResourceWrapper <EOL> from twisted . web . server import GzipEncoderFactory <EOL> return EncodingResourceWrapper ( server , [ GzipEncoderFactory ( ) ] ) </s>
