From af88b087a0f0f973898c47209492084b1f87ac69 Mon Sep 17 00:00:00 2001 From: Robin Date: Tue, 24 Jan 2017 14:00:49 +0100 Subject: [PATCH] Added better info when the content cannot be mapped, write the result from the server to the log if it's readable by humans. [release] --- .../HttpContentExtensions.cs | 28 +++++++++++++++---- appveyor.yml | 3 +- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/Dapplo.HttpExtensions/HttpContentExtensions.cs b/Dapplo.HttpExtensions/HttpContentExtensions.cs index 2644d80..bc7ab91 100644 --- a/Dapplo.HttpExtensions/HttpContentExtensions.cs +++ b/Dapplo.HttpExtensions/HttpContentExtensions.cs @@ -22,6 +22,7 @@ #region using using System; +using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Http; @@ -44,7 +45,18 @@ namespace Dapplo.HttpExtensions public static class HttpContentExtensions { private static readonly LogSource Log = new LogSource(); - + /// + /// Specify the supported content types + /// + private static readonly IList ReadableContentTypes = new List + { + MediaTypes.Txt.EnumValueOf(), + MediaTypes.Html.EnumValueOf(), + MediaTypes.Json.EnumValueOf(), + MediaTypes.WwwFormUrlEncoded.EnumValueOf(), + MediaTypes.Xml.EnumValueOf(), + MediaTypes.XmlReadable.EnumValueOf() + }; /// /// Extension method reading the httpContent to a Typed object, depending on the returned content-type /// Currently we support: @@ -96,7 +108,9 @@ public static async Task GetAsAsync(this HttpContent httpContent, Type r // For everything that comes here, a fitting converter should be written, or the ValidateResponseContentType can be set to false var contentType = httpContent.GetContentType(); Log.Error().WriteLine($"Unsupported result type {resultType} & {contentType} combination."); - if (MediaTypes.Txt.EnumValueOf() == contentType && Log.IsErrorEnabled()) + + // Only write when the result is something readable + if (ReadableContentTypes.Contains(contentType)) { Log.Error().WriteLine("Unprocessable result: {0}", await httpContent.ReadAsStringAsync().ConfigureAwait(false)); } @@ -120,11 +134,13 @@ public static async Task GetContentStream(this HttpContent httpContent) if (httpBehaviour.UseProgressStream && contentLength > 0) { long position = 0; - var progressStream = new ProgressStream(contentStream); - progressStream.BytesRead = (sender, eventArgs) => + var progressStream = new ProgressStream(contentStream) { - position += eventArgs.BytesMoved; - httpBehaviour.DownloadProgress?.Invoke((float)position / contentLength); + BytesRead = (sender, eventArgs) => + { + position += eventArgs.BytesMoved; + httpBehaviour.DownloadProgress?.Invoke((float) position/contentLength); + } }; contentStream = progressStream; } diff --git a/appveyor.yml b/appveyor.yml index b5c570b..a33df43 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -12,8 +12,6 @@ before_build: - ps: .\SetVersion.ps1 - appveyor-retry dotnet restore -v Minimal - ps: if(-not $env:APPVEYOR_PULL_REQUEST_NUMBER) { $env:is_not_pr = "true"; } -before_package: -- ps: if(-not $env:APPVEYOR_PULL_REQUEST_NUMBER) { GitLink .\ } after_test: - ps: .\CreateCoverage.ps1 - pip install codecov @@ -38,6 +36,7 @@ build_script: - dotnet build Dapplo.HttpExtensions -c %CONFIGURATION% --no-dependencies - dotnet build Dapplo.HttpExtensions.Tests -c %CONFIGURATION% --no-dependencies after_build: +- ps: if(-not $env:APPVEYOR_PULL_REQUEST_NUMBER) { GitLink .\ } - dotnet pack Dapplo.HttpExtensions -c %CONFIGURATION% --no-build -o artifacts notifications: - provider: Email