Permalink
Browse files

Made quick corrections to fix inconsistencies with JSONRPClib new upd…

…ates.

git-svn-id: http://tornadorpc.googlecode.com/svn/trunk@8 6a0cc88c-bc38-11de-9a73-11f8458f6c97
  • Loading branch information...
1 parent 8088e07 commit 3da9f512e6554f491f567efebd450415503c6996 catchjosh committed Sep 10, 2010
Showing with 44 additions and 26 deletions.
  1. +11 −7 tornadorpc/base.py
  2. +29 −16 tornadorpc/json.py
  3. +4 −3 tornadorpc/xml.py
View
@@ -298,7 +298,7 @@ def __init__(self):
__call__ = obj
return PrivateMethod()
-def start_server(routes, route=r'/', port=8080):
+def start_server(handlers, route=r'/', port=8080):
"""
This is just a friendly wrapper around the default
Tornado instantiation calls. It simplifies the imports
@@ -310,18 +310,22 @@ def start_server(routes, route=r'/', port=8080):
import tornado.ioloop
import tornado.httpserver
import types
- if type(routes) not in (types.ListType, types.TupleType):
- routes = [(route, routes),]
- application = tornado.web.Application(routes)
+ if type(handlers) not in (types.ListType, types.TupleType):
+ handler = handlers
+ handlers = [(route, handler),]
+ if route != '/RPC2':
+ # friendly addition for /RPC2 if it's the only one
+ handlers.append(('/RPC2', handler))
+ application = tornado.web.Application(handlers)
http_server = tornado.httpserver.HTTPServer(application)
http_server.listen(port)
loop_instance = tornado.ioloop.IOLoop.instance()
""" Setting the '_server' attribute if not set """
- for route in routes:
+ for (route, handler) in handlers:
try:
- server_attrib = setattr(route[1], '_server', loop_instance)
+ server_attrib = setattr(handler, '_server', loop_instance)
except AttributeError:
- route[1]._server = loop_instance
+ handler._server = loop_instance
loop_instance.start()
return loop_instance
View
@@ -39,8 +39,10 @@
distribution as the "json" module.
"""
-from base import BaseRPCParser, BaseRPCHandler
+from tornadorpc.base import BaseRPCParser, BaseRPCHandler
import jsonrpclib
+from jsonrpclib.jsonrpc import isbatch, isnotification, Fault
+from jsonrpclib.jsonrpc import dumps, loads
import types
class JSONRPCParser(BaseRPCParser):
@@ -49,15 +51,15 @@ class JSONRPCParser(BaseRPCParser):
def parse_request(self, request_body):
try:
- request = jsonrpclib.loads(request_body)
+ request = loads(request_body)
except:
# Bad request formatting. Bad.
self.traceback()
return self.faults.parse_error()
self._requests = request
self._batch = False
request_list = []
- if jsonrpclib.isbatch(request):
+ if isbatch(request):
self._batch = True
for req in request:
req_tuple = (req['method'], req['params'])
@@ -68,15 +70,15 @@ def parse_request(self, request_body):
return tuple(request_list)
def parse_responses(self, responses):
- if isinstance(responses, jsonrpclib.Fault):
- return jsonrpclib.dumps(responses)
+ if isinstance(responses, Fault):
+ return dumps(responses)
if len(responses) != len(self._requests):
- return jsonrpclib.dumps(self.faults.internal_error())
+ return dumps(self.faults.internal_error())
response_list = []
for i in range(0, len(responses)):
request = self._requests[i]
response = responses[i]
- if jsonrpclib.isnotification(request):
+ if isnotification(request):
# Even in batches, notifications have no
# response entry
continue
@@ -85,11 +87,15 @@ def parse_responses(self, responses):
if 'jsonrpc' not in request.keys():
version = 1.0
try:
- response_json = jsonrpclib.dumps(response, version=version,
- rpcid=rpcid, methodresponse=True)
+ response_json = dumps(
+ response, version=version,
+ rpcid=rpcid, methodresponse=True
+ )
except TypeError:
- return jsonrpclib.dumps(self.faults.server_error(),
- rpcid=rpcid, version=version)
+ return dumps(
+ self.faults.server_error(),
+ rpcid=rpcid, version=version
+ )
response_list.append(response_json)
if not self._batch:
# Ensure it wasn't a batch to begin with, then
@@ -100,25 +106,32 @@ def parse_responses(self, responses):
return response_list[0]
# Batch, return list
return '[ %s ]' % ', '.join(response_list)
+
+class JSONRPCLibraryWrapper(object):
+
+ dumps = dumps
+ loads = loads
+ Fault = Fault
class JSONRPCHandler(BaseRPCHandler):
"""
Subclass this to add methods -- you can treat them
just like normal methods, this handles the JSON formatting.
"""
- _RPC_ = JSONRPCParser(jsonrpclib)
+ _RPC_ = JSONRPCParser(JSONRPCLibraryWrapper)
if __name__ == '__main__':
# Example Implementation
import sys
- from base import start_server
- from base import TestRPCHandler
+ from tornadorpc.base import start_server
+ from tornadorpc.base import TestRPCHandler
class TestJSONRPC(TestRPCHandler):
- _RPC_ = JSONRPCParser(jsonrpclib)
+ _RPC_ = JSONRPCParser(JSONRPCLibraryWrapper)
port = 8181
if len(sys.argv) > 1:
port = int(sys.argv[1])
-
+
+ print 'Starting server on port %s' % port
start_server(TestJSONRPC, port=port)
View
@@ -32,7 +32,7 @@
"""
-from base import BaseRPCParser, BaseRPCHandler
+from tornadorpc.base import BaseRPCParser, BaseRPCHandler
import xmlrpclib
import types
@@ -86,7 +86,7 @@ def system(self):
if __name__ == '__main__':
# Test implementation
- from base import TestRPCHandler, start_server
+ from tornadorpc.base import TestRPCHandler, start_server
import sys
port = 8282
@@ -99,4 +99,5 @@ class TestXMLRPC(TestRPCHandler):
def system(self):
return XMLRPCSystem(self)
- start_server(TestJSONRPC, port=port)
+ print 'Starting server on port %s' % port
+ start_server(TestXMLRPC, port=port)

0 comments on commit 3da9f51

Please sign in to comment.