From dc1693a88d1f25957f5842642667d1c589556802 Mon Sep 17 00:00:00 2001 From: Primoz Godec Date: Fri, 1 Jul 2022 14:31:39 +0200 Subject: [PATCH] server embedder - send bytes as content --- Orange/misc/server_embedder.py | 8 +++++--- Orange/misc/tests/test_server_embedder.py | 3 ++- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/Orange/misc/server_embedder.py b/Orange/misc/server_embedder.py index 4c625b830aa..07bf09550fa 100644 --- a/Orange/misc/server_embedder.py +++ b/Orange/misc/server_embedder.py @@ -8,7 +8,7 @@ from functools import partial from json import JSONDecodeError from os import getenv -from typing import Any, Callable, List, Optional +from typing import Any, Callable, List, Optional, Dict, Union from AnyQt.QtCore import QSettings from httpx import AsyncClient, NetworkError, ReadTimeout, Response @@ -306,7 +306,7 @@ async def _send_to_server( queue.task_done() async def _send_request( - self, client: AsyncClient, data: bytes, url: str + self, client: AsyncClient, data: Union[bytes, Dict], url: str ) -> Optional[List[float]]: """ This function sends a single request to the server. @@ -331,7 +331,9 @@ async def _send_request( "Content-Length": str(len(data)), } try: - response = await client.post(url, headers=headers, data=data) + # bytes are sent as content parameter and dictionary as data + kwargs = dict(content=data) if isinstance(data, bytes) else dict(data=data) + response = await client.post(url, headers=headers, **kwargs) except ReadTimeout as ex: log.debug("Read timeout", exc_info=True) # it happens when server do not respond in time defined by timeout diff --git a/Orange/misc/tests/test_server_embedder.py b/Orange/misc/tests/test_server_embedder.py index d1c6cd53bf1..5df61cb5fac 100644 --- a/Orange/misc/tests/test_server_embedder.py +++ b/Orange/misc/tests/test_server_embedder.py @@ -27,10 +27,11 @@ def __init__(self, content): def make_dummy_post(response): @staticmethod # pylint: disable=unused-argument - async def dummy_post(url, headers, data): + async def dummy_post(url, headers, content=None, data=None): # when sleeping some workers to still compute while other are done # it causes that not all embeddings are computed if we do not wait all # workers to finish + assert (content is None) ^ (data is None) await asyncio.sleep(random() / 10) return DummyResponse(content=response)