Skip to content

feat(client): support passing chunk size for binary responses #175

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions src/finch/_base_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1727,9 +1727,14 @@ def iter_raw(self, chunk_size: Optional[int] = None) -> Iterator[bytes]:
return self.response.iter_raw(chunk_size)

@override
def stream_to_file(self, file: str | os.PathLike[str]) -> None:
def stream_to_file(
self,
file: str | os.PathLike[str],
*,
chunk_size: int | None = None,
) -> None:
with open(file, mode="wb") as f:
for data in self.response.iter_bytes():
for data in self.response.iter_bytes(chunk_size):
f.write(data)

@override
Expand Down Expand Up @@ -1757,10 +1762,15 @@ async def aiter_raw(self, chunk_size: Optional[int] = None) -> AsyncIterator[byt
return self.response.aiter_raw(chunk_size)

@override
async def astream_to_file(self, file: str | os.PathLike[str]) -> None:
async def astream_to_file(
self,
file: str | os.PathLike[str],
*,
chunk_size: int | None = None,
) -> None:
path = anyio.Path(file)
async with await path.open(mode="wb") as f:
async for data in self.response.aiter_bytes():
async for data in self.response.aiter_bytes(chunk_size):
await f.write(data)

@override
Expand Down
15 changes: 13 additions & 2 deletions src/finch/_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,12 @@ def iter_raw(self, chunk_size: Optional[int] = None) -> Iterator[bytes]:
pass

@abstractmethod
def stream_to_file(self, file: str | PathLike[str]) -> None:
def stream_to_file(
self,
file: str | PathLike[str],
*,
chunk_size: int | None = None,
) -> None:
"""
Stream the output to the given file.
"""
Expand Down Expand Up @@ -172,7 +177,13 @@ async def aiter_raw(self, chunk_size: Optional[int] = None) -> AsyncIterator[byt
"""
pass

async def astream_to_file(self, file: str | PathLike[str]) -> None:
@abstractmethod
async def astream_to_file(
self,
file: str | PathLike[str],
*,
chunk_size: int | None = None,
) -> None:
"""
Stream the output to the given file.
"""
Expand Down