Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: For netstandard2.0, use HttpClient in VendorHttpApiRequestor. #2566

Merged
merged 2 commits into from
Jun 20, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -15,6 +20,16 @@ public class VendorHttpApiRequestor

public virtual string CallVendorApi(Uri uri, string method, string vendorName, IEnumerable<string> 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<string> headers = null)
{
try
{
var request = WebRequest.Create(uri);
Expand Down Expand Up @@ -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
jaffinito marked this conversation as resolved.
Show resolved Hide resolved
{
Timeout = TimeSpan.FromMilliseconds(WebReqeustTimeout)
};

private string CallWithHttpClient(Uri uri, string method, string vendorName, IEnumerable<string> 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
}
}
Loading