diff --git a/Box.V2.Test.Integration/BoxFilesManagerIntegrationTest.cs b/Box.V2.Test.Integration/BoxFilesManagerIntegrationTest.cs index bc9f3fa53..bd7b15c51 100644 --- a/Box.V2.Test.Integration/BoxFilesManagerIntegrationTest.cs +++ b/Box.V2.Test.Integration/BoxFilesManagerIntegrationTest.cs @@ -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() { 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() { 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() { diff --git a/Box.V2.Test/BoxFilesManagerTest.cs b/Box.V2.Test/BoxFilesManagerTest.cs index f941464fc..e558d3948 100644 --- a/Box.V2.Test/BoxFilesManagerTest.cs +++ b/Box.V2.Test/BoxFilesManagerTest.cs @@ -299,7 +299,7 @@ public async Task UploadNewVersion_ValidResponse_ValidFile() public async Task ViewVersions_ValidResponse_ValidFileVersions() { /*** Arrange ***/ - Handler.Setup(h => h.ExecuteAsync>(It.IsAny())) + Handler.Setup(h => h.ExecuteAsync>(It.Is(r => "fields=version_number".Equals(r.GetQueryString())))) .Returns(Task.FromResult>>(new BoxResponse>() { Status = ResponseStatus.Success, @@ -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>(It.Is(r => "offset=100&limit=10".Equals(r.GetQueryString())))) + .Returns(Task.FromResult>>(new BoxResponse>() + { + Status = ResponseStatus.Success, + ContentString = LoadFixtureFromJson("Fixtures/BoxFiles/ViewVersions200.json") + })); + + /*** Act ***/ + BoxCollection 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("sean+apitest@box.com", f.ModifiedBy.Login); + Assert.AreEqual("1", f.VersionNumber); + } + [TestMethod] public async Task UpdateFileInformation_ValidResponse_ValidFile() { diff --git a/Box.V2/Managers/BoxFilesManager.cs b/Box.V2/Managers/BoxFilesManager.cs index f999c7b32..dbeb154d0 100644 --- a/Box.V2/Managers/BoxFilesManager.cs +++ b/Box.V2/Managers/BoxFilesManager.cs @@ -687,18 +687,48 @@ private string HexStringFromBytes(byte[] bytes) /// /// The file id. /// Attribute(s) to include in the response. + /// Zero-based index of first OffsetID of part to return. + /// How many parts to return. + /// Whether or not to auto-paginate to fetch all; defaults to false. /// 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. - public async Task> ViewVersionsAsync(string id, IEnumerable fields = null) + public async Task> ViewVersionsAsync(string id, IEnumerable 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> response = await ToResponseAsync>(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(request, limit.Value).ConfigureAwait(false); + } + else + { + IBoxResponse> response = await ToResponseAsync>(request).ConfigureAwait(false); + return response.ResponseObject; + } } /// diff --git a/Box.V2/Managers/IBoxFilesManager.cs b/Box.V2/Managers/IBoxFilesManager.cs index 0a3b8a270..f00f4f30d 100644 --- a/Box.V2/Managers/IBoxFilesManager.cs +++ b/Box.V2/Managers/IBoxFilesManager.cs @@ -221,9 +221,12 @@ Task UploadUsingSessionAsync(Stream stream, string fileName, /// /// The file id. /// Attribute(s) to include in the response. + /// Zero-based index of first OffsetID of part to return. + /// How many parts to return. + /// Whether or not to auto-paginate to fetch all; defaults to false. /// 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. - Task> ViewVersionsAsync(string id, IEnumerable fields = null); + Task> ViewVersionsAsync(string id, IEnumerable fields = null, int? offset = null, int? limit = null, bool autoPaginate = false); /// /// Used to update individual or multiple fields in the file object, including renaming the file, changing it’s description,