File tree Expand file tree Collapse file tree 1 file changed +16
-0
lines changed Expand file tree Collapse file tree 1 file changed +16
-0
lines changed Original file line number Diff line number Diff 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 )
You can’t perform that action at this time.
0 commit comments