Ejemplo n.º 1
0
        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);
            }
        }
Ejemplo n.º 2
0
        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));
            }
        }
    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));
            }
    }
Ejemplo n.º 4
0
 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);
 }
Ejemplo n.º 10
0
 /// <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);
 }
Ejemplo n.º 11
0
 /// <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);
 }
Ejemplo n.º 12
0
 /// <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);
 }
Ejemplo n.º 13
0
 /// <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);
 }
Ejemplo n.º 14
0
 /// <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);
 }
Ejemplo n.º 15
0
 /// <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);
 }
Ejemplo n.º 16
0
 /// <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);
 }
Ejemplo n.º 17
0
 /// <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);
 }
Ejemplo n.º 18
0
 /// <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);
 }
Ejemplo n.º 19
0
 /// <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);
 }
Ejemplo n.º 20
0
 /// <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);
 }
Ejemplo n.º 21
0
 /// <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);
 }
Ejemplo n.º 22
0
 /// <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);
 }
Ejemplo n.º 23
0
 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);
 }
Ejemplo n.º 24
0
 /// <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);
 }
Ejemplo n.º 25
0
 /// <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);
 }
Ejemplo n.º 26
0
 /// <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);
 }
Ejemplo n.º 27
0
 /// <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);
 }
Ejemplo n.º 28
0
 /// <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);
 }
Ejemplo n.º 29
0
 /// <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);
 }
Ejemplo n.º 30
0
 /// <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);
 }
Ejemplo n.º 31
0
 /// <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);
 }
Ejemplo n.º 32
0
 /// <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);
 }
Ejemplo n.º 33
0
 /// <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);
 }