DjangoResources: Simplistic_json_rpc-dispatcher

File Simplistic_json_rpc-dispatcher, 1.4 KB (added by floydpetrus, 6 years ago)
Line 
1
2== A simplistic json-rpc dispatcher-function for Django in 15 lines ==
3
4{{{
5  ### myproj/myapp/views.py
6
7import sets
8import jsonrpc
9
10
11def JSON_RPC_io(object):
12      methods = Set(['<method1>', '<metohod2>', '<method3>', ....])  # Set of available service method names
13      rpc_inputs = jsonrpc.json.loads(object.raw_post_data)
14      sub_eval = str(rpc_inputs['method']) + '(rpc_inputs[\'params\'])'
15      if rpc_inputs['method'] in methods
16         result = eval(sub_eval)
17         json_retur = jsonrpc.json.dumps({'result': result['result'], 'error': result['error'], 'id': rpc_inputs['id']})
18         response = HttpResponse(json_retur)
19         response.__setitem__('Content-Type', 'application/json-rpc')
20         return response
21      else:
22         json_retur = jsonrpc.json.dumps({'result': None, 'error': '<non_existent_method_mesg>', 'id': rpc_inputs['id']})
23         response = HttpResponse(json_retur)
24         response.__setitem__('Content-Type', 'application/json-rpc')
25         return response
26         
27}}}
28
29
30This function works as a decoding/encoding dispatcher between Django's '''HttpRequest'''-object and
31arbitrary defined method-functions of the json-rpc service. Those method-functions shall return a
32dictionary-object such as '''{'result': <result_data>, 'error': <error_data>}''' . This dispatcher-function
33is suitable for non-public json-rpc apps handling specific data process/validate tasks.     
Back to Top