17 | | Another power of python here! |
| 17 | |
| 18 | == django-json-rpc == |
| 19 | |
| 20 | Another, easier to use JSON-RPC implementation. |
| 21 | |
| 22 | [http://github.com/samuraisam/django-json-rpc/tree/master django-json-rpc @ github] |
| 23 | |
| 24 | Features: |
| 25 | * Simple, pythonic API |
| 26 | * Support for Django authentication |
| 27 | * Mostly supports JSON-RPC 1.1 spec |
| 28 | * Proxy to test your JSON Service |
| 29 | |
| 30 | The basic API: |
| 31 | |
| 32 | {{{ |
| 33 | ### myproj/myapp/views.py |
| 34 | |
| 35 | from jsonrpc import jsonrpc_method |
| 36 | |
| 37 | @jsonrpc_method('myapp.sayHello') |
| 38 | def whats_the_time(request, name='Lester'): |
| 39 | return "Hello %s" % name |
| 40 | |
| 41 | @jsonrpc_method('myapp.gimmeThat', authenticated=True) |
| 42 | def something_special(request, secret_data): |
| 43 | return {'sauce': ['authenticated', 'sauce']} |
| 44 | |
| 45 | |
| 46 | ### myproj/urls.py |
| 47 | |
| 48 | from jsonrpc import jsonrpc_site |
| 49 | import myproj.myapp.views # you must import the views that need connected |
| 50 | |
| 51 | urls += patterns('', (r'^json/', jsonrpc_site.dispatch)) |
| 52 | }}} |
| 53 | |
| 54 | To test your service: |
| 55 | |
| 56 | {{{ |
| 57 | >>> from jsonrpc.proxy import ServiceProxy |
| 58 | |
| 59 | >>> s = ServiceProxy('http://localhost:8080/json/') |
| 60 | |
| 61 | >>> s.myapp.sayHello('Sam') |
| 62 | {u'error': None, u'id': u'jsonrpc', u'result': u'Hello Sam'} |
| 63 | |
| 64 | >>> s.myapp.gimmeThat('username', 'password', 'test data') |
| 65 | {u'error': None, u'id': u'jsonrpc', u'result': {u'sauce': [u'authenticated', u'sauce']}} |
| 66 | }}} |