private static void ExecuteAPIMethods(string clientId, string clientSecret, string accessToken, string refreshToken) { try { // Optionally refresh the user's access token var authenticator = new TokenProvider(clientId, clientSecret); var oAuthToken = authenticator.RefreshAccessToken(refreshToken); accessToken = oAuthToken.AccessToken; // Instantiate a BoxManager with your api key and a user's auth token var boxManager = new BoxManager(accessToken); // Create a new file in the root folder boxManager.CreateFile(Folder.Root, "a new file.txt", Encoding.UTF8.GetBytes("hello, world!")); // Fetch the root folder Folder folder = boxManager.GetFolder(Folder.Root); // Find a 'mini' representation of the created file among the root folder's contents File file = folder.Files.Single(f => f.Name.Equals("a new file.txt")); // Get the file with all properties populated. file = boxManager.Get(file); // Rename the file file = boxManager.Rename(file, "the new name.txt"); // Create a new subfolder Folder subfolder = boxManager.CreateFolder(Folder.Root, "my subfolder"); // Move the file to the subfolder file = boxManager.Move(file, subfolder); // Write some content to the file using (var stream = new MemoryStream(Encoding.UTF8.GetBytes("goodbye, world!"))) { file = boxManager.Write(file, stream); } // Read the contents to a stream using (var stream = new MemoryStream()) { boxManager.Read(file, stream); using (var reader = new StreamReader(stream)) { stream.Position = 0; Console.Out.WriteLine("File content: '{0}'", reader.ReadToEnd()); } } // Delete the folder and its contents boxManager.Delete(subfolder, recursive: true); } catch (BoxException e) { Console.Out.WriteLine(e); } }
private static void AssertEntityConstraints(File item, ResourceType expectedType, string expectedName, string expectedParentId, string expectedId) { Assert.That(item, Is.Not.Null); Assert.That(item.Type, Is.EqualTo(expectedType)); Assert.That(item.Name, Is.EqualTo(expectedName)); if (expectedParentId == null) { Assert.That(item.Parent, Is.Null); } else { Assert.That(item.Parent.Id, Is.EqualTo(expectedParentId)); } if (expectedId != null) { Assert.That(item.Id, Is.EqualTo(expectedId)); } }
protected static void AssertFileConstraints(File file, string expectedName, string expectedParentId, string expectedId = null) { AssertEntityConstraints(file, ResourceType.File, expectedName, expectedParentId, expectedId); }
/// <summary> /// Update one or more of a file's name, description, parent, or shared link. /// </summary> /// <param name="onSuccess">Action to perform with the update file</param> /// <param name="onFailure">Action to perform following a failed File operation</param> /// <param name="file">The file to update</param> /// <param name="fields">The properties that should be set on the returned File object. Type and Id are always set. If left null, all properties will be set, which can increase response time.</param> /// <param name="etag">Include the item's etag to prevent the completion of this operation if you don't have the must current version of the item. A BoxItemModifiedException will be thrown if the item is stale.</param> public void Update(Action<File> onSuccess, Action<Error> onFailure, File file, IEnumerable<FileField> fields = null, string etag = null) { GuardFromNull(file, "file"); IRestRequest request = _requestHelper.Update(ResourceType.File, file.Id, etag, fields, file.Parent.Id, file.Name, file.Description, file.SharedLink); _restClient.ExecuteAsync(request, onSuccess, onFailure); }
/// <summary> /// Creates a shared link to the specified file /// </summary> /// <param name="onSuccess">Action to perform with the linked file</param> /// <param name="onFailure">Action to perform following a failed File operation</param> /// <param name="file">The file for which to create a shared link</param> /// <param name="sharedLink">The properties of the shared link</param> /// <param name="fields">The properties that should be set on the returned File object. Type and Id are always set. If left null, all properties will be set, which can increase response time.</param> /// <param name="etag">Include the item's etag to prevent the completion of this operation if you don't have the must current version of the item. A BoxItemModifiedException will be thrown if the item is stale.</param> /// <remarks>In order for File.SharedLink to be populated, you must either include Field.SharedLink in the fields list, or leave the list null</remarks> public void ShareLink(Action<File> onSuccess, Action<Error> onFailure, File file, SharedLink sharedLink, IEnumerable<FileField> fields = null, string etag = null) { GuardFromNull(file, "file"); ShareFileLink(onSuccess, onFailure, file.Id, sharedLink, fields, etag); }
/// <summary> /// Renames a file /// </summary> /// <param name="onSuccess">Action to perform with the renamed file</param> /// <param name="onFailure">Action to perform following a failed File operation</param> /// <param name="file">The file to rename</param> /// <param name="newName">The new name for the file</param> /// <param name="fields">The properties that should be set on the returned File object. Type and Id are always set. If left null, all properties will be set, which can increase response time.</param> /// <param name="etag">Include the item's etag to prevent the completion of this operation if you don't have the must current version of the item. A BoxItemModifiedException will be thrown if the item is stale.</param> public void Rename(Action<File> onSuccess, Action<Error> onFailure, File file, string newName, IEnumerable<FileField> fields = null, string etag = null) { GuardFromNull(file, "file"); RenameFile(onSuccess, onFailure, file.Id, newName, fields, etag); }
/// <summary> /// Copies a file to the specified folder /// </summary> /// <param name="onSuccess">Action to perform following a successful File operation</param> /// <param name="onFailure">Action to perform following a failed File operation</param> /// <param name="file">The file to copy</param> /// <param name="newParentId">The ID of the destination folder for the copied file</param> /// <param name="newName">The optional new name for the copied file</param> /// <param name="fields">The properties that should be set on the returned File object. Type and Id are always set. If left null, all properties will be set, which can increase response time.</param> public void Copy(Action<File> onSuccess, Action<Error> onFailure, File file, string newParentId, string newName = null, IEnumerable<FileField> fields = null) { GuardFromNull(file, "file"); CopyFile(onSuccess, onFailure, file.Id, newParentId, newName, fields); }
/// <summary> /// Retrieve the contents of the specified file /// </summary> /// <param name="onSuccess">Action to perform with the file contents</param> /// <param name="onFailure">Action to perform following a failed File operation</param> /// <param name="file">The file to read</param> public void Read(Action<byte[]> onSuccess, Action<Error> onFailure, File file) { GuardFromNull(file, "file"); Read(onSuccess, onFailure, file.Id); }
/// <summary> /// Retrieve the contents of the specified file /// </summary> /// <param name="file">The file to read</param> /// <returns>The raw file contents</returns> public byte[] Read(File file) { GuardFromNull(file, "file"); return Read(file.Id); }
/// <summary> /// Deletes a file from the user's Box /// </summary> /// <param name="file">The file to delete</param> /// <param name="etag">Include the item's etag to prevent the completion of this operation if you don't have the must current version of the item. A BoxItemModifiedException will be thrown if the item is stale.</param> public void Delete(File file, string etag = null) { GuardFromNull(file, "file"); DeleteFile(file.Id, etag); }
/// <summary> /// Updates the content of a file /// </summary> /// <param name="onSuccess">Action to perform with the successfully written file</param> /// <param name="onFailure">Action to perform following a failed File operation</param> /// <param name="file">The file to update</param> /// <param name="etag">Include the item's etag to prevent the completion of this operation if you don't have the must current version of the item. A BoxItemModifiedException will be thrown if the item is stale.</param> /// <param name="content">The file's data</param> public void Write(Action<File> onSuccess, Action<Error> onFailure, File file, byte[] content, string etag = null) { GuardFromNull(file, "file"); Write(onSuccess, onFailure, file.Id, file.Name, content, etag); }
/// <summary> /// Updates the content of a file /// </summary> /// <param name="onSuccess">Action to perform with the successfully written file</param> /// <param name="onFailure">Action to perform following a failed File operation</param> /// <param name="file">The file to update</param> /// <param name="etag">Include the item's etag to prevent the completion of this operation if you don't have the must current version of the item. A BoxItemModifiedException will be thrown if the item is stale.</param> /// <param name="content">A stream containing the file's data</param> public void Write(Action<File> onSuccess, Action<Error> onFailure, File file, Stream content, string etag = null) { GuardFromNull(file, "file"); Write(onSuccess, onFailure, file, ReadFully(content), etag); }
/// <summary> /// Updates the content of a file /// </summary> /// <param name="file">The file to update</param> /// <param name="content">The file's data</param> /// <param name="etag">Include the item's etag to prevent the completion of this operation if you don't have the must current version of the item. A BoxItemModifiedException will be thrown if the item is stale.</param> /// <returns>An updated file object</returns> public File Write(File file, byte[] content, string etag = null) { GuardFromNull(file, "file"); return Write(file.Id, file.Name, content, etag); }
/// <summary> /// Updates the content of a file /// </summary> /// <param name="file">The file to update</param> /// <param name="content">A stream containing the file's data</param> /// <param name="etag">Include the item's etag to prevent the completion of this operation if you don't have the must current version of the item. A BoxItemModifiedException will be thrown if the item is stale.</param> /// <returns>An updated file object</returns> public File Write(File file, Stream content, string etag = null) { return Write(file, ReadFully(content), etag); }
/// <summary> /// Updates a file's description /// </summary> /// <param name="onSuccess">Action to perform with the update file</param> /// <param name="onFailure">Action to perform following a failed File operation</param> /// <param name="file">The file to update</param> /// <param name="description">The new description for the file</param> /// <param name="fields">The properties that should be set on the returned File object. Type and Id are always set. If left null, all properties will be set, which can increase response time.</param> /// <param name="etag">Include the item's etag to prevent the completion of this operation if you don't have the must current version of the item. A BoxItemModifiedException will be thrown if the item is stale.</param> public void UpdateDescription(Action<File> onSuccess, Action<Error> onFailure, File file, string description, IEnumerable<FileField> fields = null, string etag = null) { GuardFromNull(file, "file"); UpdateFileDescription(onSuccess, onFailure, file.Id, description, fields, etag); }
/// <summary> /// Retrieve a range of the specified file content /// </summary> /// <param name="file">The file to read</param> /// <param name="firstByte">The first byte to of the range</param> /// <param name="lastByte">The last byte of the range</param> /// <returns>The raw file content range</returns> public byte[] Read(File file, int firstByte, int lastByte) { return Read(file.Id, firstByte, lastByte); }
/// <summary> /// Moves a file to the specified destination /// </summary> /// <param name="onSuccess">Action to perform with the moved file</param> /// <param name="onFailure">Action to perform following a failed File operation</param> /// <param name="file">The file to move</param> /// <param name="newParent">The destination folder</param> /// <param name="fields">The properties that should be set on the returned File object. Type and Id are always set. If left null, all properties will be set, which can increase response time.</param> /// <param name="etag">Include the item's etag to prevent the completion of this operation if you don't have the must current version of the item. A BoxItemModifiedException will be thrown if the item is stale.</param> public void Move(Action<File> onSuccess, Action<Error> onFailure, File file, Folder newParent, IEnumerable<FileField> fields = null, string etag = null) { GuardFromNull(newParent, "newParent"); Move(onSuccess, onFailure, file, newParent.Id, fields, etag); }
/// <summary> /// Deletes a file from the user's Box /// </summary> /// <param name="onSuccess">Action to perform following a successful delete</param> /// <param name="onFailure">Action to perform following a failed File operation</param> /// <param name="file">The file to delete</param> /// <param name="etag">Include the item's etag to prevent the completion of this operation if you don't have the must current version of the item. A BoxItemModifiedException will be thrown if the item is stale.</param> public void Delete(Action onSuccess, Action<Error> onFailure, File file, string etag = null) { GuardFromNull(file, "file"); DeleteFile(onSuccess, onFailure, file.Id, file.Etag); }
/// <summary> /// Retrieve the contents of the specified file /// </summary> /// <param name="file">The file to read</param> /// <param name="output">A stream to which the file contents will be written</param> public void Read(File file, Stream output) { GuardFromNull(file, "file"); Read(file.Id, output); }
/// <summary> /// Retrieves a file /// </summary> /// <param name="file">The file to get</param> /// <param name="fields">The properties that should be set on the returned File object. Type and Id are always set. If left null, all properties will be set, which can increase response time.</param> /// <param name="etag">Include the item's etag to prevent unnecessary response data if you already have the latest version of the item. A BoxItemNotModifiedException will be thrown if the item is up to date.</param> /// <returns>The fetched file</returns> public File Get(File file, IEnumerable<FileField> fields = null, string etag = null) { GuardFromNull(file, "file"); return GetFile(file.Id, fields, etag); }
/// <summary> /// Retrieve the contents of the specified file /// </summary> /// <param name="onSuccess">Action to perform with the file stream</param> /// <param name="onFailure">Action to perform following a failed File operation</param> /// <param name="file">The file to read</param> public void ReadToStream(Action<Stream> onSuccess, Action<Error> onFailure, File file) { GuardFromNull(file, "file"); ReadToStream(onSuccess, onFailure, file.Id); }
public byte[] GetThumbnail(File file, ThumbnailSize? minHeight = null, ThumbnailSize? minWidth = null, ThumbnailSize? maxHeight = null, ThumbnailSize? maxWidth = null, string extension = "png") { GuardFromNull(file, "file"); return GetThumbnail(file.Id, minHeight, minWidth, maxHeight, maxWidth, extension); }
/// <summary> /// Renames a file /// </summary> /// <param name="file">The file to rename</param> /// <param name="newName">The new name for the file</param> /// <param name="fields">The properties that should be set on the returned File object. Type and Id are always set. If left null, all properties will be set, which can increase response time.</param> /// <param name="etag">Include the item's etag to prevent the completion of this operation if you don't have the must current version of the item. A BoxItemModifiedException will be thrown if the item is stale.</param> /// <returns>The renamed file</returns> public File Rename(File file, string newName, IEnumerable<FileField> fields = null, string etag = null) { GuardFromNull(file, "file"); return RenameFile(file.Id, newName, fields, etag); }
/// <summary> /// Retrieves metadata about older versions of a file /// </summary> /// <param name="file">The file for which to retrieve metadata</param> /// <param name="fields">The properties that should be set on the returned VersionCollection.Entries. Type and Id are always set. If left null, all properties will be set, which can increase response time.</param> /// <returns>A collection of metadata objects</returns> public VersionCollection GetVersions(File file, IEnumerable<FileField> fields = null) { GuardFromNull(file, "file"); return GetVersions(file.Id); }
/// <summary> /// Creates a shared link to the specified file /// </summary> /// <param name="file">The file for which to create a shared link</param> /// <param name="sharedLink">The properties of the shared link</param> /// <param name="fields">The properties that should be set on the returned File object. Type and Id are always set. If left null, all properties will be set, which can increase response time.</param> /// <param name="etag">Include the item's etag to prevent the completion of this operation if you don't have the must current version of the item. A BoxItemModifiedException will be thrown if the item is stale.</param> /// <returns>A file object populated with the shared link</returns> /// <remarks>In order for File.SharedLink to be populated, you must either include Field.SharedLink in the fields list, or leave the list null</remarks> public File ShareLink(File file, SharedLink sharedLink, IEnumerable<FileField> fields = null, string etag = null) { GuardFromNull(file, "file"); return ShareFileLink(file.Id, sharedLink, fields, etag); }
/// <summary> /// Copies a file to the specified folder /// </summary> /// <param name="file">The file to copy</param> /// <param name="newParentId">The ID of the destination folder for the copied file</param> /// <param name="newName">The optional new name for the copied file</param> /// <param name="fields">The properties that should be set on the returned File object. Type and Id are always set. If left null, all properties will be set, which can increase response time.</param> /// <returns>The copied file</returns> public File Copy(File file, string newParentId, string newName = null, IEnumerable<FileField> fields = null) { GuardFromNull(file, "file"); return CopyFile(file.Id, newParentId, newName, fields); }
/// <summary> /// Update one or more of a file's name, description, parent, or shared link. /// </summary> /// <param name="file">The file to update</param> /// <param name="fields">The properties that should be set on the returned File object. Type and Id are always set. If left null, all properties will be set, which can increase response time.</param> /// <param name="etag">Include the item's etag to prevent the completion of this operation if you don't have the must current version of the item. A BoxItemModifiedException will be thrown if the item is stale.</param> /// <returns>The updated file</returns> public File Update(File file, IEnumerable<FileField> fields = null, string etag = null) { GuardFromNull(file, "file"); IRestRequest request = _requestHelper.Update(ResourceType.File, file.Id, etag, fields, file.Parent.Id, file.Name, file.Description, file.SharedLink); return _restClient.ExecuteAndDeserialize<File>(request); }
/// <summary> /// Retrieves metadata about older versions of a file /// </summary> /// <param name="onSuccess">Action to perform with the retrieved metadata</param> /// <param name="onFailure">Action to perform following a failed File operation</param> /// <param name="file">The file for which to retrieve metadata</param> /// <param name="fields">The properties that should be set on the returned VersionCollection.Entries. Type and Id are always set. If left null, all properties will be set, which can increase response time.</param> public void GetVersions(Action<VersionCollection> onSuccess, Action<Error> onFailure, File file, IEnumerable<FileField> fields = null) { GuardFromNull(file, "file"); GetVersions(onSuccess, onFailure, file.Id); }
/// <summary> /// Updates a file's description /// </summary> /// <param name="file">The file to update</param> /// <param name="description">The new description for the file</param> /// <param name="fields">The properties that should be set on the returned File object. Type and Id are always set. If left null, all properties will be set, which can increase response time.</param> /// <param name="etag">Include the item's etag to prevent the completion of this operation if you don't have the must current version of the item. A BoxItemModifiedException will be thrown if the item is stale.</param> /// <returns>The updated file</returns> public File UpdateDescription(File file, string description, IEnumerable<FileField> fields = null, string etag = null) { GuardFromNull(file, "file"); return UpdateFileDescription(file.Id, description, fields, etag); }
/// <summary> /// Moves a file to the specified destination /// </summary> /// <param name="file">The file to move</param> /// <param name="newParentId">The ID of the destination folder</param> /// <param name="fields">The properties that should be set on the returned File object. Type and Id are always set. If left null, all properties will be set, which can increase response time.</param> /// <param name="etag">Include the item's etag to prevent the completion of this operation if you don't have the must current version of the item. A BoxItemModifiedException will be thrown if the item is stale.</param> /// <returns>The moved file</returns> public File Move(File file, string newParentId, IEnumerable<FileField> fields = null, string etag = null) { GuardFromNull(file, "file"); return MoveFile(file.Id, newParentId, fields, etag); }
/// <summary> /// Moves a file to the specified destination /// </summary> /// <param name="file">The file to move</param> /// <param name="newParent">The destination folder</param> /// <param name="fields">The properties that should be set on the returned File object. Type and Id are always set. If left null, all properties will be set, which can increase response time.</param> /// <param name="etag">Include the item's etag to prevent the completion of this operation if you don't have the must current version of the item. A BoxItemModifiedException will be thrown if the item is stale.</param> /// <returns>The moved file</returns> public File Move(File file, Folder newParent, IEnumerable<FileField> fields = null, string etag = null) { GuardFromNull(newParent, "newParent"); return Move(file, newParent.Id, fields, etag); }
/// <summary> /// Add a comment to a file /// </summary> /// <param name="onSuccess">The action to perform with the added comment</param> /// <param name="onFailure">Action to perform following a failed Comment operation</param> /// <param name="file">The file on which to commment</param> /// <param name="message">The message to add</param> /// <param name="fields">The properties that should be set on the returned Comment. Type and Id are always set. If left null, all properties will be set, which can increase response time.</param> public void CreateComment(Action<Comment> onSuccess, Action<Error> onFailure, File file, string message, IEnumerable<CommentField> fields = null) { GuardFromNull(file, "file"); CreateFileComment(onSuccess, onFailure, file.Id, message, fields); }