Skip to content

Commit 8f2e0d1

Browse files
committed
Add test utility for handling file downloads
1 parent ad7e578 commit 8f2e0d1

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

sdk/batch/azure-batch/tests/async_wrapper.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
from collections.abc import AsyncIterable
21
import inspect
3-
from typing import Any, cast, Coroutine, Iterable, TypeVar, Union
2+
from typing import AsyncIterable, AsyncIterator, Any, cast, Coroutine, Iterable, Iterator, TypeVar, Union
43

54
T = TypeVar("T")
65

@@ -12,11 +11,23 @@ async def wrap_result(result: Union[T, Coroutine[Any, Any, T]]) -> T:
1211
return cast(T, result)
1312

1413
async def wrap_list_result(result: Union[Iterable[T], AsyncIterable[T]]) -> Iterable[T]:
15-
"""Handle a list operation result and convert to a list if it's an AsyncIterable"""
14+
"""Handle a list operation result and convert to a Iterable if it's async"""
1615
if isinstance(result, AsyncIterable):
1716
items: Iterable[T] = []
1817
async for item in result:
1918
items.append(item)
2019
return items
2120

2221
return result
22+
23+
async def wrap_file_result(result: Union[Iterator[bytes], Coroutine[Any, Any, AsyncIterator[bytes]]]) -> Iterator[bytes]:
24+
"""Handle a file download operation result and convert to an Iterator if it's async"""
25+
if inspect.iscoroutine(result):
26+
iterator: AsyncIterator[bytes] = await result
27+
chunks = []
28+
async for chunk in iterator:
29+
chunks.append(chunk)
30+
return iter(chunks)
31+
if isinstance(result, Iterator):
32+
return result
33+
raise TypeError(f"Cannot wrap file operation result. Unexpected result type: {result.__class__}")

0 commit comments

Comments
 (0)