From 788bc1223cf073fa39ab3b3efd1cf3ef10c74c62 Mon Sep 17 00:00:00 2001 From: mwoda Date: Fri, 24 Feb 2023 14:16:55 +0100 Subject: [PATCH] feat: add shared link support to `GetFolderItemsAsync` --- .../BoxFolderManagerIntegrationTest.cs | 23 +++++++++++++++++++ .../Configuration/IntegrationTestBase.cs | 2 +- Box.V2.Test/BoxFoldersManagerTest.cs | 17 ++++++++++++++ Box.V2/Box.V2.csproj | 1 + Box.V2/Managers/BoxFoldersManager.cs | 12 +++++++++- Box.V2/Managers/BoxSharedItemsManager.cs | 4 +++- Box.V2/Managers/IBoxFoldersManager.cs | 5 +++- Box.V2/Utility/SharedLinkUtils.cs | 10 ++++++++ 8 files changed, 70 insertions(+), 4 deletions(-) create mode 100644 Box.V2/Utility/SharedLinkUtils.cs diff --git a/Box.V2.Test.Integration/BoxFolderManagerIntegrationTest.cs b/Box.V2.Test.Integration/BoxFolderManagerIntegrationTest.cs index 3b29d9788..0276af975 100644 --- a/Box.V2.Test.Integration/BoxFolderManagerIntegrationTest.cs +++ b/Box.V2.Test.Integration/BoxFolderManagerIntegrationTest.cs @@ -199,5 +199,28 @@ public async Task RemoveWatermarkAsync_ForExistingFolder_ShouldCorrectlyRemoveWa var folderInfo = await UserClient.FoldersManager.GetInformationAsync(folder.Id, fieldList); Assert.IsFalse(folderInfo.WatermarkInfo.IsWatermarked); } + + [TestMethod] + public async Task GetFolderItemsAsync_ForFolderWithSharedLink_ShouldReturnAllFolderItems() + { + var folder = await CreateFolderAsAdmin(); + var file = await CreateSmallFileAsAdmin(folder.Id); + + var password = "SuperSecret123"; + var sharedLinkRequest = new BoxSharedLinkRequest + { + Access = BoxSharedLinkAccessType.open, + Password = password + }; + var sharedLink = await AdminClient.FoldersManager.CreateSharedLinkAsync(folder.Id, sharedLinkRequest); + + var sharedItems = await UserClient.SharedItemsManager.SharedItemsAsync(sharedLink.SharedLink.Url, password); + var items = await UserClient.FoldersManager.GetFolderItemsAsync(sharedItems.Id, 100, sharedLink: sharedLink.SharedLink.Url, + sharedLinkPassword: password); + + + Assert.AreEqual(items.TotalCount, 1); + Assert.AreEqual(items.Entries[0].Id, file.Id); + } } } diff --git a/Box.V2.Test.Integration/Configuration/IntegrationTestBase.cs b/Box.V2.Test.Integration/Configuration/IntegrationTestBase.cs index 6087bc4eb..03c6f72f2 100644 --- a/Box.V2.Test.Integration/Configuration/IntegrationTestBase.cs +++ b/Box.V2.Test.Integration/Configuration/IntegrationTestBase.cs @@ -250,7 +250,7 @@ public static async Task CreateFolder(string parentId = "0", CommandS return createFolderCommand.Folder; } - public static async Task CreateFolderAsAdmin(string parentId) + public static async Task CreateFolderAsAdmin(string parentId = "0") { return await CreateFolder(parentId, CommandScope.Test, CommandAccessLevel.Admin); } diff --git a/Box.V2.Test/BoxFoldersManagerTest.cs b/Box.V2.Test/BoxFoldersManagerTest.cs index ad0f3acd4..9515b115e 100644 --- a/Box.V2.Test/BoxFoldersManagerTest.cs +++ b/Box.V2.Test/BoxFoldersManagerTest.cs @@ -75,6 +75,23 @@ public async Task GetFolderItems_ValidResponse_SortDirection() Assert.AreEqual("DESC", boxRequest.Parameters["direction"]); } + [TestMethod] + public async Task GetFolderItems_ValidHeader_ValidSharedLink() + { + IBoxRequest boxRequest = null; + Handler.Setup(h => h.ExecuteAsync>(It.IsAny())) + .Returns(() => Task.FromResult>>(new BoxResponse>() + { + Status = ResponseStatus.Success, + ContentString = "{\"total_count\":24,\"entries\":[{\"type\":\"folder\",\"id\":\"192429928\",\"sequence_id\":\"1\",\"etag\":\"1\",\"name\":\"Stephen Curry Three Pointers\"},{\"type\":\"file\",\"id\":\"818853862\",\"sequence_id\":\"0\",\"etag\":\"0\",\"name\":\"Warriors.jpg\"}],\"offset\":0,\"limit\":2,\"order\":[{\"by\":\"type\",\"direction\":\"ASC\"},{\"by\":\"name\",\"direction\":\"ASC\"}]}" + })).Callback(r => boxRequest = r); + + BoxCollection items = await _foldersManager.GetFolderItemsAsync("0", 2, sharedLink: "my_shared_link", sharedLinkPassword: "SuperSecret123"); + + Assert.IsTrue(boxRequest.HttpHeaders.ContainsKey("BoxApi")); + Assert.AreEqual(boxRequest.HttpHeaders["BoxApi"], "shared_link=my_shared_link&shared_link_password=SuperSecret123"); + } + [TestMethod] public async Task CreateFolder_ValidResponse_ValidFolder() { diff --git a/Box.V2/Box.V2.csproj b/Box.V2/Box.V2.csproj index 22cd981d1..af6900757 100644 --- a/Box.V2/Box.V2.csproj +++ b/Box.V2/Box.V2.csproj @@ -268,6 +268,7 @@ + diff --git a/Box.V2/Managers/BoxFoldersManager.cs b/Box.V2/Managers/BoxFoldersManager.cs index 7c8082992..abb93eff3 100644 --- a/Box.V2/Managers/BoxFoldersManager.cs +++ b/Box.V2/Managers/BoxFoldersManager.cs @@ -7,6 +7,7 @@ using Box.V2.Extensions; using Box.V2.Models; using Box.V2.Services; +using Box.V2.Utility; using Newtonsoft.Json.Linq; namespace Box.V2.Managers @@ -32,10 +33,13 @@ public BoxFoldersManager(IBoxConfig config, IBoxService service, IBoxConverter c /// Whether or not to auto-paginate to fetch all items; defaults to false. /// The field to sort items on /// The direction to sort results in: ascending or descending + /// The shared link for this folder + /// The password for the shared link (if required) /// A collection of items contained in the folder is returned. An error is thrown if the folder does not exist, /// or if any of the parameters are invalid. The total_count returned may not match the number of entries when using enterprise scope, /// because external folders are hidden the list of entries. - public async Task> GetFolderItemsAsync(string id, int limit, int offset = 0, IEnumerable fields = null, bool autoPaginate = false, string sort = null, BoxSortDirection? direction = null) + public async Task> GetFolderItemsAsync(string id, int limit, int offset = 0, IEnumerable fields = null, bool autoPaginate = false, string sort = null, BoxSortDirection? direction = null, + string sharedLink = null, string sharedLinkPassword = null) { id.ThrowIfNullOrWhiteSpace("id"); @@ -46,6 +50,12 @@ public async Task> GetFolderItemsAsync(string id, int lim .Param("direction", direction.ToString()) .Param(ParamFields, fields); + if (!string.IsNullOrEmpty(sharedLink)) + { + var sharedLinkHeader = SharedLinkUtils.GetSharedLinkHeader(sharedLink, sharedLinkPassword); + request.Header(sharedLinkHeader.Item1, sharedLinkHeader.Item2); + } + if (autoPaginate) { return await AutoPaginateLimitOffset(request, limit).ConfigureAwait(false); diff --git a/Box.V2/Managers/BoxSharedItemsManager.cs b/Box.V2/Managers/BoxSharedItemsManager.cs index 3bfff38b6..610a68c9b 100644 --- a/Box.V2/Managers/BoxSharedItemsManager.cs +++ b/Box.V2/Managers/BoxSharedItemsManager.cs @@ -5,6 +5,7 @@ using Box.V2.Extensions; using Box.V2.Models; using Box.V2.Services; +using Box.V2.Utility; namespace Box.V2.Managers { @@ -36,8 +37,9 @@ public BoxSharedItemsManager(IBoxConfig config, IBoxService service, IBoxConvert public async Task SharedItemsAsync(string sharedLink, string sharedLinkPassword = null) { sharedLink.ThrowIfNullOrWhiteSpace("sharedLink"); + var sharedLinkHeader = SharedLinkUtils.GetSharedLinkHeader(sharedLink, sharedLinkPassword); BoxRequest request = new BoxRequest(_config.SharedItemsUri, null) - .Header("BoxApi", string.Format("shared_link={0}{1}", sharedLink, (string.IsNullOrEmpty(sharedLinkPassword) ? "" : ("&shared_link_password=" + sharedLinkPassword)))); + .Header(sharedLinkHeader.Item1, sharedLinkHeader.Item2); IBoxResponse response = await ToResponseAsync(request).ConfigureAwait(false); return response.ResponseObject; } diff --git a/Box.V2/Managers/IBoxFoldersManager.cs b/Box.V2/Managers/IBoxFoldersManager.cs index fc19302be..02872d628 100644 --- a/Box.V2/Managers/IBoxFoldersManager.cs +++ b/Box.V2/Managers/IBoxFoldersManager.cs @@ -23,10 +23,13 @@ public interface IBoxFoldersManager /// Whether or not to auto-paginate to fetch all items; defaults to false. /// The field to sort items on /// The direction to sort results in: ascending or descending + /// The shared link for this folder + /// The password for the shared link (if required) /// A collection of items contained in the folder is returned. An error is thrown if the folder does not exist, /// or if any of the parameters are invalid. The total_count returned may not match the number of entries when using enterprise scope, /// because external folders are hidden the list of entries. - Task> GetFolderItemsAsync(string id, int limit, int offset = 0, IEnumerable fields = null, bool autoPaginate = false, string sort = null, BoxSortDirection? direction = null); + Task> GetFolderItemsAsync(string id, int limit, int offset = 0, IEnumerable fields = null, bool autoPaginate = false, string sort = null, BoxSortDirection? direction = null, + string sharedLink = null, string sharedLinkPassword = null); /// /// Used to create a new empty folder. The new folder will be created inside of the specified parent folder. diff --git a/Box.V2/Utility/SharedLinkUtils.cs b/Box.V2/Utility/SharedLinkUtils.cs new file mode 100644 index 000000000..0831d3341 --- /dev/null +++ b/Box.V2/Utility/SharedLinkUtils.cs @@ -0,0 +1,10 @@ +using System; + +namespace Box.V2.Utility +{ + internal static class SharedLinkUtils + { + internal static Tuple GetSharedLinkHeader(string sharedLink, string sharedLinkPassword) + => Tuple.Create("BoxApi", string.Format("shared_link={0}{1}", sharedLink, string.IsNullOrEmpty(sharedLinkPassword) ? "" : ("&shared_link_password=" + sharedLinkPassword))); + } +}