iro/view/jsonrpc.py
branchdevel
changeset 296 a73bbc1d8b4b
equal deleted inserted replaced
295:dc3cc61c7f6f 296:a73bbc1d8b4b
       
     1 # -*- coding: utf-8 -*-
       
     2 
       
     3 # Copyright (c) 2012 netzguerilla.net <iro@netzguerilla.net>
       
     4 #
       
     5 # This file is part of Iro.
       
     6 #
       
     7 # Permission is hereby granted, free of charge, to any person obtaining a copy of
       
     8 # this software and associated documentation files (the "Software"), to deal in
       
     9 # the Software without restriction, including without limitation the rights to use,
       
    10 # copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
       
    11 # #Software, and to permit persons to whom the Software is furnished to do so,
       
    12 # subject to the following conditions:
       
    13 #
       
    14 # The above copyright notice and this permission notice shall be included in
       
    15 # all copies or substantial portions of the Software.
       
    16 #
       
    17 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
       
    18 # INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
       
    19 # PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
       
    20 # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
       
    21 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
       
    22 # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
       
    23 
       
    24 from txjsonrpc.web import jsonrpc
       
    25 from txjsonrpc import jsonrpclib
       
    26 from ..controller.viewinterface import Interface
       
    27 
       
    28 from ..error import InterfaceException, ValidateException
       
    29 
       
    30 class TwistedInterface(Interface):
       
    31     """Class that addes needed function for XML-RPC/SOAP"""
       
    32     def __init__(self):
       
    33         Interface.__init__(self)
       
    34 
       
    35     def listMethods(self):
       
    36         """Since we override lookupProcedure, its suggested to override
       
    37         listProcedures too.
       
    38         """
       
    39         return self.listProcedures()
       
    40 
       
    41 
       
    42     def listProcedures(self):
       
    43         """returns a list of all functions that are allowed to call via XML-RPC."""
       
    44         return ['listMethods','status','sms','fax','mail','routes','defaultRoute','bill','telnumber','email']
       
    45 
       
    46 
       
    47 class JSONRPCInterface(TwistedInterface,jsonrpc.JSONRPC):
       
    48     """JSON-RPC interface"""
       
    49     NOT_FOUND = jsonrpclib.METHOD_NOT_FOUND
       
    50     def __init__(self):
       
    51         jsonrpc.JSONRPC.__init__(self)
       
    52         TwistedInterface.__init__(self)
       
    53 
       
    54     def _getFunction(self, procedurePath):
       
    55         if procedurePath not in self.listProcedures():
       
    56             raise jsonrpclib.NoSuchFunction(self.NOT_FOUND,
       
    57                     "procedure %s not found" % procedurePath)
       
    58         try:
       
    59             return getattr(self,procedurePath)
       
    60         except KeyError:
       
    61             raise jsonrpclib.NoSuchFunction(self.NOT_FOUND,
       
    62                     "procedure %s not found" % procedurePath)
       
    63 
       
    64     def _ebRender(self, failure, id):
       
    65         if isinstance(failure.value, InterfaceException):
       
    66             return jsonrpclib.Fault(failure.value.code, failure.value.msg)
       
    67         if isinstance(failure.value, ValidateException):
       
    68             return jsonrpclib.Fault(failure.value.code, failure.value.msg)
       
    69         return jsonrpc.JSONRPC._ebRender(self, failure, id)
       
    70 
       
    71 
       
    72 def appendResource(root):
       
    73     """adding JSONRPC to root."""
       
    74     root.putChild('jsonrpc', JSONRPCInterface())
       
    75 
       
    76 if __name__ == '__main__':
       
    77     from twisted.web import resource, server
       
    78     from twisted.internet import reactor
       
    79 
       
    80     root = resource.Resource()
       
    81     root = appendResource(root)
       
    82     reactor.listenTCP(7080, server.Site(root))
       
    83     reactor.run()