Ver Fonte

Merge remote-tracking branch 'origin/on_air_post' into on-air

Rodja Trappe há 2 anos atrás
pai
commit
a219770887
3 ficheiros alterados com 21 adições e 12 exclusões
  1. 19 10
      nicegui/air.py
  2. 1 1
      nicegui/templates/index.html
  3. 1 1
      tests/conftest.py

+ 19 - 10
nicegui/air.py

@@ -16,23 +16,32 @@ class Air:
     def __init__(self, token: str) -> None:
         self.token = token
         self.relay = AsyncClient()
-        self.client = httpx.AsyncClient(app=globals.app, base_url="http://test")
-
-        @self.relay.on('get')
-        async def on_get(data: Dict[str, Any]) -> Dict[str, Any]:
-            headers = {'Accept-Encoding': 'identity', 'X-Forwarded-Prefix': data['prefix']}
-            response = await self.client.get(data['path'], headers=headers)
+        self.client = httpx.AsyncClient(app=globals.app)
+
+        @self.relay.on('http')
+        async def on_http(data: Dict[str, Any]) -> Dict[str, Any]:
+            headers: Dict[str, Any] = data['headers']
+            headers.update({'Accept-Encoding': 'identity', 'X-Forwarded-Prefix': data['prefix']})
+            url = 'http://test' + data['path']
+            request = self.client.build_request(
+                data['method'],
+                url,
+                params=data['params'],
+                headers=headers,
+                content=data['body'],
+            )
+            response = await self.client.send(request)
             content = response.content.replace(
                 b'const extraHeaders = {};',
                 (f'const extraHeaders = {{ "fly-force-instance-id" : "{data["instance-id"]}" }};').encode(),
             )
-            headers = dict(response.headers)
-            headers['content-encoding'] = 'gzip'
+            response_headers = dict(response.headers)
+            response_headers['content-encoding'] = 'gzip'
             compressed = gzip.compress(content)
-            headers['content-length'] = str(len(compressed))
+            response_headers['content-length'] = str(len(compressed))
             return {
                 'status_code': response.status_code,
-                'headers': headers,
+                'headers': response_headers,
                 'content': compressed,
             }
 

+ 1 - 1
nicegui/templates/index.html

@@ -154,7 +154,7 @@
           const query = { client_id: "{{ client_id }}" };
           const url = window.location.protocol === 'https:' ? 'wss://' : 'ws://' + window.location.host;
           const extraHeaders = {{ socket_io_js_extra_headers | safe }};
-          const transports = ['websocket', 'polling']
+          const transports = ['websocket', 'polling'];
           window.path_prefix = "{{ prefix | safe }}";
           window.socket = io(url, { path: "{{ prefix | safe }}/_nicegui_ws/socket.io", query, extraHeaders, transports });
           window.socket.on("connect", () => {

+ 1 - 1
tests/conftest.py

@@ -36,7 +36,7 @@ def selenium(selenium: webdriver.Chrome) -> webdriver.Chrome:
 
 
 @pytest.fixture(autouse=True)
-def reset_globals() -> Generator[None, None, None]:
+def reset_globals() -> None:
     for path in {'/'}.union(globals.page_routes.values()):
         globals.app.remove_route(path)
     importlib.reload(globals)