Mercurial > libervia-web
annotate libervia/pages/_browser/bridge.py @ 1422:027f5059e1e2
pages (lists/view_item): browser part was not in the repository
author | Goffi <goffi@goffi.org> |
---|---|
date | Sat, 01 May 2021 19:05:05 +0200 |
parents | 8998f01088ac |
children | b28025a7cc28 |
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) |