diff --git a/OpenAI-DotNet-Tests/TestFixture_08_Files.cs b/OpenAI-DotNet-Tests/TestFixture_08_Files.cs index 734ab670..991661bb 100644 --- a/OpenAI-DotNet-Tests/TestFixture_08_Files.cs +++ b/OpenAI-DotNet-Tests/TestFixture_08_Files.cs @@ -80,5 +80,21 @@ public async Task Test_04_DeleteFiles() Assert.IsNotNull(fileList); Assert.IsEmpty(fileList); } + + [Test] + public async Task Test_05_RetrieveFileStreamAsync() + { + Assert.IsNotNull(OpenAIClient.FilesEndpoint); + var fileList = await OpenAIClient.FilesEndpoint.ListFilesAsync(); + + Assert.IsNotNull(fileList); + Assert.IsNotEmpty(fileList); + + var testFileData = fileList[0]; + var result = await OpenAIClient.FilesEndpoint.RetrieveFileStreamAsync(testFileData); + + Assert.IsNotNull(result); + Assert.IsTrue(result.CanRead); + } } } diff --git a/OpenAI-DotNet/Files/FilesEndpoint.cs b/OpenAI-DotNet/Files/FilesEndpoint.cs index 4254407d..35f548f8 100644 --- a/OpenAI-DotNet/Files/FilesEndpoint.cs +++ b/OpenAI-DotNet/Files/FilesEndpoint.cs @@ -186,10 +186,21 @@ public async Task DownloadFileAsync(FileResponse fileData, string direct } } - await using var response = await client.Client.GetStreamAsync(GetUrl($"/{fileData.Id}/content"), cancellationToken).ConfigureAwait(false); + await using var response = await RetrieveFileStreamAsync(fileData, cancellationToken).ConfigureAwait(false); await using var fileStream = new FileStream(filePath, FileMode.CreateNew); await response.CopyToAsync(fileStream, cancellationToken).ConfigureAwait(false); return filePath; } + + /// + /// Gets the specified file as stream + /// + /// to download. + /// Optional, + /// The file as a stream in an asynchronous operation. + public async Task RetrieveFileStreamAsync(FileResponse fileData, CancellationToken cancellationToken = default) + { + return await client.Client.GetStreamAsync(GetUrl($"/{fileData.Id}/content"), cancellationToken).ConfigureAwait(false); + } } } diff --git a/OpenAI-DotNet/OpenAI-DotNet.csproj b/OpenAI-DotNet/OpenAI-DotNet.csproj index e52797c9..fa2f5fde 100644 --- a/OpenAI-DotNet/OpenAI-DotNet.csproj +++ b/OpenAI-DotNet/OpenAI-DotNet.csproj @@ -28,8 +28,10 @@ More context [on Roger Pincombe's blog](https://rogerpincombe.com/openai-dotnet- OpenAI-DotNet.pfx True True - 7.6.2 + 7.6.3 +Version 7.6.3 +- Added RetrieveFileStreamAsync method to Files.FilesEndpoint Version 7.6.2 - Fixed parameter name in Threads.CreateMessageRequest - Added Stream overload to Threads.FileUploadRequest