Skip to content

Commit 4b07805

Browse files
committed
minor improvements
1 parent fe83bee commit 4b07805

File tree

2 files changed

+13
-14
lines changed

2 files changed

+13
-14
lines changed

clients/python/src/osparc/_api_files_api.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,13 @@ async def download_file_async(
101101
url = urljoin(
102102
self.api_client.configuration.host, f"/v0/files/{file_id}/content"
103103
)
104-
with open(downloaded_file, mode="wb") as f:
105-
async for chunk in session.stream(
106-
"GET", url=url, auth=self._auth, follow_redirects=True
107-
):
108-
f.write(chunk)
104+
async for response in await session.stream(
105+
"GET", url=url, auth=self._auth, follow_redirects=True
106+
):
107+
response.raise_for_status()
108+
with open(downloaded_file, mode="wb") as f:
109+
async for chunk in response.aiter_bytes():
110+
f.write(chunk)
109111
return str(downloaded_file.resolve())
110112

111113
def upload_file(

clients/python/src/osparc/_http_client.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ async def _():
8888

8989
async def _stream(
9090
self, method: Literal["GET"], url: str, *args, **kwargs
91-
) -> AsyncGenerator[bytes, None]:
91+
) -> AsyncGenerator[httpx.Response, None]:
9292
n_attempts = self.configuration.retries.total
9393
assert isinstance(n_attempts, int)
9494

@@ -98,17 +98,15 @@ async def _stream(
9898
stop=tenacity.stop_after_attempt(n_attempts),
9999
retry=tenacity.retry_if_exception_type(httpx.HTTPStatusError),
100100
)
101-
async def _() -> AsyncGenerator[bytes, None]:
101+
async def _() -> AsyncGenerator[httpx.Response, None]:
102102
async with self._client.stream(
103103
method=method, url=url, *args, **kwargs
104104
) as response:
105105
if response.status_code in self.configuration.retries.status_forcelist:
106106
response.raise_for_status()
107-
async for chunk in response.aiter_bytes():
108-
yield chunk
107+
yield response
109108

110-
async for chunk in _():
111-
yield chunk
109+
return _()
112110

113111
async def put(self, *args, **kwargs) -> httpx.Response:
114112
return await self._request(self._client.put, *args, **kwargs)
@@ -127,9 +125,8 @@ async def get(self, *args, **kwargs) -> httpx.Response:
127125

128126
async def stream(
129127
self, method: Literal["GET"], url: str, *args, **kwargs
130-
) -> AsyncGenerator[bytes, None]:
131-
async for chunk in self._stream(method=method, url=url, *args, **kwargs):
132-
yield chunk
128+
) -> AsyncGenerator[httpx.Response, None]:
129+
return await self._stream(method=method, url=url, *args, **kwargs)
133130

134131
def _wait_callback(self, retry_state: tenacity.RetryCallState) -> int:
135132
assert retry_state.outcome is not None

0 commit comments

Comments
 (0)