# HG changeset patch # User Sandro Knauß # Date 1348748403 -7200 # Node ID a73bbc1d8b4bcc962d005d105d6d2998679debdf # Parent dc3cc61c7f6f4dd6a8dc42111e6041e0a13ace6f adding jsonrpc interface diff -r dc3cc61c7f6f -r a73bbc1d8b4b iro/iro.py --- a/iro/iro.py Thu Sep 27 14:13:06 2012 +0200 +++ b/iro/iro.py Thu Sep 27 14:20:03 2012 +0200 @@ -28,7 +28,7 @@ from sqlalchemy import create_engine, pool import config, install -from .view import xmlrpc +from .view import xmlrpc, jsonrpc from .model import setEngine, setPool from .controller.pool import startPool, dbPool @@ -67,10 +67,12 @@ raise Exception("offerlist is not up-to-date.\nPlease run iro-install --update") root = resource.Resource() - root = xmlrpc.appendResource(root) + xmlrpc.appendResource(root) + jsonrpc.appendResource(root) v2 = resource.Resource() - v2 = xmlrpc.appendResource(root) + xmlrpc.appendResource(v2) + jsonrpc.appendResource(v2) root.putChild('1.0a', v2) internet.TCPServer(config.main.port, server.Site(root)).setServiceParent(top_service) diff -r dc3cc61c7f6f -r a73bbc1d8b4b iro/main.py --- a/iro/main.py Thu Sep 27 14:13:06 2012 +0200 +++ b/iro/main.py Thu Sep 27 14:20:03 2012 +0200 @@ -27,7 +27,7 @@ from .model import setEngine, setPool from .controller.pool import startPool, dbPool -from .view import xmlrpc +from .view import xmlrpc, jsonrpc from . import config def runReactor(reactor, engine, port, root): @@ -56,10 +56,12 @@ root = resource.Resource() - root = xmlrpc.appendResource(root) + xmlrpc.appendResource(root) + jsonrpc.appendResource(root) v2 = resource.Resource() - v2 = xmlrpc.appendResource(root) - root.putChild('2.0', v2) + xmlrpc.appendResource(v2) + jsonrpc.pappendResource(v2) + root.putChild('1.0a', v2) runReactor(reactor, engine, config.main.port, root) diff -r dc3cc61c7f6f -r a73bbc1d8b4b iro/tests/jsonrpc.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/iro/tests/jsonrpc.py Thu Sep 27 14:20:03 2012 +0200 @@ -0,0 +1,279 @@ +# Copyright (c) 2012 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. + +import unittest +from twisted.internet import reactor, defer +from twisted.web import server +from txjsonrpc.web import jsonrpc as txjsonrpc +from txjsonrpc.web.jsonrpc import Fault +from txjsonrpc import jsonrpclib + +from datetime import datetime + +from iro.model.schema import User, Offer, Userright, Job, Message + +from iro.view import jsonrpc +import iro.error as IroError + +from iro.test_helpers.dbtestcase import DBTestCase + +from iro.model import setEngine, setPool +from iro.controller.pool import startPool, dbPool + + +class JSONRPCTest(DBTestCase): + """tests for the jsonrpc interface""" + def setUp(self): + DBTestCase.setUp(self) + + setEngine(self.engine) + startPool(reactor) + setPool(dbPool) + self.p = reactor.listenTCP(0, server.Site(jsonrpc.JSONRPCInterface()), + interface="127.0.0.1") + self.port = self.p.getHost().port + + def tearDown(self): + DBTestCase.tearDown(self) + return self.p.stopListening() + + def proxy(self): + return txjsonrpc.Proxy("http://127.0.0.1:%d/" % self.port,version=jsonrpclib.VERSION_2) + + def testListMethods(self): + '''list of all offical Methods, that can be executed''' + + def cbMethods(ret): + ret.sort() + self.failUnlessEqual(ret, ['bill', 'defaultRoute', 'email', 'fax', 'listMethods', 'mail', 'routes', 'sms', 'status', 'telnumber']) + + d=self.proxy().callRemote("listMethods") + d.addCallback(cbMethods) + return d + + def testApikey(self): + ''' test apikey''' + with self.session() as session: + session.add(User(name='test',apikey='abcdef123456789')) + + d = self.proxy().callRemote('status', 'abcdef123456789') + d.addCallback(lambda ret: self.failUnlessEqual(ret,{})) + + return d + + def testStatus(self): + ''' test the status function''' + with self.session() as session: + u = User(name='test',apikey='abcdef123456789') + session.add(u) + j = Job(info='info', status="started") + j.user=u + session.add(j) + session.commit() + jid=j.id + status = {str(jid):{"status":"started"}} + args=[('abcdef123456789',jid), + ('abcdef123456789',), + ('abcdef123456789', '', 'false'), + ('abcdef123456789', '', 0), + ] + dl=[] + for a in args: + d = self.proxy().callRemote('status',*a) + d.addCallback(lambda ret: self.failUnlessEqual(ret,status)) + dl.append(d) + + def f(exc): + jnf = IroError.JobNotFound() + self.failUnlessEqual(exc.faultCode, jnf.code) + self.failUnlessEqual(exc.faultString, jnf.msg) + d = self.proxy().callRemote('status', 'abcdef123456789', jid+1) + d = self.assertFailure(d, Fault) + d.addCallback(f) + dl.append(d) + + return defer.DeferredList(dl, fireOnOneErrback=True) + + def testNoSuchUser(self): + '''a unknown user should raise a UserNotNound Exception + bewcause xmlrpc only has a Fault exception this Exception has to be deliverd through a xmlrpclib.Fault Exception''' + def f(exc): + unf = IroError.UserNotFound() + self.failUnlessEqual(exc.faultCode, unf.code) + self.failUnlessEqual(exc.faultString, unf.msg) + d = self.proxy().callRemote('status', 'abcdef123456789') + d = self.assertFailure(d, Fault) + d.addCallback(f) + return d + + def testNoSuchMethod(self): + '''a unknown mothod should raise a Exception ''' + def f(exc): + self.failUnlessEqual(exc.faultCode, -32601) + self.failUnlessEqual(exc.faultString, 'procedure nosuchmethod not found') + d = self.proxy().callRemote('nosuchmethod') + d = self.assertFailure(d, Fault) + d.addCallback(f) + return d + + def testValidationFault(self): + '''a validate Exception should be translated to a xmlrpclib.Fault.''' + def f(exc): + self.failUnlessEqual(exc.faultCode, 700) + self.failUnlessEqual(exc.faultString, "Validation of 'apikey' failed.") + d = self.proxy().callRemote('status','xxx') + d = self.assertFailure(d, Fault) + d.addCallback(f) + return d + + @defer.inlineCallbacks + def testRoutes(self): + '''test the route function''' + with self.session() as session: + u=User(name='test',apikey='abcdef123456789') + o=Offer(name="sipgate_basic", provider="sipgate", route="basic", typ="sms") + u.rights.append(Userright(o)) + session.add(u) + + x = yield self.proxy().callRemote('routes','abcdef123456789','sms') + self.failUnlessEqual(x,['sipgate_basic']) + + def f(exc): + self.failUnlessEqual(exc.faultCode, 700) + self.failUnlessEqual(exc.faultString, "Typ fax is not valid.") + d = self.proxy().callRemote('routes','abcdef123456789','fax') + d = self.assertFailure(d, Fault) + d.addCallback(f) + yield d + + with self.session() as session: + o=Offer(name="sipgate_plus", provider="sipgate", route="plus", typ="sms") + u = session.query(User).filter_by(name="test").first() + u.rights.append(Userright(o)) + o=Offer(name="faxde", provider="faxde", route="", typ="fax") + session.add(o) + session.commit() + + x = yield self.proxy().callRemote('routes','abcdef123456789','sms') + self.failUnlessEqual(x, ['sipgate_basic','sipgate_plus']) + x = yield self.proxy().callRemote('routes','abcdef123456789','fax') + self.failUnlessEqual(x, []) + + with self.session() as session: + u = session.query(User).filter_by(name="test").first() + u.rights.append(Userright(o)) + + x = yield self.proxy().callRemote('routes','abcdef123456789','sms') + self.failUnlessEqual(x, ['sipgate_basic','sipgate_plus']) + x = yield self.proxy().callRemote('routes','abcdef123456789','fax') + self.failUnlessEqual(x, ['faxde']) + + def testDefaultRoutes(self): + '''test the defaultRoute function''' + with self.session() as session: + u=User(name='test',apikey='abcdef123456789') + o=Offer(name="sipgate_basic", provider="sipgate", route="basic", typ="sms") + u.rights.append(Userright(o,True)) + o=Offer(name="sipgate_plus", provider="sipgate", route="plus", typ="sms") + u.rights.append(Userright(o)) + session.add(u) + d = self.proxy().callRemote('defaultRoute','abcdef123456789','sms') + d.addCallback(lambda x: self.failUnlessEqual(x,['sipgate_basic'])) + + return d + + def testTelnumbers(self): + '''test the telefon validator''' + dl = [] + d = self.proxy().callRemote('telnumber',["0123/456(78)","+4912346785433","00123435456-658"]) + d.addCallback(lambda x: self.failUnlessEqual(x,True)) + dl.append(d) + + invalid=['xa','+1','1-23',';:+0','0123'] + def f(exc): + self.failUnlessEqual(exc.faultCode, 701) + self.failUnlessEqual(exc.faultString, "No valid telnumber: '%s'" % invalid[0]) + d = self.proxy().callRemote('telnumber', ['01234']+invalid) + d = self.assertFailure(d, Fault) + d.addCallback(f) + dl.append(d) + + return defer.DeferredList(dl, fireOnOneErrback=True) + + def testVaildEmail(self): + '''test vaild email adresses (got from wikipedia)''' + validmails=["niceandsimple@example.com"] + d = self.proxy().callRemote('email', validmails) + d.addCallback(lambda x: self.failUnlessEqual(x,True)) + return d + + def testInvaildEmail(self): + '''test invaild email adresses (got from wikipedia)''' + invalid=["Abc.example.com",'foo@t.de'] + def f(exc): + self.failUnlessEqual(exc.faultCode, 702) + self.failUnlessEqual(exc.faultString, "No valid email: '%s'" % invalid[0]) + d = self.proxy().callRemote('email', invalid) + d = self.assertFailure(d, Fault) + d.addCallback(f) + return d + + def testBill(self): + '''test bill function''' + apikey='abcdef123456789' + with self.session() as session: + u=User(name='test',apikey=apikey) + session.add(u) + d = self.proxy().callRemote('bill', apikey) + d.addCallback(lambda x: self.failUnlessEqual(x,{'total':{'price':0.0,'anz':0}})) + + return d + + def testBillWithPrice(self): + apikey='abcdef123456789' + with self.session() as session: + u=User(name='test',apikey=apikey) + session.add(u) + o = Offer(name='sipgate_basic',provider="sipgate",route="basic",typ="sms") + u.rights.append(Userright(o)) + j = Job(info='i',status='sended') + j.messages.append(Message(recipient='0123456789', isBilled=False, date=datetime.now() , price=0.4, offer=o)) + u.jobs.append(j) + + j = Job(info='a',status='sended') + j.messages.append(Message(recipient='0123456789', isBilled=False, date=datetime.now(), price=0.4, offer=o)) + u.jobs.append(j) + + def f(ret): + self.failUnlessEqual(ret['total'],{'price':0.8,'anz':2}) + self.failUnlessEqual(ret['sipgate_basic'], + {'price':0.8,'anz':2, + 'info':{'i':{'price':0.4,'anz':1}, + 'a':{'price':0.4,'anz':1}, + } + }) + + d = self.proxy().callRemote('bill', apikey) + d.addCallback(f) + return d + +if __name__ == '__main__': + unittest.main() diff -r dc3cc61c7f6f -r a73bbc1d8b4b iro/view/jsonrpc.py --- /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 +# +# 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() diff -r dc3cc61c7f6f -r a73bbc1d8b4b iro/view/xmlrpc.py --- a/iro/view/xmlrpc.py Thu Sep 27 14:13:06 2012 +0200 +++ b/iro/view/xmlrpc.py Thu Sep 27 14:20:03 2012 +0200 @@ -86,7 +86,6 @@ """adding XML-RPC and SOAP to root.""" root.putChild('RPC2', XMLRPCInterface()) root.putChild('SOAP', SOAPInterface()) - return root if __name__ == '__main__': from twisted.web import resource, server