Skip to content

Commit 152fafc

Browse files
chore: add write_to_file binary helper method (#265)
1 parent 6d1df20 commit 152fafc

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

src/finch/_legacy_response.py

+16
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,22 @@ def iter_lines(self) -> Iterator[str]:
336336
def iter_raw(self, chunk_size: int | None = None) -> Iterator[bytes]:
337337
return self.response.iter_raw(chunk_size)
338338

339+
def write_to_file(
340+
self,
341+
file: str | os.PathLike[str],
342+
) -> None:
343+
"""Write the output to the given file.
344+
345+
Accepts a filename or any path-like object, e.g. pathlib.Path
346+
347+
Note: if you want to stream the data to the file instead of writing
348+
all at once then you should use `.with_streaming_response` when making
349+
the API request, e.g. `client.with_streaming_response.foo().stream_to_file('my_filename.txt')`
350+
"""
351+
with open(file, mode="wb") as f:
352+
for data in self.response.iter_bytes():
353+
f.write(data)
354+
339355
@deprecated(
340356
"Due to a bug, this method doesn't actually stream the response content, `.with_streaming_response.method()` should be used instead"
341357
)

0 commit comments

Comments
 (0)