From bfdab06b635975656c7d4ec879301f827c64d5fe Mon Sep 17 00:00:00 2001 From: James Jensen Date: Mon, 16 Aug 2021 10:55:47 -0700 Subject: [PATCH 1/3] Populate ResponseContentPreview when creating a WebServiceException from an AutoWebServiceResponse. --- .../Json/AutoWebServiceRequest.cs | 19 +++---------------- .../Json/AutoWebServiceResponse.cs | 15 +++++++++++++++ .../Json/AutoWebServiceResponseUtility.cs | 19 +++++++++++++++++++ 3 files changed, 37 insertions(+), 16 deletions(-) diff --git a/src/Faithlife.WebRequests/Json/AutoWebServiceRequest.cs b/src/Faithlife.WebRequests/Json/AutoWebServiceRequest.cs index 80e12d5..9ca0a12 100644 --- a/src/Faithlife.WebRequests/Json/AutoWebServiceRequest.cs +++ b/src/Faithlife.WebRequests/Json/AutoWebServiceRequest.cs @@ -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; @@ -57,14 +55,13 @@ public AutoWebServiceRequest(Uri uri) protected override async Task HandleResponseCoreAsync(WebServiceResponseHandlerInfo 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) @@ -75,7 +72,7 @@ protected override async Task HandleResponseCoreAsync(WebServiceResponseHa isStatusCodeHandled = true; } - var statusCodeProperty = GetProperty(responseType, "StatusCode"); + var statusCodeProperty = AutoWebServiceResponseUtility.GetStatusCodeProperty(response, "StatusCode"); if (statusCodeProperty?.CanWrite ?? false) { Type statusCodePropertyType = statusCodeProperty.PropertyType; @@ -101,7 +98,7 @@ protected override async Task 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 @@ -139,16 +136,6 @@ protected override async Task 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 ReadContentAsAsync(WebServiceResponseHandlerInfo info, Type propertyType) { var webResponse = info.WebResponse!; diff --git a/src/Faithlife.WebRequests/Json/AutoWebServiceResponse.cs b/src/Faithlife.WebRequests/Json/AutoWebServiceResponse.cs index 15134d4..173c02b 100644 --- a/src/Faithlife.WebRequests/Json/AutoWebServiceResponse.cs +++ b/src/Faithlife.WebRequests/Json/AutoWebServiceResponse.cs @@ -2,6 +2,7 @@ using System.Net; using System.Net.Http.Headers; using System.Threading.Tasks; +using Faithlife.Json; namespace Faithlife.WebRequests.Json { @@ -16,6 +17,20 @@ public abstract class AutoWebServiceResponse /// A new exception for the response. 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, diff --git a/src/Faithlife.WebRequests/Json/AutoWebServiceResponseUtility.cs b/src/Faithlife.WebRequests/Json/AutoWebServiceResponseUtility.cs index 64c8333..096b10c 100644 --- a/src/Faithlife.WebRequests/Json/AutoWebServiceResponseUtility.cs +++ b/src/Faithlife.WebRequests/Json/AutoWebServiceResponseUtility.cs @@ -1,5 +1,7 @@ using System; using System.Collections.Generic; +using System.Linq; +using System.Reflection; namespace Faithlife.WebRequests.Json { @@ -39,5 +41,22 @@ public static void VerifyResultIsExpected(this TResponse r { GetExpectedResult(response, getProperty); } + + /// + /// Returns PropertyInfo of the desired property. + /// + /// The response. + /// The name of the property. + /// The PropertyInfo of the property. + internal static PropertyInfo? GetStatusCodeProperty(object response, string? propertyName) + { + if (propertyName == null) + return null; + + return response.GetType().GetRuntimeProperties().FirstOrDefault(x => + x.GetMethod != null && + !x.GetMethod.IsStatic && + string.Equals(x.Name, propertyName, StringComparison.OrdinalIgnoreCase)); + } } } From 095734e6dfcfd3d92d2d609a8af39376a0effc79 Mon Sep 17 00:00:00 2001 From: James Jensen Date: Mon, 16 Aug 2021 12:44:00 -0700 Subject: [PATCH 2/3] Increment version and add release note. --- Directory.Build.props | 2 +- ReleaseNotes.md | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/Directory.Build.props b/Directory.Build.props index e15dc06..382acc3 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -1,7 +1,7 @@ - 0.10.0 + 0.11.0 9.0 enable true diff --git a/ReleaseNotes.md b/ReleaseNotes.md index a514717..b9bc789 100644 --- a/ReleaseNotes.md +++ b/ReleaseNotes.md @@ -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). From a9ba5878b1ccfb1b16abfa9ff047afc965f19aad Mon Sep 17 00:00:00 2001 From: James Jensen Date: Mon, 16 Aug 2021 13:51:30 -0700 Subject: [PATCH 3/3] Make new utility method more closely resemble pre-existing private method. --- .../Json/AutoWebServiceResponseUtility.cs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/Faithlife.WebRequests/Json/AutoWebServiceResponseUtility.cs b/src/Faithlife.WebRequests/Json/AutoWebServiceResponseUtility.cs index 096b10c..1e48122 100644 --- a/src/Faithlife.WebRequests/Json/AutoWebServiceResponseUtility.cs +++ b/src/Faithlife.WebRequests/Json/AutoWebServiceResponseUtility.cs @@ -53,10 +53,15 @@ public static void VerifyResultIsExpected(this TResponse r if (propertyName == null) return null; - return response.GetType().GetRuntimeProperties().FirstOrDefault(x => - x.GetMethod != 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; } } }