Skip to content

Commit

Permalink
Added better info when the content cannot be mapped, write the result…
Browse files Browse the repository at this point in the history
… from the server to the log if it's readable by humans.

[release]
  • Loading branch information
Lakritzator committed Jan 24, 2017
1 parent 01ec439 commit af88b08
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
28 changes: 22 additions & 6 deletions Dapplo.HttpExtensions/HttpContentExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#region using

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
Expand All @@ -44,7 +45,18 @@ namespace Dapplo.HttpExtensions
public static class HttpContentExtensions
{
private static readonly LogSource Log = new LogSource();

/// <summary>
/// Specify the supported content types
/// </summary>
private static readonly IList<string> ReadableContentTypes = new List<string>
{
MediaTypes.Txt.EnumValueOf(),
MediaTypes.Html.EnumValueOf(),
MediaTypes.Json.EnumValueOf(),
MediaTypes.WwwFormUrlEncoded.EnumValueOf(),
MediaTypes.Xml.EnumValueOf(),
MediaTypes.XmlReadable.EnumValueOf()
};
/// <summary>
/// Extension method reading the httpContent to a Typed object, depending on the returned content-type
/// Currently we support:
Expand Down Expand Up @@ -96,7 +108,9 @@ public static async Task<object> 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));
}
Expand All @@ -120,11 +134,13 @@ public static async Task<Stream> 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;
}
Expand Down
3 changes: 1 addition & 2 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit af88b08

Please sign in to comment.