Skip to content

Commit 7a6cc92

Browse files
🐛 fix deserialization issue of pages (#66)
* fix deserialization issue of pages * add proper base url to async http client context manager
1 parent ad0c87c commit 7a6cc92

File tree

4 files changed

+24
-20
lines changed

4 files changed

+24
-20
lines changed

clients/python/client/osparc/_files_api.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,10 @@ async def upload_file_async(self, file: Union[str, Path]) -> File:
9191

9292
tasks: list = []
9393
async with AsyncHttpClient(
94-
exception_request_type="post",
95-
exception_url=links.abort_upload,
96-
exception_auth=self._auth,
94+
base_url=httpx.URL(self.api_client.configuration.host),
95+
request_type="post",
96+
url=links.abort_upload,
97+
auth=self._auth,
9798
) as session:
9899
async for chunck, size in _file_chunk_generator(file, chunk_size):
99100
# following https://docs.python.org/3/library/asyncio-task.html#asyncio.create_task

clients/python/client/osparc/_http_client.py

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,17 @@ class AsyncHttpClient:
99

1010
def __init__(
1111
self,
12-
exception_request_type: Optional[str] = None,
13-
exception_url: Optional[str] = None,
14-
exception_auth: Optional[httpx.BasicAuth] = None,
12+
*,
13+
base_url: httpx.URL,
14+
request_type: Optional[str] = None,
15+
url: Optional[str] = None,
16+
auth: Optional[httpx.BasicAuth] = None,
1517
):
16-
self._client = httpx.AsyncClient()
17-
self._exc_callback = (
18-
getattr(self._client, exception_request_type)
19-
if exception_request_type
20-
else None
18+
self._client = httpx.AsyncClient(
19+
base_url=base_url, auth=auth, follow_redirects=True
2120
)
22-
self._exc_url = exception_url
23-
self._exc_auth = exception_auth
21+
self._callback = getattr(self._client, request_type) if request_type else None
22+
self._url = url
2423

2524
async def __aenter__(self) -> httpx.AsyncClient:
2625
return self._client
@@ -29,7 +28,7 @@ async def __aexit__(self, exc_type, exc_value, traceback) -> None:
2928
if exc_value is None:
3029
await self._client.aclose()
3130
else: # exception raised: need to handle
32-
if self._exc_callback is not None:
31+
if self._callback is not None:
3332
try:
3433
async for attempt in tenacity.AsyncRetrying(
3534
reraise=True,
@@ -38,9 +37,7 @@ async def __aexit__(self, exc_type, exc_value, traceback) -> None:
3837
retry=tenacity.retry_if_exception_type(httpx.RequestError),
3938
):
4039
with attempt:
41-
response = await self._exc_callback(
42-
self._exc_url, auth=self._exc_auth
43-
)
40+
response = await self._callback(self._url)
4441
response.raise_for_status()
4542
except Exception as err:
4643
await self._client.aclose()

clients/python/client/osparc/_solvers_api.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ def pagination_method():
5151
)
5252

5353
return PaginationGenerator(
54-
pagination_method,
54+
first_page_callback=pagination_method,
55+
api_client=self.api_client,
5556
base_url=self.api_client.configuration.host,
5657
auth=self._auth,
5758
)

clients/python/client/osparc/_utils.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import httpx
66
from osparc_client import (
7+
ApiClient,
78
File,
89
Job,
910
PageFile,
@@ -24,12 +25,16 @@ class PaginationGenerator:
2425
def __init__(
2526
self,
2627
first_page_callback: Callable[[], Page],
28+
api_client: ApiClient,
2729
base_url: str,
2830
auth: Optional[httpx.BasicAuth],
2931
):
3032
self._first_page_callback: Callable[[], Page] = first_page_callback
33+
self._api_client: ApiClient = api_client
3134
self._next_page_url: Optional[str] = None
32-
self._client: httpx.Client = httpx.Client(auth=auth, base_url=base_url)
35+
self._client: httpx.Client = httpx.Client(
36+
auth=auth, base_url=base_url, follow_redirects=True
37+
)
3338

3439
def __del__(self):
3540
self._client.close()
@@ -52,7 +57,7 @@ def __iter__(self) -> Generator[T, None, None]:
5257
if page.links.next is None:
5358
break
5459
response: httpx.Response = self._client.get(page.links.next)
55-
page = type(page)(**response.json())
60+
page = self._api_client._ApiClient__deserialize(response.json(), type(page))
5661

5762

5863
async def _file_chunk_generator(

0 commit comments

Comments
 (0)