Created
August 12, 2021 21:34
-
-
Save steve-codemunkies/1605b7525d7a99f462e8601c063c81bd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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