From c0bee03f9e1de6e06893ec5f9d56f913782fe552 Mon Sep 17 00:00:00 2001 From: Jacob Affinito Date: Tue, 18 Jun 2024 13:55:34 -0700 Subject: [PATCH 1/2] chore: For netstandard2.0, use HttpClient in VendorHttpApiRequestor. --- .../Utilization/VendorHttpApiRequestor.cs | 67 ++++++++++++++++++- 1 file changed, 66 insertions(+), 1 deletion(-) diff --git a/src/Agent/NewRelic/Agent/Core/Utilization/VendorHttpApiRequestor.cs b/src/Agent/NewRelic/Agent/Core/Utilization/VendorHttpApiRequestor.cs index 3a67f59eeb..98b25699db 100644 --- a/src/Agent/NewRelic/Agent/Core/Utilization/VendorHttpApiRequestor.cs +++ b/src/Agent/NewRelic/Agent/Core/Utilization/VendorHttpApiRequestor.cs @@ -3,9 +3,14 @@ using System; using System.Collections.Generic; +using NewRelic.Agent.Extensions.Logging; + +#if NETFRAMEWORK using System.IO; using System.Net; -using NewRelic.Agent.Extensions.Logging; +#else +using System.Net.Http; +#endif namespace NewRelic.Agent.Core.Utilization { @@ -15,6 +20,16 @@ public class VendorHttpApiRequestor public virtual string CallVendorApi(Uri uri, string method, string vendorName, IEnumerable headers = null) { +#if NETFRAMEWORK + return CallWithWebRequest(uri, method, vendorName, headers); +#else + return CallWithHttpClient(uri, method, vendorName, headers); +#endif + } + +#if NETFRAMEWORK + private string CallWithWebRequest(Uri uri, string method, string vendorName, IEnumerable headers = null) + { try { var request = WebRequest.Create(uri); @@ -66,5 +81,55 @@ public virtual string CallVendorApi(Uri uri, string method, string vendorName, I return null; } } +#else + // create a static HttpClient for use across all the vendor API calls + private static readonly HttpClient httpClient = new HttpClient + { + Timeout = TimeSpan.FromMilliseconds(WebReqeustTimeout) + }; + + private string CallWithHttpClient(Uri uri, string method, string vendorName, IEnumerable headers = null) + { + try + { + var request = new HttpRequestMessage + { + RequestUri = uri, + Method = new HttpMethod(method) + }; + + if (headers != null) + { + foreach (var header in headers) + { + var separatorIndex = header.IndexOf(": "); + if (separatorIndex > -1) + { + var headerName = header.Substring(0, separatorIndex).Trim(); + var headerValue = header.Substring(separatorIndex + 2).Trim(); // +2 to skip past ": " + request.Headers.TryAddWithoutValidation(headerName, headerValue); + } + } + } + + var response = httpClient.SendAsync(request).GetAwaiter().GetResult(); + if (!response.IsSuccessStatusCode) + { + var statusCode = response.StatusCode; + var statusDescription = response.ReasonPhrase ?? string.Empty; + // intentionally not passing the exception parameter here + Log.Debug("CallVendorApi ({0}) failed with WebException with status: {1}; message: {2}", vendorName, statusCode, statusDescription); + } + + return response.Content.ReadAsStringAsync().GetAwaiter().GetResult(); + } + catch (Exception ex) + { + // intentionally not passing the exception parameter here + Log.Debug($"CallVendorApi ({{0}}) failed: {ex.Message}", vendorName); + return null; + } + } +#endif } } From bb56de835f010a309ff0d6d07f242fc45e3a10a7 Mon Sep 17 00:00:00 2001 From: Jacob Affinito Date: Tue, 18 Jun 2024 17:42:17 -0700 Subject: [PATCH 2/2] Lazy init the httpClient --- .../Core/Utilization/VendorHttpApiRequestor.cs | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/Agent/NewRelic/Agent/Core/Utilization/VendorHttpApiRequestor.cs b/src/Agent/NewRelic/Agent/Core/Utilization/VendorHttpApiRequestor.cs index 98b25699db..d14f492a7c 100644 --- a/src/Agent/NewRelic/Agent/Core/Utilization/VendorHttpApiRequestor.cs +++ b/src/Agent/NewRelic/Agent/Core/Utilization/VendorHttpApiRequestor.cs @@ -82,11 +82,12 @@ private string CallWithWebRequest(Uri uri, string method, string vendorName, IEn } } #else - // create a static HttpClient for use across all the vendor API calls - private static readonly HttpClient httpClient = new HttpClient - { - Timeout = TimeSpan.FromMilliseconds(WebReqeustTimeout) - }; + private static readonly Lazy httpClient = new Lazy(() => + new HttpClient + { + Timeout = TimeSpan.FromMilliseconds(WebReqeustTimeout) + }); + private string CallWithHttpClient(Uri uri, string method, string vendorName, IEnumerable headers = null) { @@ -112,7 +113,7 @@ private string CallWithHttpClient(Uri uri, string method, string vendorName, IEn } } - var response = httpClient.SendAsync(request).GetAwaiter().GetResult(); + var response = httpClient.Value.SendAsync(request).GetAwaiter().GetResult(); if (!response.IsSuccessStatusCode) { var statusCode = response.StatusCode;