Skip to content
This repository has been archived by the owner on Feb 22, 2023. It is now read-only.

Commit

Permalink
Merge pull request #28 from Auk14HP/PopuplateContentOnException
Browse files Browse the repository at this point in the history
Populate ResponseContentPreview when creating a WebServiceException from an AutoWebServiceResponse.
  • Loading branch information
ddunkin authored Aug 18, 2021
2 parents a209ad7 + a9ba587 commit 4edd1ea
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 17 deletions.
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project>

<PropertyGroup>
<VersionPrefix>0.10.0</VersionPrefix>
<VersionPrefix>0.11.0</VersionPrefix>
<LangVersion>9.0</LangVersion>
<Nullable>enable</Nullable>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
Expand Down
4 changes: 4 additions & 0 deletions ReleaseNotes.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Release Notes

## 0.11.0

* Populate `AutoWebServiceResponse.ResponseContentPreview` when creating a `WebServiceException` from the response.

## 0.10.0

* Support additional json content types (e.g., application/vnd.api+json).
Expand Down
19 changes: 3 additions & 16 deletions src/Faithlife.WebRequests/Json/AutoWebServiceRequest.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
using System;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using Faithlife.Json;
Expand Down Expand Up @@ -57,14 +55,13 @@ public AutoWebServiceRequest(Uri uri)
protected override async Task<bool> HandleResponseCoreAsync(WebServiceResponseHandlerInfo<TResponse> info)
{
var response = CreateResponse()!;
Type responseType = response.GetType();
var webResponse = info.WebResponse!;

// find specific status code property
bool isStatusCodeHandled = false;
HttpStatusCode statusCode = webResponse.StatusCode;
string statusCodeText = statusCode.ToString();
var resultProperty = GetProperty(responseType, statusCodeText);
var resultProperty = AutoWebServiceResponseUtility.GetStatusCodeProperty(response, statusCodeText);
object? content = null;

if (resultProperty?.CanWrite ?? false)
Expand All @@ -75,7 +72,7 @@ protected override async Task<bool> HandleResponseCoreAsync(WebServiceResponseHa
isStatusCodeHandled = true;
}

var statusCodeProperty = GetProperty(responseType, "StatusCode");
var statusCodeProperty = AutoWebServiceResponseUtility.GetStatusCodeProperty(response, "StatusCode");
if (statusCodeProperty?.CanWrite ?? false)
{
Type statusCodePropertyType = statusCodeProperty.PropertyType;
Expand All @@ -101,7 +98,7 @@ protected override async Task<bool> HandleResponseCoreAsync(WebServiceResponseHa
#pragma warning disable CA1307 // Specify StringComparison for clarity
string propertyName = headerName.Replace("-", "");
#pragma warning restore CA1307 // Specify StringComparison for clarity
var headerProperty = GetProperty(responseType, propertyName);
var headerProperty = AutoWebServiceResponseUtility.GetStatusCodeProperty(response, propertyName);
if (headerProperty?.CanWrite ?? false)
{
// get header text
Expand Down Expand Up @@ -139,16 +136,6 @@ protected override async Task<bool> HandleResponseCoreAsync(WebServiceResponseHa
return true;
}

private static PropertyInfo? GetProperty(Type type, string propertyName)
{
var property = type.GetRuntimeProperties().FirstOrDefault(x => x.GetMethod is object && !x.GetMethod.IsStatic && string.Equals(x.Name, propertyName, StringComparison.OrdinalIgnoreCase));

if (property is object && property.SetMethod is null)
property = property.DeclaringType.GetRuntimeProperty(property.Name);

return property;
}

private async Task<object> ReadContentAsAsync(WebServiceResponseHandlerInfo<TResponse> info, Type propertyType)
{
var webResponse = info.WebResponse!;
Expand Down
15 changes: 15 additions & 0 deletions src/Faithlife.WebRequests/Json/AutoWebServiceResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Net;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using Faithlife.Json;

namespace Faithlife.WebRequests.Json
{
Expand All @@ -16,6 +17,20 @@ public abstract class AutoWebServiceResponse
/// <returns>A new exception for the response.</returns>
public WebServiceException CreateException(string? message = null, Exception? innerException = null)
{
if (m_responseContentPreview == null)
{
var statusCodeProperty = AutoWebServiceResponseUtility.GetStatusCodeProperty(this, m_responseStatusCode?.ToString());

if (statusCodeProperty?.CanRead == true)
{
const int maxPreviewContentLength = 1000;
m_responseContentPreview = JsonUtility.ToJson(statusCodeProperty.GetValue(this));

if (m_responseContentPreview.Length > maxPreviewContentLength)
m_responseContentPreview = m_responseContentPreview.Substring(0, maxPreviewContentLength) + "...";
}
}

return new WebServiceException(
message: message,
requestMethod: m_requestMethod,
Expand Down
24 changes: 24 additions & 0 deletions src/Faithlife.WebRequests/Json/AutoWebServiceResponseUtility.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

namespace Faithlife.WebRequests.Json
{
Expand Down Expand Up @@ -39,5 +41,27 @@ public static void VerifyResultIsExpected<TResponse, TProperty>(this TResponse r
{
GetExpectedResult(response, getProperty);
}

/// <summary>
/// Returns PropertyInfo of the desired property.
/// </summary>
/// <param name="response">The response.</param>
/// <param name="propertyName">The name of the property.</param>
/// <returns>The PropertyInfo of the property.</returns>
internal static PropertyInfo? GetStatusCodeProperty(object response, string? propertyName)
{
if (propertyName == null)
return null;

var property = response.GetType().GetRuntimeProperties().FirstOrDefault(x =>
x.GetMethod is object &&
!x.GetMethod.IsStatic &&
string.Equals(x.Name, propertyName, StringComparison.OrdinalIgnoreCase));

if (property is object && property.SetMethod is null)
property = property.DeclaringType.GetRuntimeProperty(property.Name);

return property;
}
}
}

0 comments on commit 4edd1ea

Please sign in to comment.