start to deal with xmlrpclib errors
[myslice.git] / manifold / manifoldapi.py
1 # Manifold API Python interface
2 import xmlrpclib
3
4 from myslice.config import Config
5
6 debug=True
7
8 class SessionExpired (Exception):
9     def __init__ (self,message):
10         self.message=message
11     def __repr__ (self): 
12         return "<SessionExpired: %s>"%self.message
13
14 class ManifoldAPI:
15
16     def __init__(self, auth=None, cainfo=None):
17         
18         config = Config()
19         self.auth = auth
20         self.cainfo = cainfo
21         self.errors = []
22         self.trace = []
23         self.calls = {}
24         self.multicall = False
25         self.url = config.manifold_url
26         self.server = xmlrpclib.Server(self.url, verbose=False, allow_none=True)
27
28     def __getattr__(self, methodName):
29         def func(*args, **kwds):
30             try:
31                 result=getattr(self.server, methodName)(self.auth, *args, **kwds)
32                 if debug:
33                     print '===> backend call',methodName, self.auth, self.url,'->',
34                     if not result:                        print "no/empty result"
35                     elif isinstance (result,str):         print "result is '%s'"%result
36                     elif isinstance (result,list):        print "result is a %d-elts list"%len(result)
37                     else:                                 print "dont know how to display result"
38                 return result
39             except xmlrpclib.Fault, error:
40                 ### xxx this is very rough for now
41                 # until we have some agreement about how the API calls should return error conditions
42                 # in some less unpolite way than this anoanymous exception, we assume it's a problem with the session
43                 # that needs to be refreshed
44                 if error.faultCode == 8002:
45                     reason="most likely your session has expired"
46                     reason += " (the manifold API has no unambiguous error reporting mechanism yet)"
47                     raise SessionExpired(reason)
48             except Exception,error:
49                 print "ManifoldAPI: unexpected exception",error
50                 raise
51         return func
52
53     def send_manifold_query (self, manifold_query):
54         (action,subject)= (manifold_query.action,manifold_query.subject)
55         if action=='get':
56             # use self.Get rather than self.server.Get so we catch exceptions as per __getattr__
57             return self.Get(self.auth, subject, manifold_query.filters, {}, manifold_query.fields)
58         # xxx...
59         else:
60             print "WARNING: ManifoldAPI.send_manifold_query: only 'get' implemented for now"