Menu

[r9968]: / trunk / django-json-rpc / jsonrpc / proxy.py  Maximize  Restore  History

Download this file

68 lines (57 with data), 2.4 kB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import urllib, uuid
from django.test.client import FakePayload
from jsonrpc._json import loads, dumps
from jsonrpc.types import *
class ServiceProxy(object):
def __init__(self, service_url, service_name=None, version='1.0'):
self.version = str(version)
self.service_url = service_url
self.service_name = service_name
def __getattr__(self, name):
if self.service_name != None:
name = "%s.%s" % (self.service_name, name)
params = dict(self.__dict__, service_name=name)
return self.__class__(**params)
def __repr__(self):
return {"jsonrpc": self.version,
"method": self.service_name}
def send_payload(self, params):
"""Performs the actual sending action and returns the result"""
return urllib.urlopen(self.service_url,
dumps({
"jsonrpc": self.version,
"method": self.service_name,
'params': params,
'id': str(uuid.uuid1())})).read()
def __call__(self, *args, **kwargs):
params = kwargs if len(kwargs) else args
if Any.kind(params) == Object and self.version != '2.0':
raise Exception('Unsupport arg type for JSON-RPC 1.0 '
'(the default version for this client, '
'pass version="2.0" to use keyword arguments)')
r = self.send_payload(params)
y = loads(r)
if u'error' in y:
try:
from django.conf import settings
if settings.DEBUG:
print '%s error %r' % (self.service_name, y)
except:
pass
return y
class TestingServiceProxy(ServiceProxy):
"""Service proxy which works inside Django unittests"""
def __init__(self, client, *args, **kwargs):
super(TestingServiceProxy, self).__init__(*args, **kwargs)
self.client = client
def send_payload(self, params):
dump = dumps({"jsonrpc" : self.version,
"method" : self.service_name,
"params" : params,
"id" : str(uuid.uuid1())
})
dump_payload = FakePayload(dump)
response = self.client.post(self.service_url,
**{"wsgi.input" : dump_payload,
'CONTENT_LENGTH' : len(dump)})
return response.content
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.