comparison libervia.py @ 203:5fdea93b2541

browser side: errback management in bridge calls
author Goffi <goffi@goffi.org>
date Thu, 20 Jun 2013 12:16:46 +0200
parents 2bc6cf004e61
children 890776a6fdb7
comparison
equal deleted inserted replaced
202:2bc6cf004e61 203:5fdea93b2541
39 class LiberviaJsonProxy(JSONProxy): 39 class LiberviaJsonProxy(JSONProxy):
40 def __init__(self, *args, **kwargs): 40 def __init__(self, *args, **kwargs):
41 JSONProxy.__init__(self, *args, **kwargs) 41 JSONProxy.__init__(self, *args, **kwargs)
42 self.handler=self 42 self.handler=self
43 self.cb={} 43 self.cb={}
44 self.eb={}
44 45
45 def call(self, method, cb, *args): 46 def call(self, method, cb, *args):
46 id = self.callMethod(method,args) 47 _id = self.callMethod(method,args)
47 if cb: 48 if cb:
48 self.cb[id] = cb 49 if isinstance(cb, tuple):
50 if len(cb) != 2:
51 print ("ERROR: tuple syntax for bridge.call is (callback, errback), aborting")
52 return
53 self.cb[_id] = cb[0]
54 self.eb[_id] = cb[1]
55 else:
56 self.cb[_id] = cb
49 57
50 def onRemoteResponse(self, response, request_info): 58 def onRemoteResponse(self, response, request_info):
51 if self.cb.has_key(request_info.id): 59 if request_info.id in self.cb:
52 _cb = self.cb[request_info.id] 60 _cb = self.cb[request_info.id]
53 if isinstance(_cb, tuple): 61 # if isinstance(_cb, tuple):
54 #we have arguments attached to the callback 62 # #we have arguments attached to the callback
55 #we send them after the answer 63 # #we send them after the answer
56 callback, args = _cb 64 # callback, args = _cb
57 callback(response, *args) 65 # callback(response, *args)
58 else: 66 # else:
59 #No additional argument, we call directly the callback 67 # #No additional argument, we call directly the callback
60 _cb(response) 68 _cb(response)
61 del self.cb[request_info.id] 69 del self.cb[request_info.id]
70 if request_info.id in self.eb:
71 del self.eb[request_info.id]
62 72
63 def onRemoteError(self, code, errobj, request_info): 73 def onRemoteError(self, code, errobj, request_info):
64 """def dump(obj): 74 """def dump(obj):
65 print "\n\nDUMPING %s\n\n" % obj 75 print "\n\nDUMPING %s\n\n" % obj
66 for i in dir(obj): 76 for i in dir(obj):
67 print "%s: %s" % (i, getattr(obj,i))""" 77 print "%s: %s" % (i, getattr(obj,i))"""
68 if code != 0: 78 if request_info.id in self.eb:
69 print ("Internal server error") 79 _eb = self.eb[request_info.id]
70 """for o in code, error, request_info: 80 _eb((code, errobj))
71 dump(o)""" 81 del self.cb[request_info.id]
82 del self.eb[request_info.id]
72 else: 83 else:
73 if isinstance(errobj['message'],dict): 84 if code != 0:
74 print("Error %s: %s" % (errobj['message']['faultCode'], errobj['message']['faultString'])) 85 print ("Internal server error")
86 """for o in code, error, request_info:
87 dump(o)"""
75 else: 88 else:
76 print("Error: %s" % errobj['message']) 89 if isinstance(errobj['message'],dict):
90 print("Error %s: %s" % (errobj['message']['faultCode'], errobj['message']['faultString']))
91 else:
92 print("Error: %s" % errobj['message'])
77 93
78 class RegisterCall(LiberviaJsonProxy): 94 class RegisterCall(LiberviaJsonProxy):
79 def __init__(self): 95 def __init__(self):
80 LiberviaJsonProxy.__init__(self, "/register_api", 96 LiberviaJsonProxy.__init__(self, "/register_api",
81 ["isRegistered","isConnected","connect"]) 97 ["isRegistered","isConnected","connect"])