| | 1 | |
| | 2 | == Django JSON-RPC == |
| | 3 | |
| | 4 | A basic JSON-RPC Implementation for Django powered sites (see https://github.com/anaoum/django-json-rpc). |
| | 5 | |
| | 6 | |
| | 7 | === Features: === |
| | 8 | |
| | 9 | Simple, pythonic API |
| | 10 | Supports JSON-RPC 2.0 Spec |
| | 11 | |
| | 12 | === The basic API: === |
| | 13 | |
| | 14 | ''project/app/views.py'' |
| | 15 | |
| | 16 | {{{ |
| | 17 | from pe.jsonrpc.views import JsonRpcView |
| | 18 | from pe.jsonrpc.decorators import publicmethod |
| | 19 | |
| | 20 | class TestRpcMethods(object): |
| | 21 | namespace = "test" |
| | 22 | @publicmethod |
| | 23 | def hello(self, who="World"): |
| | 24 | return "Hello, %s!" % who |
| | 25 | @publicmethod |
| | 26 | def echo(self, value): |
| | 27 | return value |
| | 28 | |
| | 29 | rpc = JsonRpcView.as_view(classes=[TestRpcMethods]) |
| | 30 | }}} |
| | 31 | |
| | 32 | ''project/urls.py'' |
| | 33 | |
| | 34 | {{{ |
| | 35 | from django.conf.urls.defaults import * |
| | 36 | |
| | 37 | urlpatterns = patterns('', |
| | 38 | (r'^rpc/json/$', 'app.views.rpc'), |
| | 39 | ) |
| | 40 | }}} |
| | 41 | |
| | 42 | To test your service: You can test the service with jsonrpclib (https://github.com/joshmarshall/jsonrpclib) or similar: |
| | 43 | |
| | 44 | {{{ |
| | 45 | |
| | 46 | >>> from jsonrpclib import Server |
| | 47 | |
| | 48 | >>> s = Server('http://localhost:8000/rpc/json/') |
| | 49 | |
| | 50 | >>> s.test.hello() |
| | 51 | u'Hello, World!' |
| | 52 | |
| | 53 | >>> s.test.hello('Andrew') |
| | 54 | u'Hello, Andrew!' |
| | 55 | |
| | 56 | >>> s.test.hello(who='Bob') |
| | 57 | u'Hello, Bob!' |
| | 58 | |
| | 59 | >>> s.test.echo('This is a test...') |
| | 60 | u'This is a test...' |
| | 61 | }}} |