Skip to content

Instantly share code, notes, and snippets.

@steve-codemunkies
Created August 12, 2021 21:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save steve-codemunkies/1605b7525d7a99f462e8601c063c81bd to your computer and use it in GitHub Desktop.
Save steve-codemunkies/1605b7525d7a99f462e8601c063c81bd to your computer and use it in GitHub Desktop.
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