annotate libervia/pages/_browser/bridge.py @ 1461:8ac062d2ff8b

pages(lists/view_item): fix node used: since we are now using XEP-0346, we must use the prefixed nodes for the `xmpp:` URI and for the determining ownership.
author Goffi <goffi@goffi.org>
date Thu, 30 Sep 2021 11:43:44 +0200
parents 8998f01088ac
children b28025a7cc28
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1297
999dccf0093e browser: new bridge module to access restricted bridge from browser
Goffi <goffi@goffi.org>
parents:
diff changeset
1 from browser import window
999dccf0093e browser: new bridge module to access restricted bridge from browser
Goffi <goffi@goffi.org>
parents:
diff changeset
2 import javascript
999dccf0093e browser: new bridge module to access restricted bridge from browser
Goffi <goffi@goffi.org>
parents:
diff changeset
3
999dccf0093e browser: new bridge module to access restricted bridge from browser
Goffi <goffi@goffi.org>
parents:
diff changeset
4
999dccf0093e browser: new bridge module to access restricted bridge from browser
Goffi <goffi@goffi.org>
parents:
diff changeset
5 class Bridge:
999dccf0093e browser: new bridge module to access restricted bridge from browser
Goffi <goffi@goffi.org>
parents:
diff changeset
6
999dccf0093e browser: new bridge module to access restricted bridge from browser
Goffi <goffi@goffi.org>
parents:
diff changeset
7 def __getattr__(self, attr):
999dccf0093e browser: new bridge module to access restricted bridge from browser
Goffi <goffi@goffi.org>
parents:
diff changeset
8 return lambda *args, **kwargs: self.call(attr, *args, **kwargs)
999dccf0093e browser: new bridge module to access restricted bridge from browser
Goffi <goffi@goffi.org>
parents:
diff changeset
9
999dccf0093e browser: new bridge module to access restricted bridge from browser
Goffi <goffi@goffi.org>
parents:
diff changeset
10 def on_load(self, xhr, ev, callback, errback):
999dccf0093e browser: new bridge module to access restricted bridge from browser
Goffi <goffi@goffi.org>
parents:
diff changeset
11 if xhr.status == 200:
999dccf0093e browser: new bridge module to access restricted bridge from browser
Goffi <goffi@goffi.org>
parents:
diff changeset
12 ret = javascript.JSON.parse(xhr.response)
1314
8998f01088ac browser (bridge): handle case where `callback` or `errback` is None
Goffi <goffi@goffi.org>
parents: 1297
diff changeset
13 if callback is not None:
8998f01088ac browser (bridge): handle case where `callback` or `errback` is None
Goffi <goffi@goffi.org>
parents: 1297
diff changeset
14 if ret is None:
8998f01088ac browser (bridge): handle case where `callback` or `errback` is None
Goffi <goffi@goffi.org>
parents: 1297
diff changeset
15 callback()
8998f01088ac browser (bridge): handle case where `callback` or `errback` is None
Goffi <goffi@goffi.org>
parents: 1297
diff changeset
16 else:
8998f01088ac browser (bridge): handle case where `callback` or `errback` is None
Goffi <goffi@goffi.org>
parents: 1297
diff changeset
17 callback(ret)
1297
999dccf0093e browser: new bridge module to access restricted bridge from browser
Goffi <goffi@goffi.org>
parents:
diff changeset
18 elif xhr.status == 502:
999dccf0093e browser: new bridge module to access restricted bridge from browser
Goffi <goffi@goffi.org>
parents:
diff changeset
19 # PROXY_ERROR is used for bridge error
999dccf0093e browser: new bridge module to access restricted bridge from browser
Goffi <goffi@goffi.org>
parents:
diff changeset
20 ret = javascript.JSON.parse(xhr.response)
1314
8998f01088ac browser (bridge): handle case where `callback` or `errback` is None
Goffi <goffi@goffi.org>
parents: 1297
diff changeset
21 if errback is not None:
8998f01088ac browser (bridge): handle case where `callback` or `errback` is None
Goffi <goffi@goffi.org>
parents: 1297
diff changeset
22 errback(ret)
1297
999dccf0093e browser: new bridge module to access restricted bridge from browser
Goffi <goffi@goffi.org>
parents:
diff changeset
23 else:
999dccf0093e browser: new bridge module to access restricted bridge from browser
Goffi <goffi@goffi.org>
parents:
diff changeset
24 print(f"bridge called failed: code: {xhr.response}, text: {xhr.statusText}")
1314
8998f01088ac browser (bridge): handle case where `callback` or `errback` is None
Goffi <goffi@goffi.org>
parents: 1297
diff changeset
25 if errback is not None:
8998f01088ac browser (bridge): handle case where `callback` or `errback` is None
Goffi <goffi@goffi.org>
parents: 1297
diff changeset
26 errback({"fullname": "InternalError", "message": xhr.statusText})
1297
999dccf0093e browser: new bridge module to access restricted bridge from browser
Goffi <goffi@goffi.org>
parents:
diff changeset
27
999dccf0093e browser: new bridge module to access restricted bridge from browser
Goffi <goffi@goffi.org>
parents:
diff changeset
28 def call(self, method_name, *args, callback, errback, **kwargs):
999dccf0093e browser: new bridge module to access restricted bridge from browser
Goffi <goffi@goffi.org>
parents:
diff changeset
29 xhr = window.XMLHttpRequest.new()
999dccf0093e browser: new bridge module to access restricted bridge from browser
Goffi <goffi@goffi.org>
parents:
diff changeset
30 xhr.bind('load', lambda ev: self.on_load(xhr, ev, callback, errback))
999dccf0093e browser: new bridge module to access restricted bridge from browser
Goffi <goffi@goffi.org>
parents:
diff changeset
31 xhr.bind('error', lambda ev: errback(
999dccf0093e browser: new bridge module to access restricted bridge from browser
Goffi <goffi@goffi.org>
parents:
diff changeset
32 {"fullname": "ConnectionError", "message": xhr.statusText}))
999dccf0093e browser: new bridge module to access restricted bridge from browser
Goffi <goffi@goffi.org>
parents:
diff changeset
33 xhr.open("POST", f"/_bridge/{method_name}", True)
999dccf0093e browser: new bridge module to access restricted bridge from browser
Goffi <goffi@goffi.org>
parents:
diff changeset
34 data = javascript.JSON.stringify({
999dccf0093e browser: new bridge module to access restricted bridge from browser
Goffi <goffi@goffi.org>
parents:
diff changeset
35 "args": args,
999dccf0093e browser: new bridge module to access restricted bridge from browser
Goffi <goffi@goffi.org>
parents:
diff changeset
36 "kwargs": kwargs,
999dccf0093e browser: new bridge module to access restricted bridge from browser
Goffi <goffi@goffi.org>
parents:
diff changeset
37 })
999dccf0093e browser: new bridge module to access restricted bridge from browser
Goffi <goffi@goffi.org>
parents:
diff changeset
38 xhr.setRequestHeader('X-Csrf-Token', window.csrf_token)
999dccf0093e browser: new bridge module to access restricted bridge from browser
Goffi <goffi@goffi.org>
parents:
diff changeset
39 xhr.send(data)