Changes between Version 5 and Version 6 of Simplistic_json_rpc-dispatcher


Ignore:
Timestamp:
Nov 24, 2009, 4:37:53 PM (15 years ago)
Author:
anonymous
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • Simplistic_json_rpc-dispatcher

    v5 v6  
    44  ### myproj/myapp/views.py
    55
    6 import sets
    7 import jsonrpc
    86
     7import json
    98
    10 def JSON_RPC_io(object):
    11       methods = set(['<method1>', '<method2>', '<method3>', ....])  # Set of available service method names
    12       rpc_inputs = jsonrpc.json.loads(object.raw_post_data)
     9jsonrpc_methods = []
     10
     11def JSON_RPC_io(obj):
     12      rpc_inputs = json.loads(obj.raw_post_data)
    1313      sub_eval = str(rpc_inputs['method']) + '(rpc_inputs[\'params\'])'
    14       if rpc_inputs['method'] in methods:
     14      if rpc_inputs['method'] in jsonrpc_methods:
    1515         result = eval(sub_eval)
    16          json_retur = jsonrpc.json.dumps({'result': result['result'], 'error': result['error'], 'id': rpc_inputs['id']})
     16         json_retur = json.dumps({'result': result['result'], 'error': result['error'], 'id': rpc_inputs['id']})
    1717         response = HttpResponse(json_retur)
    1818         response.__setitem__('Content-Type', 'application/json-rpc')
    1919         return response
    2020      else:
    21          json_retur = jsonrpc.json.dumps({'result': None, 'error': '<non_existent_method_mesg>', 'id': rpc_inputs['id']})
     21         json_retur = json.dumps({'result': None, 'error': '<non_existent_method_mesg>', 'id': rpc_inputs['id']})
    2222         response = HttpResponse(json_retur)
    2323         response.__setitem__('Content-Type', 'application/json-rpc')
     
    2929This function works as a decoding/encoding dispatcher between Django's '''HttpRequest'''-object and
    3030arbitrary defined method-functions of the json-rpc service. Those '''method-functions shall return''' a
    31 dictionary-object as '''{'result': <result_data>, 'error': <error_data>}''' . This dispatcher-function
    32 is suitable for non-public json-rpc apps handling specific data process/validate tasks.   
     31dictionary-object as '''{'result': <result_data>, 'error': <error_data>}''' and their '''names shall be appended/registered''' to the '''jsonrpc_methods list'''. This dispatcher-function is generally suitable for json-rpc apps handling different data process/validate tasks.   
Back to Top