Commits

Sven Hendriks committed fa37d87

Now using JSON for requests as well

Comments (0)

Files changed (3)

+import urllib
+
 import httplib2
-import simplejson
+import simplejson as json
 
 # Borrowed from xmlrpclib
 class RemoteMethod(object):
 		return RemoteMethod(self._send, attr)
 
 	def _send(self, *args, **kwargs):
-		path = '/'.join([str(arg) for arg in args])
+		query = '?args_json=%s&kwargs_json=%s' % (urllib.quote(json.dumps(args[1:])), urllib.quote(json.dumps(kwargs)))
 
-		url = 'http://%s:%d/%s' % (self.host, self.port, path)
+		url = 'http://%s:%d/%s/%s' % (self.host, self.port, args[0], query)
 
 		h = httplib2.Http()
+
 		resp, content = h.request(url)
 
 		status = resp.get('status')
 		if status in ('400', '404', '500'):
 			raise ServerError(content)
 
-		return simplejson.loads(content)
+		return json.loads(content)
 
 		self._handler = handler
 
 	@cherrypy.expose
-	def default(self, *args, **kwargs):
-		# check if we have a method name
-		if len(args) == 0:
-			raise NoMethodError
-
-		# extract the medthod name
-		methodname = args[0]
+	def default(self, methodname, args_json, kwargs_json):
+		args = json.loads(args_json)
+		kwargs = json.loads(kwargs_json)
 
 		# try to get the method from the handler
 		try:
 			raise MethodNotFound
 
 		# call method on handler
-		result = method(*args[1:], **kwargs)
+		result = method(*args, **kwargs)
 
 		# return result as JSON
 		cherrypy.response.headers['Content-Type'] = 'application/json'
 	install_requires=[
 		'simplejson',
 		'cherrypy',
+		'httplib2'
 	],
 )