iro/view/jsonrpc.py
branchdevel
changeset 296 a73bbc1d8b4b
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/iro/view/jsonrpc.py	Thu Sep 27 14:20:03 2012 +0200
@@ -0,0 +1,83 @@
+# -*- coding: utf-8 -*-
+
+# Copyright (c) 2012 netzguerilla.net <iro@netzguerilla.net>
+#
+# This file is part of Iro.
+#
+# Permission is hereby granted, free of charge, to any person obtaining a copy of
+# this software and associated documentation files (the "Software"), to deal in
+# the Software without restriction, including without limitation the rights to use,
+# copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
+# #Software, and to permit persons to whom the Software is furnished to do so,
+# subject to the following conditions:
+#
+# The above copyright notice and this permission notice shall be included in
+# all copies or substantial portions of the Software.
+#
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
+# INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
+# PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+from txjsonrpc.web import jsonrpc
+from txjsonrpc import jsonrpclib
+from ..controller.viewinterface import Interface
+
+from ..error import InterfaceException, ValidateException
+
+class TwistedInterface(Interface):
+    """Class that addes needed function for XML-RPC/SOAP"""
+    def __init__(self):
+        Interface.__init__(self)
+
+    def listMethods(self):
+        """Since we override lookupProcedure, its suggested to override
+        listProcedures too.
+        """
+        return self.listProcedures()
+
+
+    def listProcedures(self):
+        """returns a list of all functions that are allowed to call via XML-RPC."""
+        return ['listMethods','status','sms','fax','mail','routes','defaultRoute','bill','telnumber','email']
+
+
+class JSONRPCInterface(TwistedInterface,jsonrpc.JSONRPC):
+    """JSON-RPC interface"""
+    NOT_FOUND = jsonrpclib.METHOD_NOT_FOUND
+    def __init__(self):
+        jsonrpc.JSONRPC.__init__(self)
+        TwistedInterface.__init__(self)
+
+    def _getFunction(self, procedurePath):
+        if procedurePath not in self.listProcedures():
+            raise jsonrpclib.NoSuchFunction(self.NOT_FOUND,
+                    "procedure %s not found" % procedurePath)
+        try:
+            return getattr(self,procedurePath)
+        except KeyError:
+            raise jsonrpclib.NoSuchFunction(self.NOT_FOUND,
+                    "procedure %s not found" % procedurePath)
+
+    def _ebRender(self, failure, id):
+        if isinstance(failure.value, InterfaceException):
+            return jsonrpclib.Fault(failure.value.code, failure.value.msg)
+        if isinstance(failure.value, ValidateException):
+            return jsonrpclib.Fault(failure.value.code, failure.value.msg)
+        return jsonrpc.JSONRPC._ebRender(self, failure, id)
+
+
+def appendResource(root):
+    """adding JSONRPC to root."""
+    root.putChild('jsonrpc', JSONRPCInterface())
+
+if __name__ == '__main__':
+    from twisted.web import resource, server
+    from twisted.internet import reactor
+
+    root = resource.Resource()
+    root = appendResource(root)
+    reactor.listenTCP(7080, server.Site(root))
+    reactor.run()