12 #along with this program; if not, see <http://www.gnu.org/licenses/>. |
12 #along with this program; if not, see <http://www.gnu.org/licenses/>. |
13 from twisted.web import soap, xmlrpc, resource, server |
13 from twisted.web import soap, xmlrpc, resource, server |
14 import logging |
14 import logging |
15 logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(name)s(%(processName)s)-%(levelname)s: %(message)s') |
15 logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(name)s(%(processName)s)-%(levelname)s: %(message)s') |
16 |
16 |
|
17 |
|
18 class User(object): |
|
19 def __init__(self,name,userhash): |
|
20 self.name=name |
|
21 self.userhash=userhash |
|
22 |
|
23 def __repr__(self): |
|
24 return"User<'%s','%s'>"%(self.name,self.userhash) |
|
25 |
|
26 users={"1":User("spam","1"), |
|
27 "2":User("foo","2") |
|
28 } |
|
29 |
|
30 def getuser(userhash): |
|
31 try: |
|
32 return users[userhash] |
|
33 except KeyError: |
|
34 raise UserNotFound() |
|
35 |
|
36 def with_user(f): |
|
37 def new_f(*args,**kargs): |
|
38 args=list(args) |
|
39 try: |
|
40 logging.debug("Entering %s"%f.__name__) |
|
41 try: |
|
42 kargs["user"]=getuser(kargs["apikey"]) |
|
43 del kargs["apikey"] |
|
44 except KeyError: |
|
45 kargs["user"]=getuser(args[1]) |
|
46 del args[1] |
|
47 ret=f(*args,**kargs) |
|
48 logging.debug("Exited %s"%f.__name__) |
|
49 return ret |
|
50 except InterfaceException, e: |
|
51 return e.dict() |
|
52 new_f.__name__ = f.__name__ |
|
53 return new_f |
|
54 |
|
55 |
|
56 class InterfaceException(Exception): |
|
57 def __init__(self, code=999, msg="Unbekannter Fehler."): |
|
58 self.code=code |
|
59 self.msg=msg |
|
60 |
|
61 def dict(self): |
|
62 return {"code":self.code, |
|
63 "msg":self.msg, |
|
64 } |
|
65 def __str__(self): |
|
66 return "%i:%s"%(self.code,self.msg) |
|
67 |
|
68 class UserNotFound(InterfaceException): |
|
69 def __init__(self): |
|
70 InterfaceException.__init__(self, 901, "Der API-Key ist ungültig.") |
|
71 |
|
72 class ExternalException(InterfaceException): |
|
73 def __init__(self): |
|
74 InterfaceException.__init__(self, 950, "Fehler in externer API.") |
|
75 |
|
76 |
17 class Interface(object): |
77 class Interface(object): |
18 '''class for a xmlrpc user |
78 '''class for a xmlrpc user |
19 ''' |
79 ''' |
20 |
80 |
21 def status(self, apikey, id=None, detailed=False): |
81 @with_user |
22 '''Gibt den aktuellen Status eines Auftrages zurück. |
82 def status(self, user, id=None, detailed=False): |
|
83 '''Gibt den aktuellen Status eines Auftrages oder Mehreren zurück. |
23 |
84 |
24 Keywords: |
85 Keywords: |
25 apikey[string]: Der API Key |
86 apikey[string]: Der API Key |
26 id[hash]: Eine Auftragsnummer |
87 id[hash]: Eine Auftragsnummer |
27 detailed[boolean]: Details ausgeben |
88 detailed[boolean]: Details ausgeben |
31 job.name[string]: Angebener Name |
92 job.name[string]: Angebener Name |
32 job.status[string]: Status des Auftrages |
93 job.status[string]: Status des Auftrages |
33 |
94 |
34 |
95 |
35 ''' |
96 ''' |
36 return "" |
97 #return user.status(id,detailed) |
37 |
98 return "" |
38 def stop(self, apikey,id): |
99 |
|
100 @with_user |
|
101 def stop(self, user, id): |
39 '''Stoppt den angegeben Auftrag. |
102 '''Stoppt den angegeben Auftrag. |
40 |
103 |
41 Keywords: |
104 Keywords: |
42 apikey[string]: Der API Key |
105 apikey[string]: Der API Key |
43 id[hash]: Eine Auftragsnummer |
106 id[hash]: Eine Auftragsnummer |
61 id[hash]: Die ID des Auftrages |
125 id[hash]: Die ID des Auftrages |
62 |
126 |
63 ''' |
127 ''' |
64 return "" |
128 return "" |
65 |
129 |
66 |
130 @with_user |
67 def fax(self, apikey, subject, fax, recipients, route="default"): |
131 def fax(self, user, subject, fax, recipients, route="default"): |
68 '''Versendet ein FAX. |
132 '''Versendet ein FAX. |
69 |
133 |
70 Keywords: |
134 Keywords: |
71 apikey[string]: Der API Key |
135 apikey[string]: Der API Key |
72 subject[string]: Der Betreff |
136 subject[string]: Der Betreff |
79 id[hash]: Die ID des Auftrages |
143 id[hash]: Die ID des Auftrages |
80 |
144 |
81 ''' |
145 ''' |
82 return "" |
146 return "" |
83 |
147 |
84 def mail(self, apikey, subject, body, recipients, frm, route="default"): |
148 @with_user |
|
149 def mail(self, user, subject, body, recipients, frm, route="default"): |
85 '''Versendet eine Email. |
150 '''Versendet eine Email. |
86 |
151 |
87 Keywords: |
152 Keywords: |
88 apikey[string]: Der API Key |
153 apikey[string]: Der API Key |
89 subject[string]: Der Betreff |
154 subject[string]: Der Betreff |
97 id[hash]: Die ID des Auftrages |
162 id[hash]: Die ID des Auftrages |
98 |
163 |
99 ''' |
164 ''' |
100 return "" |
165 return "" |
101 |
166 |
102 def routes(self, apikey, typ): |
167 @with_user |
|
168 def routes(self, user, typ): |
103 '''Gibt eine Liste aller verfügbaren Provider zurück. |
169 '''Gibt eine Liste aller verfügbaren Provider zurück. |
104 |
170 |
105 Keywords: |
171 Keywords: |
106 apikey[string]: Der API Key |
172 apikey[string]: Der API Key |
107 typ[string]: Der Typ zu dem die Providerloste ausgeben werden soll |
173 typ[string]: Der Typ zu dem die Providerloste ausgeben werden soll |
111 providerlist[list]: Eine Liste aller möglichen Provider |
177 providerlist[list]: Eine Liste aller möglichen Provider |
112 |
178 |
113 ''' |
179 ''' |
114 return "" |
180 return "" |
115 |
181 |
116 def defaultRoute(self, apikey, typ): |
182 @with_user |
|
183 def defaultRoute(self, user, typ): |
117 '''Gibt den Standardprovider zurück. |
184 '''Gibt den Standardprovider zurück. |
118 |
185 |
119 Keywords: |
186 Keywords: |
120 apikey[string]: Der API Key |
187 apikey[string]: Der API Key |
121 typ[string]: Der Typ zu dem die Providerloste ausgeben werden soll |
188 typ[string]: Der Typ zu dem die Providerloste ausgeben werden soll |
155 |
223 |
156 class XMLRPCInterface(Interface,xmlrpc.XMLRPC): |
224 class XMLRPCInterface(Interface,xmlrpc.XMLRPC): |
157 def __init__(self): |
225 def __init__(self): |
158 xmlrpc.XMLRPC.__init__(self) |
226 xmlrpc.XMLRPC.__init__(self) |
159 Interface.__init__(self) |
227 Interface.__init__(self) |
160 self.allowNone = True |
|
161 |
228 |
162 def lookupProcedure(self, procedurePath): |
229 def lookupProcedure(self, procedurePath): |
163 logging.debug("lookupProcedure('%s')"%procedurePath) |
230 logging.debug("lookupProcedure('%s')"%procedurePath) |
164 if procedurePath not in self.listProcedures(): |
231 if procedurePath not in self.listProcedures(): |
165 raise xmlrpc.NoSuchFunction(self.NOT_FOUND, |
232 raise xmlrpc.NoSuchFunction(self.NOT_FOUND, |