Skip to content

Commit

Permalink
fix: Added pagination option to IBoxFilesManager#ViewVersionsAsync (#…
Browse files Browse the repository at this point in the history
…869)

Fixes #866
  • Loading branch information
antusus authored Dec 6, 2022
1 parent f5915b3 commit 2324495
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 5 deletions.
26 changes: 26 additions & 0 deletions Box.V2.Test.Integration/BoxFilesManagerIntegrationTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,32 @@ public async Task ViewVersions_ShouldReturnCorrectVersionNumber_WhenFileVersionI
Assert.AreEqual("2", response.Entries[0].VersionNumber);
}

[TestMethod]
public async Task ViewVersions_ShouldReturnCorrectVersionNumber_WhenPaginationUsed()
{
var file = await CreateSmallFile();
await CreateNewFileVersion(file.Id);
await CreateNewFileVersion(file.Id);

var response = await UserClient.FilesManager.ViewVersionsAsync(file.Id, new List<string>() { BoxFileVersion.FieldVersionNumber }, 1, 1);

Assert.AreEqual(1, response.Entries.Count);
Assert.AreEqual("1", response.Entries[0].VersionNumber);
}

[TestMethod]
public async Task ViewVersions_ShouldReturnCorrectVersionNumber_WhenAutoPaginationUsed()
{
var file = await CreateSmallFile();
await CreateNewFileVersion(file.Id);
await CreateNewFileVersion(file.Id);

var response = await UserClient.FilesManager.ViewVersionsAsync(file.Id, new List<string>() { BoxFileVersion.FieldVersionNumber }, 0, 1, true);

Assert.AreEqual("2", response.Entries[0].VersionNumber);
Assert.AreEqual("1", response.Entries[1].VersionNumber);
}

[TestMethod]
public async Task AddSharedLink_ForValidNewFile_ShouldCreateNewSharedLink()
{
Expand Down
34 changes: 33 additions & 1 deletion Box.V2.Test/BoxFilesManagerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ public async Task UploadNewVersion_ValidResponse_ValidFile()
public async Task ViewVersions_ValidResponse_ValidFileVersions()
{
/*** Arrange ***/
Handler.Setup(h => h.ExecuteAsync<BoxCollection<BoxFileVersion>>(It.IsAny<IBoxRequest>()))
Handler.Setup(h => h.ExecuteAsync<BoxCollection<BoxFileVersion>>(It.Is<IBoxRequest>(r => "fields=version_number".Equals(r.GetQueryString()))))
.Returns(Task.FromResult<IBoxResponse<BoxCollection<BoxFileVersion>>>(new BoxResponse<BoxCollection<BoxFileVersion>>()
{
Status = ResponseStatus.Success,
Expand Down Expand Up @@ -327,6 +327,38 @@ public async Task ViewVersions_ValidResponse_ValidFileVersions()
Assert.AreEqual("1", f.VersionNumber);
}

[TestMethod]
public async Task ViewVersionsWithOffsetAndLimit_ValidResponse_ValidFileVersions()
{
/*** Arrange ***/
Handler.Setup(h => h.ExecuteAsync<BoxCollection<BoxFileVersion>>(It.Is<IBoxRequest>(r => "offset=100&limit=10".Equals(r.GetQueryString()))))
.Returns(Task.FromResult<IBoxResponse<BoxCollection<BoxFileVersion>>>(new BoxResponse<BoxCollection<BoxFileVersion>>()
{
Status = ResponseStatus.Success,
ContentString = LoadFixtureFromJson("Fixtures/BoxFiles/ViewVersions200.json")
}));

/*** Act ***/
BoxCollection<BoxFileVersion> c = await _filesManager.ViewVersionsAsync("0", null, 100, 10);

/*** Assert ***/
Assert.AreEqual(c.TotalCount, 1);
Assert.AreEqual(c.Entries.Count, 1);
BoxFileVersion f = c.Entries.First();
Assert.AreEqual("file_version", f.Type);
Assert.AreEqual("672259576", f.Id);
Assert.AreEqual("359c6c1ed98081b9a69eb3513b9deced59c957f9", f.Sha1);
Assert.AreEqual("Dragons.js", f.Name);
Assert.AreEqual(DateTimeOffset.Parse("2012-08-20T10:20:30-07:00"), f.CreatedAt);
Assert.AreEqual(DateTimeOffset.Parse("2012-11-28T13:14:58-08:00"), f.ModifiedAt);
Assert.AreEqual(92556, f.Size);
Assert.AreEqual("user", f.ModifiedBy.Type);
Assert.AreEqual("183732129", f.ModifiedBy.Id);
Assert.AreEqual("sean rose", f.ModifiedBy.Name);
Assert.AreEqual("[email protected]", f.ModifiedBy.Login);
Assert.AreEqual("1", f.VersionNumber);
}

[TestMethod]
public async Task UpdateFileInformation_ValidResponse_ValidFile()
{
Expand Down
36 changes: 33 additions & 3 deletions Box.V2/Managers/BoxFilesManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -687,18 +687,48 @@ private string HexStringFromBytes(byte[] bytes)
/// </summary>
/// <param name="id">The file id.</param>
/// <param name="fields">Attribute(s) to include in the response.</param>
/// <param name="offset">Zero-based index of first OffsetID of part to return.</param>
/// <param name="limit">How many parts to return.</param>
/// <param name="autoPaginate">Whether or not to auto-paginate to fetch all; defaults to false.</param>
/// <returns>A collection of versions other than the main version of the file. If a file has no other versions, an empty collection will be returned.
/// Note that if a file has a total of three versions, only the first two version will be returned.</returns>
public async Task<BoxCollection<BoxFileVersion>> ViewVersionsAsync(string id, IEnumerable<string> fields = null)
public async Task<BoxCollection<BoxFileVersion>> ViewVersionsAsync(string id, IEnumerable<string> fields = null, int? offset = null, int? limit = null, bool autoPaginate = false)
{
id.ThrowIfNullOrWhiteSpace("id");

BoxRequest request = new BoxRequest(_config.FilesEndpointUri, string.Format(Constants.VersionsPathString, id))
.Param(ParamFields, fields);

IBoxResponse<BoxCollection<BoxFileVersion>> response = await ToResponseAsync<BoxCollection<BoxFileVersion>>(request).ConfigureAwait(false);
if (offset.HasValue)
{
request.Param("offset", offset.Value.ToString());
}

return response.ResponseObject;
if (limit.HasValue)
{
request.Param("limit", limit.Value.ToString());
}

if (autoPaginate)
{
if (!limit.HasValue)
{
limit = 100;
request.Param("limit", limit.ToString());
}

if (!offset.HasValue)
{
request.Param("offset", "0");
}

return await AutoPaginateLimitOffset<BoxFileVersion>(request, limit.Value).ConfigureAwait(false);
}
else
{
IBoxResponse<BoxCollection<BoxFileVersion>> response = await ToResponseAsync<BoxCollection<BoxFileVersion>>(request).ConfigureAwait(false);
return response.ResponseObject;
}
}

/// <summary>
Expand Down
5 changes: 4 additions & 1 deletion Box.V2/Managers/IBoxFilesManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -221,9 +221,12 @@ Task<BoxFile> UploadUsingSessionAsync(Stream stream, string fileName,
/// </summary>
/// <param name="id">The file id.</param>
/// <param name="fields">Attribute(s) to include in the response.</param>
/// <param name="offset">Zero-based index of first OffsetID of part to return.</param>
/// <param name="limit">How many parts to return.</param>
/// <param name="autoPaginate">Whether or not to auto-paginate to fetch all; defaults to false.</param>
/// <returns>A collection of versions other than the main version of the file. If a file has no other versions, an empty collection will be returned.
/// Note that if a file has a total of three versions, only the first two version will be returned.</returns>
Task<BoxCollection<BoxFileVersion>> ViewVersionsAsync(string id, IEnumerable<string> fields = null);
Task<BoxCollection<BoxFileVersion>> ViewVersionsAsync(string id, IEnumerable<string> fields = null, int? offset = null, int? limit = null, bool autoPaginate = false);

/// <summary>
/// Used to update individual or multiple fields in the file object, including renaming the file, changing it’s description,
Expand Down

0 comments on commit 2324495

Please sign in to comment.