/// <summary> /// To add or remove an item from a collection, you do a PUT on that item and change the list of collections it belongs to. /// </summary> /// <param name="fileId">Id of the file.</param> /// <param name="collectionsRequest">The request which contains collections ids</param> /// <returns>A full file object is returned.</returns> public async Task<BoxFile> CreateOrDeleteCollectionsForFileAsync(string fileId, BoxCollectionsRequest collectionsRequest) { fileId.ThrowIfNullOrWhiteSpace("fileId"); collectionsRequest.ThrowIfNull("collectionsRequest") .Collections.ThrowIfNull("collectionsRequest.Collections"); foreach (var collection in collectionsRequest.Collections) { collection.Type = null; } BoxRequest request = new BoxRequest(_config.FilesEndpointUri, fileId) .Method(RequestMethod.Put) .Payload(_converter.Serialize(collectionsRequest)); IBoxResponse<BoxFile> response = await ToResponseAsync<BoxFile>(request).ConfigureAwait(false); return response.ResponseObject; }
public async Task Collections_LiveSession() { var collections = await _client.CollectionsManager.GetCollectionsAsync(); var favorites = collections.Entries.FirstOrDefault(c => c.CollectionType == "favorites"); Assert.IsNotNull(favorites, "Could not find 'favorites' collection"); //add a file to favorites const string fileId = "16894944949"; var bcr = new BoxCollectionsRequest() { Collections = new List<BoxRequestEntity>() { new BoxRequestEntity() { Id = favorites.Id } } }; var updatedFile = await _client.CollectionsManager.CreateOrDeleteCollectionsForFileAsync(fileId, bcr); Assert.AreEqual(updatedFile.Id, fileId, "Could not add item to favorites collection"); //get list of collection items var items = await _client.CollectionsManager.GetCollectionItemsAsync(favorites.Id); Assert.AreEqual(1, items.Entries.Count, "Wrong number of items in favorites collection"); Assert.AreEqual(items.Entries[0].Id, fileId, "Incorrect file in favorites collection"); //clear out favorites bcr = new BoxCollectionsRequest() { Collections = new List<BoxRequestEntity>() }; updatedFile = await _client.CollectionsManager.CreateOrDeleteCollectionsForFileAsync(fileId, bcr); Assert.AreEqual(updatedFile.Id, fileId, "Could not remove item from favorites collection"); items = await _client.CollectionsManager.GetCollectionItemsAsync(favorites.Id); Assert.AreEqual(0, items.Entries.Count, "Wrong number of items in favorites collection"); }
public async Task CreateOrDeleteCollectionsForFolder_ValidResponse() { /*** Arrange ***/ string responseString = @"{ ""type"": ""folder"", ""id"": ""11446498"", ""sequence_id"": ""1"", ""etag"": ""1"", ""name"": ""New Folder Name!"", ""created_at"": ""2012-12-12T10:53:43-08:00"", ""modified_at"": ""2012-12-12T11:15:04-08:00"", ""description"": ""Some pictures I took"", ""size"": 629644, ""path_collection"": { ""total_count"": 1, ""entries"": [ { ""type"": ""folder"", ""id"": ""0"", ""sequence_id"": null, ""etag"": null, ""name"": ""All Files"" } ] }, ""created_by"": { ""type"": ""user"", ""id"": ""17738362"", ""name"": ""sean rose"", ""login"": ""*****@*****.**"" }, ""modified_by"": { ""type"": ""user"", ""id"": ""17738362"", ""name"": ""sean rose"", ""login"": ""*****@*****.**"" }, ""owned_by"": { ""type"": ""user"", ""id"": ""17738362"", ""name"": ""sean rose"", ""login"": ""*****@*****.**"" }, ""shared_link"": { ""url"": ""https://www.box.com/s/vspke7y05sb214wjokpk"", ""download_url"": null, ""vanity_url"": null, ""is_password_enabled"": false, ""unshared_at"": null, ""download_count"": 0, ""preview_count"": 0, ""access"": ""open"", ""permissions"": { ""can_download"": true, ""can_preview"": true } }, ""folder_upload_email"": { ""access"": ""open"", ""email"": ""*****@*****.**"" }, ""parent"": { ""type"": ""folder"", ""id"": ""0"", ""sequence_id"": null, ""etag"": null, ""name"": ""All Files"" }, ""item_status"": ""active"", ""item_collection"": { ""total_count"": 1, ""entries"": [ { ""type"": ""file"", ""id"": ""5000948880"", ""sequence_id"": ""3"", ""etag"": ""3"", ""sha1"": ""134b65991ed521fcfe4724b7d814ab8ded5185dc"", ""name"": ""tigers.jpeg"" } ], ""offset"": 0, ""limit"": 100 } }"; IBoxRequest boxRequest = null; _handler.Setup(h => h.ExecuteAsync<BoxFolder>(It.IsAny<IBoxRequest>())) .Returns(Task.FromResult<IBoxResponse<BoxFolder>>(new BoxResponse<BoxFolder>() { Status = ResponseStatus.Success, ContentString = responseString })) .Callback<IBoxRequest>(r => boxRequest = r); /*** Act ***/ BoxCollectionsRequest collectionsRequest = new BoxCollectionsRequest() { Collections = new List<BoxRequestEntity>() { new BoxRequestEntity() { Id="123" } } }; BoxFolder result = await _collectionsManager.CreateOrDeleteCollectionsForFolderAsync("11446498", collectionsRequest); /*** Assert ***/ //Request check Assert.IsNotNull(boxRequest); Assert.AreEqual(RequestMethod.Put, boxRequest.Method); Assert.AreEqual(_FoldersUri + "11446498", boxRequest.AbsoluteUri.AbsoluteUri); BoxCollectionsRequest payload = JsonConvert.DeserializeObject<BoxCollectionsRequest>(boxRequest.Payload); Assert.AreEqual(collectionsRequest.Collections[0].Id, payload.Collections[0].Id); //Response check Assert.AreEqual("11446498", result.Id); Assert.AreEqual(1, result.PathCollection.TotalCount); Assert.AreEqual("https://www.box.com/s/vspke7y05sb214wjokpk", result.SharedLink.Url); Assert.AreEqual("17738362", result.CreatedBy.Id); }