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