/// <summary> /// Creates a new instance of an UnexpectedStatusCodeError. /// </summary> /// <param name="response">The HttpResponseMessage to convert to an error.</param> /// <param name="target">The target of the error.</param> /// <returns>The new UnexpectedStatusCodeError.</returns> public static async Task <UnexpectedStatusCodeError> CreateAsync(HttpResponseMessage response, string target) { if (response is null) { throw new ArgumentNullException(nameof(response)); } var error = new UnexpectedStatusCodeError(); error.Code = "UnexpectedStatusCode"; error.Target = target; error.Message = $"The HTTP status code of the response was not expected ({(int)response.StatusCode})."; if (response.Content != null) { var stringContent = await response.Content.ReadAsStringAsync().ConfigureAwait(false); error.Content = stringContent; } error.ReasonPhrase = response.ReasonPhrase; error.StatusCode = response.StatusCode; return(error); }
/// <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>()); } } }
/// <summary> /// Runs an Anti-Virus scan on 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="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 <AntiVirusScanResult> > AntiVirusScanFileResultAsync(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.AntiVirusScanFileHttpResponseAsync(libraryId, path, tenantId, cancellationToken).ConfigureAwait(false); using (response) { switch (response.StatusCode) { case HttpStatusCode.OK: return(Result.Create(await response.DeserializeJsonContentAsync <AntiVirusScanResult>().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 <AntiVirusScanResult>()); } default: { UnexpectedStatusCodeError error = await UnexpectedStatusCodeError.CreateAsync(response, $"{nameof(IDocumentLibraryClient)}.{nameof(AntiVirusScanFileResultAsync)}").ConfigureAwait(false); return(error.ToResult <AntiVirusScanResult>()); } } } }
/// <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> > >()); } } } }