Every repository with this icon (
Every repository with this icon (
| name | age | message | |
|---|---|---|---|
| |
COPYING | Fri Jul 03 14:22:30 -0700 2009 | |
| |
README.mdown | Sun Nov 15 02:09:40 -0800 2009 | |
| |
jsonrpc/ | Sun Nov 15 02:09:40 -0800 2009 | |
| |
setup.py | Sat Oct 17 20:29:17 -0700 2009 | |
| |
test/ | Sun Nov 15 02:09:40 -0800 2009 |
Django JSON-RPC
A basic JSON-RPC Implementation for your Django-powered sites. See this blog entry for more information.
Features:
- Simple, pythonic API
- Support for Django authentication
- Mostly supports JSON-RPC 1.1 and 2.0 Spec
- Proxy to test your JSON Service
- Graphical JSON-RPC browser and web console
- Provides
system.describe
The basic API:
myproj/myapp/views.py
from jsonrpc import jsonrpc_method
@jsonrpc_method('myapp.sayHello')
def whats_the_time(request, name='Lester'):
return "Hello %s" % name
@jsonrpc_method('myapp.gimmeThat', authenticated=True)
def something_special(request, secret_data):
return {'sauce': ['authenticated', 'sauce']}
myproj/urls.py
from django.conf.urls.defaults import *
from jsonrpc import jsonrpc_site
import myproj.myapp.views # you must import the views that need connected
urlpatterns += patterns('',
url(r'^json/', jsonrpc_site.dispatch, name="jsonrpc_mountpoint"),
url(r'^json/browse/', 'jsonrpc.views.browse', name="jsonrpc_browse"), # for the graphical browser/web console only, omissible
(r'^json/(?P<method>[a-zA-Z0-9.]+)$', jsonrpc_site.dispatch) # for HTTP GET only, also omissible
)
To test your service: You can test your service using the provided graphical browser and console, available at http://YOUR_URL/json/browse/ (if using the url patterns from above) or with the included ServiceProxy:
>>> from jsonrpc.proxy import ServiceProxy
>>> s = ServiceProxy('http://localhost:8080/json/')
>>> s.myapp.sayHello('Sam')
{u'error': None, u'id': u'jsonrpc', u'result': u'Hello Sam'}
>>> s.myapp.gimmeThat('username', 'password', 'test data')
{u'error': None, u'id': u'jsonrpc', u'result': {u'sauce': [u'authenticated', u'sauce']}}
Method Browser:
We add the jsonrpc_version variable to the request object. It be either '1.0', '1.1' or '2.0'. Arg.
The jsonrpc.jsonrpc_method decorator
jsonrpc.jsonrpc_method(name, authenticated=False, safe=False)
-
nameThe name of your method. IE:
namespace.methodName -
authenticated=FalseAdds
usernameandpasswordarguments to the beginning of your method if the user hasn't already been authenticated. These will be used to authenticate the user againstdjango.contrib.authenticateIf you use HTTP auth or other authentication middleware,usernameandpasswordwill not be added, and this method will only check againstrequest.user.is_authenticated.You may pass a callablle to replace
django.contrib.auth.authenticateas the authentication method. It must return either a User orNoneand take the keyword argumentsusernameandpassword. -
safe=FalseDesignates whether or not your method may be accessed by HTTP GET. By default this is turned off.








