I am calling an external API and want my API to be a module for testing. And for this I am trying to wrap an HttpClient. I need only one method. Here is my interface.
public interface IHttpClient
{
Task<string> GetStringAsync(string url);
}
And this is how I implemented it.
public class HttpClientWrapper: IHttpClient {private readonly HttpClient _httpClient;
public HttpClientWrapper()
{
_httpClient = new HttpClient(new HttpClientHandler(), false);
}
public async Task<string> GetStringAsync(string url)
{
return await _httpClient.GetStringAsync(url);
}
}
I have doubts? is this the right way to do this? Will the bool parameter result in a resource leak here? I read a couple of conflicting ideas about whether the HttpClient should be configured on every call or not. I took the non-disposable side, but really not quite sure. If there is a way to use HttpClient without a wrapper, but make the API validate, that would be great too. But so far I have not succeeded.
Thanks CleanKoder