Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
using System;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
namespace HttpClientTest.Class
{
public interface ICallTheInternet
{
public Task<bool> GetWithNamedClient(string clientName, string url, HttpStatusCode expectedStatusCode);
public Task<bool> GetWithClient(HttpClient client, string url, HttpStatusCode expectedStatusCode);
}
public class InternetCaller : ICallTheInternet
{
private readonly IHttpClientFactory _httpClientFactory;
public InternetCaller(IHttpClientFactory httpClientFactory)
{
_httpClientFactory = httpClientFactory ?? throw new ArgumentNullException(nameof(httpClientFactory));
}
public async Task<bool> GetWithClient(HttpClient client, string url, HttpStatusCode expectedStatusCode)
{
var result = await client.GetAsync(url);
return result.StatusCode == expectedStatusCode;
}
public Task<bool> GetWithNamedClient(string clientName, string url, HttpStatusCode expectedStatusCode)
{
var httpClient = _httpClientFactory.CreateClient(clientName);
return GetWithClient(httpClient, url, expectedStatusCode);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment