コード例 #1
0
    public async Task ValidateRequest_Default_Success()
    {
        string location  = Guid.NewGuid().ToString();
        string name      = Guid.NewGuid().ToString();
        string libraryId = Guid.NewGuid().ToString();
        string tenantId  = Guid.NewGuid().ToString();

        var           mockHttp = new MockHttpMessageHandler();
        MockedRequest request  = mockHttp
                                 .When(HttpMethod.Get, "*/libraries")
                                 .Respond("application/json", $"[{{'libraryId': '{libraryId}', 'tenantId': '{tenantId}', 'name': '{name}', 'location': '{location}'}}]");

        IServiceProvider       services     = ConfigureServices(mockHttp);
        IDocumentLibraryClient docLibClient = services.GetRequiredService <IDocumentLibraryClient>();

        ICollection <Library> libraries = await docLibClient.ListLibrariesAsync().ConfigureAwait(false);

        mockHttp.GetMatchCount(request).Should().Be(1);

        libraries.Should().NotBeNullOrEmpty();
        libraries.Should().HaveCount(1);

        Library library = libraries.Single();

        library.Should().NotBeNull();
        library.TenantId.Should().Be(tenantId);
        library.Name.Should().Be(name);
    }
コード例 #2
0
        /// <summary>
        /// Gets a library.
        /// </summary>
        /// <param name="documentLibraryClient">The IDocumentLibraryClient instance.</param>
        /// <param name="libraryId">The library ID.</param>
        /// <param name="tenantId">Optional. Specifies which tenant to use.</param>
        /// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
        /// <returns>The library.</returns>
        public static async Task <Library> GetLibraryAsync(this IDocumentLibraryClient documentLibraryClient, Guid libraryId, Guid?tenantId = null, CancellationToken cancellationToken = default)
        {
            if (documentLibraryClient is null)
            {
                throw new ArgumentNullException(nameof(documentLibraryClient));
            }

            Result <Library> result = await documentLibraryClient.GetLibraryResultAsync(libraryId, tenantId, cancellationToken).ConfigureAwait(false);

            if (result.IsSuccess)
            {
                return(result.Value);
            }

            throw ApiException.Create(result.Error);
        }
コード例 #3
0
        /// <summary>
        /// Uploads a file.
        /// </summary>
        /// <param name="documentLibraryClient">The IDocumentLibraryClient instance.</param>
        /// <param name="libraryId">The library ID.</param>
        /// <param name="path">The path to the file.</param>
        /// <param name="fileData">The file data to upload.</param>
        /// <param name="contentType">Optional. The content type to assign this file.</param>
        /// <param name="overwrite">A value that indicates whether to overwrite the file if it already exists.</param>
        /// <param name="tenantId">Optional. Specifies which tenant to use.</param>
        /// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
        /// <returns>The uploaded file.</returns>
        public static async Task <LibraryFileInfo> UploadFileAsync(this IDocumentLibraryClient documentLibraryClient, Guid libraryId, CloudPath path, Stream fileData, string?contentType = null, bool overwrite = false, Guid?tenantId = null, CancellationToken cancellationToken = default)
        {
            if (documentLibraryClient is null)
            {
                throw new ArgumentNullException(nameof(documentLibraryClient));
            }

            Result <LibraryFileInfo> result = await documentLibraryClient.UploadFileResultAsync(libraryId, path, fileData, contentType, overwrite, tenantId, cancellationToken).ConfigureAwait(false);

            if (result.IsSuccess)
            {
                return(result.Value);
            }

            throw ApiException.Create(result.Error);
        }
コード例 #4
0
        /// <summary>
        /// Imports files into a library.
        /// </summary>
        /// <param name="documentLibraryClient">The IDocumentLibraryClient instance.</param>
        /// <param name="libraryId">The library ID.</param>
        /// <param name="importRecords">The records to import.</param>
        /// <param name="path">The path to the file.</param>
        /// <param name="tenantId">Optional. Specifies which tenant to use.</param>
        /// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
        /// <returns>The uploaded file.</returns>
        public static async Task <ICollection <LibraryFileInfo> > ImportFilesAsync(this IDocumentLibraryClient documentLibraryClient, Guid libraryId, IEnumerable <ImportRecord> importRecords, CloudPath?path = null, Guid?tenantId = null, CancellationToken cancellationToken = default)
        {
            if (documentLibraryClient is null)
            {
                throw new ArgumentNullException(nameof(documentLibraryClient));
            }

            Result <ICollection <LibraryFileInfo> > result = await documentLibraryClient.ImportFilesResultAsync(libraryId, importRecords, path, tenantId, cancellationToken).ConfigureAwait(false);

            if (result.IsSuccess)
            {
                return(result.Value);
            }

            throw ApiException.Create(result.Error);
        }
コード例 #5
0
        /// <summary>
        /// Moves a file or directory from a source location in the library to a target location.
        /// </summary>
        /// <param name="documentLibraryClient">The IDocumentLibraryClient instance.</param>
        /// <param name="libraryId">The library ID.</param>
        /// <param name="source">The path to the source file or directory.</param>
        /// <param name="target">The path to the target file or directory.</param>
        /// <param name="tenantId">Optional. Specifies which tenant to use.</param>
        /// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
        /// <returns>Success if there were no errors.</returns>
        public static async Task MoveAsync(this IDocumentLibraryClient documentLibraryClient, Guid libraryId, CloudPath source, CloudPath target, Guid?tenantId = null, CancellationToken cancellationToken = default)
        {
            if (documentLibraryClient is null)
            {
                throw new ArgumentNullException(nameof(documentLibraryClient));
            }

            Result result = await documentLibraryClient.MoveResultAsync(libraryId, source, target, tenantId, cancellationToken).ConfigureAwait(false);

            if (result.IsSuccess)
            {
                return;
            }

            throw ApiException.Create(result.Error);
        }
コード例 #6
0
        /// <summary>
        /// Lists directory children.
        /// </summary>
        /// <param name="documentLibraryClient">The IDocumentLibraryClient instance.</param>
        /// <param name="libraryId">The library ID.</param>
        /// <param name="path">The path to the directory.</param>
        /// <param name="tenantId">Optional. Specifies which tenant to use.</param>
        /// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
        /// <returns>The directory children.</returns>
        public static async Task <ICollection <LibraryItemInfo> > ListChildrenAsync(this IDocumentLibraryClient documentLibraryClient, Guid libraryId, CloudPath path, Guid?tenantId = null, CancellationToken cancellationToken = default)
        {
            if (documentLibraryClient is null)
            {
                throw new ArgumentNullException(nameof(documentLibraryClient));
            }

            Result <ICollection <LibraryItemInfo> > result = await documentLibraryClient.ListChildrenResultAsync(libraryId, path, tenantId, cancellationToken).ConfigureAwait(false);

            if (result.IsSuccess)
            {
                return(result.Value);
            }

            throw ApiException.Create(result.Error);
        }
コード例 #7
0
        /// <summary>
        /// Removes a single tag associated with a file.
        /// </summary>
        /// <param name="documentLibraryClient">The IDocumentLibraryClient instance.</param>
        /// <param name="libraryId">The library ID.</param>
        /// <param name="path">The path to the file.</param>
        /// <param name="key">The meatadata key.</param>
        /// <param name="tenantId">Optional. Specifies which tenant to use.</param>
        /// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
        /// <returns>Success if there were no errors.</returns>
        public static async Task RemoveFileTagAsync(this IDocumentLibraryClient documentLibraryClient, Guid libraryId, CloudPath path, string key, Guid?tenantId = null, CancellationToken cancellationToken = default)
        {
            if (documentLibraryClient is null)
            {
                throw new ArgumentNullException(nameof(documentLibraryClient));
            }

            Result result = await documentLibraryClient.RemoveFileTagResultAsync(libraryId, path, key, tenantId, cancellationToken).ConfigureAwait(false);

            if (result.IsSuccess)
            {
                return;
            }

            throw ApiException.Create(result.Error);
        }
コード例 #8
0
        /// <summary>
        /// Downloads the file contents.
        /// </summary>
        /// <param name="documentLibraryClient">The IDocumentLibraryClient instance.</param>
        /// <param name="libraryId">The library ID.</param>
        /// <param name="path">The path to the file.</param>
        /// <param name="tenantId">Optional. Specifies which tenant to use.</param>
        /// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
        /// <returns>The http file stream.</returns>
        public static async Task <HttpFileResponse> DownloadContentAsync(this IDocumentLibraryClient documentLibraryClient, Guid libraryId, CloudPath path, Guid?tenantId = null, CancellationToken cancellationToken = default)
        {
            if (documentLibraryClient is null)
            {
                throw new ArgumentNullException(nameof(documentLibraryClient));
            }

            Result <HttpFileResponse> result = await documentLibraryClient.DownloadContentResultAsync(libraryId, path, tenantId, cancellationToken).ConfigureAwait(false);

            if (result.IsSuccess)
            {
                return(result.Value);
            }

            throw ApiException.Create(result.Error);
        }
コード例 #9
0
        /// <summary>
        /// Uploads a file.
        /// </summary>
        /// <param name="documentLibraryClient">The IDocumentLibraryClient instance.</param>
        /// <param name="libraryId">The library ID.</param>
        /// <param name="path">The path to the file.</param>
        /// <param name="fileData">The file data to upload.</param>
        /// <param name="contentType">Optional. The content type to assign this file.</param>
        /// <param name="overwrite">A value that indicates whether to overwrite the file if it already exists.</param>
        /// <param name="tenantId">Optional. Specifies which tenant to use.</param>
        /// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
        /// <returns>Either the file or an error.</returns>
        public static async Task <Result <LibraryFileInfo> > UploadFileResultAsync(this IDocumentLibraryClient documentLibraryClient, Guid libraryId, CloudPath path, Stream fileData, string?contentType = null, bool overwrite = false, Guid?tenantId = null, CancellationToken cancellationToken = default)
        {
            if (documentLibraryClient is null)
            {
                throw new ArgumentNullException(nameof(documentLibraryClient));
            }

            HttpResponseMessage response = await documentLibraryClient.UploadFileHttpResponseAsync(libraryId, path, fileData, contentType, overwrite, tenantId, cancellationToken).ConfigureAwait(false);

            using (response)
            {
                switch (response.StatusCode)
                {
                case HttpStatusCode.OK:
                    return(Result.Create(await response.DeserializeJsonContentAsync <LibraryFileInfo>().ConfigureAwait(false)));

                case HttpStatusCode.NoContent:
                    return(default);

                case HttpStatusCode.BadRequest:
                case HttpStatusCode.InternalServerError:
                {
                    ErrorResponse errorResponse = await response.DeserializeJsonContentAsync <ErrorResponse>().ConfigureAwait(false);

                    return(errorResponse.Error.ToResult <LibraryFileInfo>());
                }

                default:
                {
                    UnexpectedStatusCodeError error = await UnexpectedStatusCodeError.CreateAsync(response, $"{nameof(IDocumentLibraryClient)}.{nameof(MoveResultAsync)}").ConfigureAwait(false);

                    return(error.ToResult <LibraryFileInfo>());
                }
                }
            }
        }
コード例 #10
0
        /// <summary>
        /// Moves a file or directory from a source location in the library to a target location.
        /// </summary>
        /// <param name="documentLibraryClient">The IDocumentLibraryClient instance.</param>
        /// <param name="libraryId">The library ID.</param>
        /// <param name="source">The path to the source file or directory.</param>
        /// <param name="target">The path to the target file or directory.</param>
        /// <param name="tenantId">Optional. Specifies which tenant to use.</param>
        /// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
        /// <returns>Either a succesful result or an error.</returns>
        public static async Task <Result> MoveResultAsync(this IDocumentLibraryClient documentLibraryClient, Guid libraryId, CloudPath source, CloudPath target, Guid?tenantId = null, CancellationToken cancellationToken = default)
        {
            if (documentLibraryClient is null)
            {
                throw new ArgumentNullException(nameof(documentLibraryClient));
            }

            HttpResponseMessage response = await documentLibraryClient.MoveHttpResponseAsync(libraryId, source, target, tenantId, cancellationToken).ConfigureAwait(false);

            using (response)
            {
                switch (response.StatusCode)
                {
                case HttpStatusCode.OK:
                    return(Result.Default);

                case HttpStatusCode.NoContent:
                    return(default);

                case HttpStatusCode.BadRequest:
                case HttpStatusCode.InternalServerError:
                {
                    ErrorResponse errorResponse = await response.DeserializeJsonContentAsync <ErrorResponse>().ConfigureAwait(false);

                    return(errorResponse.Error.ToResult());
                }

                default:
                {
                    UnexpectedStatusCodeError error = await UnexpectedStatusCodeError.CreateAsync(response, $"{nameof(IDocumentLibraryClient)}.{nameof(MoveResultAsync)}").ConfigureAwait(false);

                    return(error.ToResult());
                }
                }
            }
        }
コード例 #11
0
        /// <summary>
        /// Imports files into a library.
        /// </summary>
        /// <param name="documentLibraryClient">The IDocumentLibraryClient instance.</param>
        /// <param name="libraryId">The library ID.</param>
        /// <param name="importRecords">The records to import.</param>
        /// <param name="path">The path to the file.</param>
        /// <param name="tenantId">Optional. Specifies which tenant to use.</param>
        /// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
        /// <returns>Either the imported files or an error.</returns>
        public static async Task <Result <ICollection <LibraryFileInfo> > > ImportFilesResultAsync(this IDocumentLibraryClient documentLibraryClient, Guid libraryId, IEnumerable <ImportRecord> importRecords, CloudPath?path = null, Guid?tenantId = null, CancellationToken cancellationToken = default)
        {
            if (documentLibraryClient is null)
            {
                throw new ArgumentNullException(nameof(documentLibraryClient));
            }

            HttpResponseMessage response = await documentLibraryClient.ImportFilesHttpResponseAsync(libraryId, importRecords, path, tenantId, cancellationToken).ConfigureAwait(false);

            using (response)
            {
                switch (response.StatusCode)
                {
                case HttpStatusCode.OK:
                    return(Result.Create(await response.DeserializeJsonContentAsync <ICollection <LibraryFileInfo> >().ConfigureAwait(false)));

                case HttpStatusCode.NoContent:
                    return(default);

                case HttpStatusCode.BadRequest:
                case HttpStatusCode.InternalServerError:
                {
                    ErrorResponse errorResponse = await response.DeserializeJsonContentAsync <ErrorResponse>().ConfigureAwait(false);

                    return(errorResponse.Error.ToResult <ICollection <LibraryFileInfo> >());
                }

                default:
                {
                    UnexpectedStatusCodeError error = await UnexpectedStatusCodeError.CreateAsync(response, $"{nameof(IDocumentLibraryClient)}.{nameof(ImportFilesResultAsync)}").ConfigureAwait(false);

                    return(error.ToResult <ICollection <LibraryFileInfo> >());
                }
                }
            }
        }
コード例 #12
0
        /// <summary>
        /// Downloads the file contents.
        /// </summary>
        /// <param name="documentLibraryClient">The IDocumentLibraryClient instance.</param>
        /// <param name="libraryId">The library ID.</param>
        /// <param name="path">The path to the file.</param>
        /// <param name="tenantId">Optional. Specifies which tenant to use.</param>
        /// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
        /// <returns>Either the http file stream or an error.</returns>
        public static async Task <Result <HttpFileResponse> > DownloadContentResultAsync(this IDocumentLibraryClient documentLibraryClient, Guid libraryId, CloudPath path, Guid?tenantId = null, CancellationToken cancellationToken = default)
        {
            if (documentLibraryClient is null)
            {
                throw new ArgumentNullException(nameof(documentLibraryClient));
            }

            HttpResponseMessage response = await documentLibraryClient.DownloadContentHttpResponseAsync(libraryId, path, tenantId, cancellationToken).ConfigureAwait(false);

            switch (response.StatusCode)
            {
            case HttpStatusCode.OK:
            {
                Result <HttpFileResponse> fileResponse = await HttpFileResponse.CreateAsync(response).ConfigureAwait(false);

                return(fileResponse);
            }

            case HttpStatusCode.NoContent:
                response.Dispose();
                return(default);

            case HttpStatusCode.BadRequest:
            case HttpStatusCode.InternalServerError:
            {
                try
                {
                    if (response.IsContentProblem())
                    {
                        HttpProblem problem = await response.DeserializeJsonContentAsync <HttpProblem>().ConfigureAwait(false);

                        return(new HttpProblemError(problem).ToResult <HttpFileResponse>());
                    }
                    else
                    {
                        ErrorResponse errorResponse = await response.DeserializeJsonContentAsync <ErrorResponse>().ConfigureAwait(false);

                        return(errorResponse.Error.ToResult <HttpFileResponse>());
                    }
                }
                finally
                {
                    response.Dispose();
                }
            }

            default:
            {
                UnexpectedStatusCodeError error = await UnexpectedStatusCodeError.CreateAsync(response, $"{nameof(IDocumentLibraryClient)}.{nameof(DownloadContentResultAsync)}").ConfigureAwait(false);

                response.Dispose();
                return(error.ToResult <HttpFileResponse>());
            }
            }
        }
コード例 #13
0
        /// <summary>
        /// Searches a library for files according to the query.
        /// </summary>
        /// <param name="documentLibraryClient">The IDocumentLibraryClient instance.</param>
        /// <param name="libraryId">The library ID.</param>
        /// <param name="query">The search query.</param>
        /// <param name="tenantId">Optional. Specifies which tenant to use.</param>
        /// <param name="filter">Extra filters to aplly to the search.</param>
        /// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
        /// <returns>Either the search results or an error.</returns>
        public static async Task <Result <ICollection <SearchResult <LibrarySearchResult> > > > SearchLibraryResultAsync(this IDocumentLibraryClient documentLibraryClient, Guid libraryId, string query, Guid?tenantId = null, string?filter = null, CancellationToken cancellationToken = default)
        {
            if (documentLibraryClient is null)
            {
                throw new ArgumentNullException(nameof(documentLibraryClient));
            }

            HttpResponseMessage response = await documentLibraryClient.SearchLibraryHttpResponseAsync(libraryId, query, tenantId, filter, cancellationToken).ConfigureAwait(false);

            using (response)
            {
                switch (response.StatusCode)
                {
                case HttpStatusCode.OK:
                    return(Result.Create(await response.DeserializeJsonContentAsync <ICollection <SearchResult <LibrarySearchResult> > >().ConfigureAwait(false)));

                case HttpStatusCode.NoContent:
                    return(default);

                case HttpStatusCode.BadRequest:
                case HttpStatusCode.InternalServerError:
                {
                    ErrorResponse errorResponse = await response.DeserializeJsonContentAsync <ErrorResponse>().ConfigureAwait(false);

                    return(errorResponse.Error.ToResult <ICollection <SearchResult <LibrarySearchResult> > >());
                }

                default:
                {
                    UnexpectedStatusCodeError error = await UnexpectedStatusCodeError.CreateAsync(response, $"{nameof(IDocumentLibraryClient)}.{nameof(SearchLibraryResultAsync)}").ConfigureAwait(false);

                    return(error.ToResult <ICollection <SearchResult <LibrarySearchResult> > >());
                }
                }
            }
        }
コード例 #14
0
 public Worker(IDocumentLibraryClient client, ILogger <Worker> logger)
 {
     this.client = client;
     _logger     = logger;
 }
コード例 #15
0
        /// <summary>
        /// Searches a library for files according to the query.
        /// </summary>
        /// <param name="documentLibraryClient">The IDocumentLibraryClient instance.</param>
        /// <param name="libraryId">The library ID.</param>
        /// <param name="query">The search query.</param>
        /// <param name="tenantId">Optional. Specifies which tenant to use.</param>
        /// <param name="filter">Extra filters to aplly to the search.</param>
        /// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
        /// <returns>The search results.</returns>
        public static async Task <ICollection <SearchResult <LibrarySearchResult> > > SearchLibraryAsync(this IDocumentLibraryClient documentLibraryClient, Guid libraryId, string query, Guid?tenantId = null, string?filter = null, CancellationToken cancellationToken = default)
        {
            if (documentLibraryClient is null)
            {
                throw new ArgumentNullException(nameof(documentLibraryClient));
            }

            Result <ICollection <SearchResult <LibrarySearchResult> > > result = await documentLibraryClient.SearchLibraryResultAsync(libraryId, query, tenantId, filter, cancellationToken).ConfigureAwait(false);

            if (result.IsSuccess)
            {
                return(result.Value);
            }

            throw ApiException.Create(result.Error);
        }