Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add shared link support to GetFolderItemsAsync #892

Merged
merged 1 commit into from
Feb 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions Box.V2.Test.Integration/BoxFolderManagerIntegrationTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ public static async Task<BoxFolder> CreateFolder(string parentId = "0", CommandS
return createFolderCommand.Folder;
}

public static async Task<BoxFolder> CreateFolderAsAdmin(string parentId)
public static async Task<BoxFolder> CreateFolderAsAdmin(string parentId = "0")
{
return await CreateFolder(parentId, CommandScope.Test, CommandAccessLevel.Admin);
}
Expand Down
17 changes: 17 additions & 0 deletions Box.V2.Test/BoxFoldersManagerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<BoxCollection<BoxItem>>(It.IsAny<IBoxRequest>()))
.Returns(() => Task.FromResult<IBoxResponse<BoxCollection<BoxItem>>>(new BoxResponse<BoxCollection<BoxItem>>()
{
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<IBoxRequest>(r => boxRequest = r);

BoxCollection<BoxItem> 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()
{
Expand Down
1 change: 1 addition & 0 deletions Box.V2/Box.V2.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@
<Compile Include="Utility\LRUCache.cs" />
<Compile Include="Utility\ContentTypeMapper.cs" />
<Compile Include="Utility\Retry.cs" />
<Compile Include="Utility\SharedLinkUtils.cs" />
<Compile Include="Wrappers\BoxError.cs" />
<Compile Include="Wrappers\BoxErrorContextInfo.cs" />
<Compile Include="Wrappers\BoxBinaryRequest.cs" />
Expand Down
12 changes: 11 additions & 1 deletion Box.V2/Managers/BoxFoldersManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -32,10 +33,13 @@ public BoxFoldersManager(IBoxConfig config, IBoxService service, IBoxConverter c
/// <param name="autoPaginate">Whether or not to auto-paginate to fetch all items; defaults to false.</param>
/// <param name="sort">The field to sort items on</param>
/// <param name="direction">The direction to sort results in: ascending or descending</param>
/// <param name="sharedLink">The shared link for this folder</param>
/// <param name="sharedLinkPassword">The password for the shared link (if required)</param>
/// <returns>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.</returns>
public async Task<BoxCollection<BoxItem>> GetFolderItemsAsync(string id, int limit, int offset = 0, IEnumerable<string> fields = null, bool autoPaginate = false, string sort = null, BoxSortDirection? direction = null)
public async Task<BoxCollection<BoxItem>> GetFolderItemsAsync(string id, int limit, int offset = 0, IEnumerable<string> fields = null, bool autoPaginate = false, string sort = null, BoxSortDirection? direction = null,
string sharedLink = null, string sharedLinkPassword = null)
{
id.ThrowIfNullOrWhiteSpace("id");

Expand All @@ -46,6 +50,12 @@ public async Task<BoxCollection<BoxItem>> 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<BoxItem>(request, limit).ConfigureAwait(false);
Expand Down
4 changes: 3 additions & 1 deletion Box.V2/Managers/BoxSharedItemsManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Box.V2.Extensions;
using Box.V2.Models;
using Box.V2.Services;
using Box.V2.Utility;

namespace Box.V2.Managers
{
Expand Down Expand Up @@ -36,8 +37,9 @@ public BoxSharedItemsManager(IBoxConfig config, IBoxService service, IBoxConvert
public async Task<BoxItem> 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<BoxItem> response = await ToResponseAsync<BoxItem>(request).ConfigureAwait(false);
return response.ResponseObject;
}
Expand Down
5 changes: 4 additions & 1 deletion Box.V2/Managers/IBoxFoldersManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,13 @@ public interface IBoxFoldersManager
/// <param name="autoPaginate">Whether or not to auto-paginate to fetch all items; defaults to false.</param>
/// <param name="sort">The field to sort items on</param>
/// <param name="direction">The direction to sort results in: ascending or descending</param>
/// <param name="sharedLink">The shared link for this folder</param>
/// <param name="sharedLinkPassword">The password for the shared link (if required)</param>
/// <returns>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.</returns>
Task<BoxCollection<BoxItem>> GetFolderItemsAsync(string id, int limit, int offset = 0, IEnumerable<string> fields = null, bool autoPaginate = false, string sort = null, BoxSortDirection? direction = null);
Task<BoxCollection<BoxItem>> GetFolderItemsAsync(string id, int limit, int offset = 0, IEnumerable<string> fields = null, bool autoPaginate = false, string sort = null, BoxSortDirection? direction = null,
string sharedLink = null, string sharedLinkPassword = null);

/// <summary>
/// Used to create a new empty folder. The new folder will be created inside of the specified parent folder.
Expand Down
10 changes: 10 additions & 0 deletions Box.V2/Utility/SharedLinkUtils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System;

namespace Box.V2.Utility
{
internal static class SharedLinkUtils
{
internal static Tuple<string, string> GetSharedLinkHeader(string sharedLink, string sharedLinkPassword)
=> Tuple.Create("BoxApi", string.Format("shared_link={0}{1}", sharedLink, string.IsNullOrEmpty(sharedLinkPassword) ? "" : ("&shared_link_password=" + sharedLinkPassword)));
}
}