Skip to content

Commit

Permalink
added CTs to FileClient
Browse files Browse the repository at this point in the history
  • Loading branch information
KrzysztofCwalina committed Jun 12, 2024
1 parent 5ad969d commit f0cd892
Showing 1 changed file with 31 additions and 20 deletions.
51 changes: 31 additions & 20 deletions src/Custom/Files/FileClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.ClientModel;
using System.ClientModel.Primitives;
using System.IO;
using System.Threading;
using System.Threading.Tasks;

namespace OpenAI.Files;
Expand Down Expand Up @@ -83,10 +84,11 @@ protected internal FileClient(ClientPipeline pipeline, Uri endpoint, OpenAIClien
/// validate the file format. The request may fail if the file extension and file format do not match.
/// </param>
/// <param name="purpose"> The intended purpose of the uploaded file. </param>
/// <param name="cancellationToken">A token that can be used to cancel this method call.</param>
/// <exception cref="ArgumentNullException"> <paramref name="file"/> or <paramref name="filename"/> is null. </exception>
/// <exception cref="ArgumentException"> <paramref name="filename"/> is an empty string, and was expected to be non-empty. </exception>
/// <returns> Information about the uploaded file. </returns>
public virtual async Task<ClientResult<OpenAIFileInfo>> UploadFileAsync(Stream file, string filename, FileUploadPurpose purpose)
public virtual async Task<ClientResult<OpenAIFileInfo>> UploadFileAsync(Stream file, string filename, FileUploadPurpose purpose, CancellationToken cancellationToken = default)
{
Argument.AssertNotNull(file, nameof(file));
Argument.AssertNotNullOrEmpty(filename, nameof(filename));
Expand All @@ -97,7 +99,7 @@ public virtual async Task<ClientResult<OpenAIFileInfo>> UploadFileAsync(Stream f
};

using MultipartFormDataBinaryContent content = options.ToMultipartContent(file, filename);
ClientResult result = await UploadFileAsync(content, content.ContentType).ConfigureAwait(false);
ClientResult result = await UploadFileAsync(content, content.ContentType, cancellationToken.ToRequestOptions()).ConfigureAwait(false);
return ClientResult.FromValue(OpenAIFileInfo.FromResponse(result.GetRawResponse()), result.GetRawResponse());
}

Expand All @@ -118,10 +120,11 @@ public virtual async Task<ClientResult<OpenAIFileInfo>> UploadFileAsync(Stream f
/// validate the file format. The request may fail if the file extension and file format do not match.
/// </param>
/// <param name="purpose"> The intended purpose of the uploaded file. </param>
/// <param name="cancellationToken">A token that can be used to cancel this method call.</param>
/// <exception cref="ArgumentNullException"> <paramref name="file"/> or <paramref name="filename"/> is null. </exception>
/// <exception cref="ArgumentException"> <paramref name="filename"/> is an empty string, and was expected to be non-empty. </exception>
/// <returns> Information about the uploaded file. </returns>
public virtual ClientResult<OpenAIFileInfo> UploadFile(Stream file, string filename, FileUploadPurpose purpose)
public virtual ClientResult<OpenAIFileInfo> UploadFile(Stream file, string filename, FileUploadPurpose purpose, CancellationToken cancellationToken = default)
{
Argument.AssertNotNull(file, nameof(file));
Argument.AssertNotNullOrEmpty(filename, nameof(filename));
Expand All @@ -132,7 +135,7 @@ public virtual ClientResult<OpenAIFileInfo> UploadFile(Stream file, string filen
};

using MultipartFormDataBinaryContent content = options.ToMultipartContent(file, filename);
ClientResult result = UploadFile(content, content.ContentType);
ClientResult result = UploadFile(content, content.ContentType, cancellationToken.ToRequestOptions());
return ClientResult.FromValue(OpenAIFileInfo.FromResponse(result.GetRawResponse()), result.GetRawResponse());
}

Expand Down Expand Up @@ -248,72 +251,78 @@ public virtual ClientResult<OpenAIFileInfo> UploadFile(string filePath, FileUplo

/// <summary> Retrieves a list of files that belong to the user's organization. </summary>
/// <param name="purpose"> Only return files with the given purpose. </param>
/// <param name="cancellationToken">A token that can be used to cancel this method call.</param>
/// <returns> Information about the files in the user's organization. </returns>
public virtual async Task<ClientResult<OpenAIFileInfoCollection>> GetFilesAsync(OpenAIFilePurpose? purpose = null)
public virtual async Task<ClientResult<OpenAIFileInfoCollection>> GetFilesAsync(OpenAIFilePurpose? purpose = null, CancellationToken cancellationToken = default)
{
ClientResult result = await GetFilesAsync(purpose?.ToString(), null).ConfigureAwait(false);
ClientResult result = await GetFilesAsync(purpose?.ToString(), cancellationToken.ToRequestOptions()).ConfigureAwait(false);
return ClientResult.FromValue(OpenAIFileInfoCollection.FromResponse(result.GetRawResponse()), result.GetRawResponse());
}

/// <summary> Retrieves a list of files that belong to the user's organization. </summary>
/// <param name="purpose"> Only return files with the given purpose. </param>
/// <param name="cancellationToken">A token that can be used to cancel this method call.</param>
/// <returns> Information about the files in the user's organization. </returns>
public virtual ClientResult<OpenAIFileInfoCollection> GetFiles(OpenAIFilePurpose? purpose = null)
public virtual ClientResult<OpenAIFileInfoCollection> GetFiles(OpenAIFilePurpose? purpose = null, CancellationToken cancellationToken = default)
{
ClientResult result = GetFiles(purpose?.ToString(), null);
ClientResult result = GetFiles(purpose?.ToString(), cancellationToken.ToRequestOptions());
return ClientResult.FromValue(OpenAIFileInfoCollection.FromResponse(result.GetRawResponse()), result.GetRawResponse());
}

/// <summary> Retrieves information about a specified file. </summary>
/// <param name="fileId"> The ID of the file to retrieve. </param>
/// <param name="cancellationToken">A token that can be used to cancel this method call.</param>
/// <exception cref="ArgumentNullException"> <paramref name="fileId"/> is null. </exception>
/// <exception cref="ArgumentException"> <paramref name="fileId"/> is an empty string, and was expected to be non-empty. </exception>
/// <returns> Information about the specified file. </returns>
public virtual async Task<ClientResult<OpenAIFileInfo>> GetFileAsync(string fileId)
public virtual async Task<ClientResult<OpenAIFileInfo>> GetFileAsync(string fileId, CancellationToken cancellationToken = default)
{
Argument.AssertNotNullOrEmpty(fileId, nameof(fileId));

ClientResult result = await GetFileAsync(fileId, (RequestOptions)null).ConfigureAwait(false);
ClientResult result = await GetFileAsync(fileId, cancellationToken.ToRequestOptions()).ConfigureAwait(false);
return ClientResult.FromValue(OpenAIFileInfo.FromResponse(result.GetRawResponse()), result.GetRawResponse());
}

/// <summary> Retrieves information about a specified file. </summary>
/// <param name="fileId"> The ID of the file to retrieve. </param>
/// <param name="cancellationToken">A token that can be used to cancel this method call.</param>
/// <exception cref="ArgumentNullException"> <paramref name="fileId"/> is null. </exception>
/// <exception cref="ArgumentException"> <paramref name="fileId"/> is an empty string, and was expected to be non-empty. </exception>
/// <returns> Information about the specified file. </returns>
public virtual ClientResult<OpenAIFileInfo> GetFile(string fileId)
public virtual ClientResult<OpenAIFileInfo> GetFile(string fileId, CancellationToken cancellationToken = default)
{
Argument.AssertNotNullOrEmpty(fileId, nameof(fileId));

ClientResult result = GetFile(fileId, (RequestOptions)null);
ClientResult result = GetFile(fileId, cancellationToken.ToRequestOptions());
return ClientResult.FromValue(OpenAIFileInfo.FromResponse(result.GetRawResponse()), result.GetRawResponse());
}

/// <summary> Deletes a previously uploaded file. </summary>
/// <param name="fileId"> The ID of the file to delete. </param>
/// <param name="cancellationToken">A token that can be used to cancel this method call.</param>
/// <exception cref="ArgumentNullException"> <paramref name="fileId"/> is null. </exception>
/// <exception cref="ArgumentException"> <paramref name="fileId"/> is an empty string, and was expected to be non-empty. </exception>
/// <returns> A boolean value indicating whether the deletion request was successful. </returns>
public virtual async Task<ClientResult<bool>> DeleteFileAsync(string fileId)
public virtual async Task<ClientResult<bool>> DeleteFileAsync(string fileId, CancellationToken cancellationToken = default)
{
Argument.AssertNotNullOrEmpty(fileId, nameof(fileId));

ClientResult result = await DeleteFileAsync(fileId, null).ConfigureAwait(false);
ClientResult result = await DeleteFileAsync(fileId, cancellationToken.ToRequestOptions()).ConfigureAwait(false);
InternalDeleteFileResponse internalDeletion = InternalDeleteFileResponse.FromResponse(result.GetRawResponse());
return ClientResult.FromValue(internalDeletion.Deleted, result.GetRawResponse());
}

/// <summary> Deletes a previously uploaded file. </summary>
/// <param name="fileId"> The ID of the file to delete. </param>
/// <param name="cancellationToken">A token that can be used to cancel this method call.</param>
/// <exception cref="ArgumentNullException"> <paramref name="fileId"/> is null. </exception>
/// <exception cref="ArgumentException"> <paramref name="fileId"/> is an empty string, and was expected to be non-empty. </exception>
/// <returns> A boolean value indicating whether the deletion request was successful. </returns>
public virtual ClientResult<bool> DeleteFile(string fileId)
public virtual ClientResult<bool> DeleteFile(string fileId, CancellationToken cancellationToken = default)
{
Argument.AssertNotNullOrEmpty(fileId, nameof(fileId));

ClientResult result = DeleteFile(fileId, null);
ClientResult result = DeleteFile(fileId, cancellationToken.ToRequestOptions());
InternalDeleteFileResponse internalDeletion = InternalDeleteFileResponse.FromResponse(result.GetRawResponse());
return ClientResult.FromValue(internalDeletion.Deleted, result.GetRawResponse());
}
Expand All @@ -340,27 +349,29 @@ public virtual ClientResult<bool> DeleteFile(OpenAIFileInfo file)

/// <summary> Downloads the binary content of the specified file. </summary>
/// <param name="fileId"> The ID of the file to download. </param>
/// <param name="cancellationToken">A token that can be used to cancel this method call.</param>
/// <exception cref="ArgumentNullException"> <paramref name="fileId"/> is null. </exception>
/// <exception cref="ArgumentException"> <paramref name="fileId"/> is an empty string, and was expected to be non-empty. </exception>
/// <returns> The contents of the specified file. </returns>
public virtual async Task<ClientResult<BinaryData>> DownloadFileAsync(string fileId)
public virtual async Task<ClientResult<BinaryData>> DownloadFileAsync(string fileId, CancellationToken cancellationToken = default)
{
Argument.AssertNotNullOrEmpty(fileId, nameof(fileId));

ClientResult result = await DownloadFileAsync(fileId, null).ConfigureAwait(false);
ClientResult result = await DownloadFileAsync(fileId, cancellationToken.ToRequestOptions()).ConfigureAwait(false);
return ClientResult.FromValue(result.GetRawResponse().Content, result.GetRawResponse());
}

/// <summary> Downloads the binary content of the specified file. </summary>
/// <param name="fileId"> The ID of the file to download. </param>
/// <param name="cancellationToken">A token that can be used to cancel this method call.</param>
/// <exception cref="ArgumentNullException"> <paramref name="fileId"/> is null. </exception>
/// <exception cref="ArgumentException"> <paramref name="fileId"/> is an empty string, and was expected to be non-empty. </exception>
/// <returns> The bionary content of the specified file. </returns>
public virtual ClientResult<BinaryData> DownloadFile(string fileId)
public virtual ClientResult<BinaryData> DownloadFile(string fileId, CancellationToken cancellationToken = default)
{
Argument.AssertNotNullOrEmpty(fileId, nameof(fileId));

ClientResult result = DownloadFile(fileId, null);
ClientResult result = DownloadFile(fileId, cancellationToken.ToRequestOptions());
return ClientResult.FromValue(result.GetRawResponse().Content, result.GetRawResponse());
}

Expand Down

0 comments on commit f0cd892

Please sign in to comment.