Modified SpotifyWebClient to use a single HttpClient per class lifeti… (#287)

* Modified SpotifyWebClient to use a single HttpClient per class lifetime. This will allow the HttpClient to properly pool connections.
Reference: https://docs.microsoft.com/en-us/aspnet/web-api/overview/advanced/calling-a-web-api-from-a-net-client

* Added suggested changes. Moved ProxyConfig to the constructor of SpotifyWebClient.
This commit is contained in:
kg73 2018-09-17 07:44:22 -05:00 committed by Jonas Dellinger
parent 81fe1763f4
commit fa0f3e2d90
2 changed files with 58 additions and 70 deletions

View File

@ -23,15 +23,14 @@ namespace SpotifyAPI.Web
{ {
_builder = new SpotifyWebBuilder(); _builder = new SpotifyWebBuilder();
UseAuth = true; UseAuth = true;
WebClient = new SpotifyWebClient WebClient = new SpotifyWebClient(proxyConfig)
{ {
JsonSettings = JsonSettings =
new JsonSerializerSettings new JsonSerializerSettings
{ {
NullValueHandling = NullValueHandling.Ignore, NullValueHandling = NullValueHandling.Ignore,
TypeNameHandling = TypeNameHandling.All TypeNameHandling = TypeNameHandling.All
}, }
ProxyConfig = proxyConfig
}; };
} }

View File

@ -13,10 +13,14 @@ namespace SpotifyAPI.Web
internal class SpotifyWebClient : IClient internal class SpotifyWebClient : IClient
{ {
public JsonSerializerSettings JsonSettings { get; set; } public JsonSerializerSettings JsonSettings { get; set; }
public ProxyConfig ProxyConfig { get; set; }
private readonly Encoding _encoding = Encoding.UTF8; private readonly Encoding _encoding = Encoding.UTF8;
private readonly HttpClient _client;
public SpotifyWebClient(ProxyConfig proxyConfig = null)
{
HttpClientHandler clientHandler = CreateClientHandler(proxyConfig);
_client = new HttpClient(clientHandler);
}
public Tuple<ResponseInfo, string> Download(string url, Dictionary<string, string> headers = null) public Tuple<ResponseInfo, string> Download(string url, Dictionary<string, string> headers = null)
{ {
@ -31,18 +35,15 @@ namespace SpotifyAPI.Web
} }
public Tuple<ResponseInfo, byte[]> DownloadRaw(string url, Dictionary<string, string> headers = null) public Tuple<ResponseInfo, byte[]> DownloadRaw(string url, Dictionary<string, string> headers = null)
{
HttpClientHandler clientHandler = CreateClientHandler(ProxyConfig);
using (HttpClient client = new HttpClient(clientHandler))
{ {
if (headers != null) if (headers != null)
{ {
foreach (KeyValuePair<string, string> headerPair in headers) foreach (KeyValuePair<string, string> headerPair in headers)
{ {
client.DefaultRequestHeaders.TryAddWithoutValidation(headerPair.Key, headerPair.Value); _client.DefaultRequestHeaders.TryAddWithoutValidation(headerPair.Key, headerPair.Value);
} }
} }
using (HttpResponseMessage response = Task.Run(() => client.GetAsync(url)).Result) using (HttpResponseMessage response = Task.Run(() => _client.GetAsync(url)).Result)
{ {
return new Tuple<ResponseInfo, byte[]>(new ResponseInfo return new Tuple<ResponseInfo, byte[]>(new ResponseInfo
{ {
@ -51,21 +52,17 @@ namespace SpotifyAPI.Web
}, Task.Run(() => response.Content.ReadAsByteArrayAsync()).Result); }, Task.Run(() => response.Content.ReadAsByteArrayAsync()).Result);
} }
} }
}
public async Task<Tuple<ResponseInfo, byte[]>> DownloadRawAsync(string url, Dictionary<string, string> headers = null) public async Task<Tuple<ResponseInfo, byte[]>> DownloadRawAsync(string url, Dictionary<string, string> headers = null)
{
HttpClientHandler clientHandler = CreateClientHandler(ProxyConfig);
using (HttpClient client = new HttpClient(clientHandler))
{ {
if (headers != null) if (headers != null)
{ {
foreach (KeyValuePair<string, string> headerPair in headers) foreach (KeyValuePair<string, string> headerPair in headers)
{ {
client.DefaultRequestHeaders.TryAddWithoutValidation(headerPair.Key, headerPair.Value); _client.DefaultRequestHeaders.TryAddWithoutValidation(headerPair.Key, headerPair.Value);
} }
} }
using (HttpResponseMessage response = await client.GetAsync(url).ConfigureAwait(false)) using (HttpResponseMessage response = await _client.GetAsync(url).ConfigureAwait(false))
{ {
return new Tuple<ResponseInfo, byte[]>(new ResponseInfo return new Tuple<ResponseInfo, byte[]>(new ResponseInfo
{ {
@ -74,7 +71,6 @@ namespace SpotifyAPI.Web
}, await response.Content.ReadAsByteArrayAsync()); }, await response.Content.ReadAsByteArrayAsync());
} }
} }
}
public Tuple<ResponseInfo, T> DownloadJson<T>(string url, Dictionary<string, string> headers = null) public Tuple<ResponseInfo, T> DownloadJson<T>(string url, Dictionary<string, string> headers = null)
{ {
@ -101,15 +97,12 @@ namespace SpotifyAPI.Web
} }
public Tuple<ResponseInfo, byte[]> UploadRaw(string url, string body, string method, Dictionary<string, string> headers = null) public Tuple<ResponseInfo, byte[]> UploadRaw(string url, string body, string method, Dictionary<string, string> headers = null)
{
HttpClientHandler clientHandler = CreateClientHandler(ProxyConfig);
using (HttpClient client = new HttpClient(clientHandler))
{ {
if (headers != null) if (headers != null)
{ {
foreach (KeyValuePair<string, string> headerPair in headers) foreach (KeyValuePair<string, string> headerPair in headers)
{ {
client.DefaultRequestHeaders.TryAddWithoutValidation(headerPair.Key, headerPair.Value); _client.DefaultRequestHeaders.TryAddWithoutValidation(headerPair.Key, headerPair.Value);
} }
} }
@ -117,7 +110,7 @@ namespace SpotifyAPI.Web
{ {
Content = new StringContent(body, _encoding) Content = new StringContent(body, _encoding)
}; };
using (HttpResponseMessage response = Task.Run(() => client.SendAsync(message)).Result) using (HttpResponseMessage response = Task.Run(() => _client.SendAsync(message)).Result)
{ {
return new Tuple<ResponseInfo, byte[]>(new ResponseInfo return new Tuple<ResponseInfo, byte[]>(new ResponseInfo
{ {
@ -126,18 +119,14 @@ namespace SpotifyAPI.Web
}, Task.Run(() => response.Content.ReadAsByteArrayAsync()).Result); }, Task.Run(() => response.Content.ReadAsByteArrayAsync()).Result);
} }
} }
}
public async Task<Tuple<ResponseInfo, byte[]>> UploadRawAsync(string url, string body, string method, Dictionary<string, string> headers = null) public async Task<Tuple<ResponseInfo, byte[]>> UploadRawAsync(string url, string body, string method, Dictionary<string, string> headers = null)
{
HttpClientHandler clientHandler = CreateClientHandler(ProxyConfig);
using (HttpClient client = new HttpClient(clientHandler))
{ {
if (headers != null) if (headers != null)
{ {
foreach (KeyValuePair<string, string> headerPair in headers) foreach (KeyValuePair<string, string> headerPair in headers)
{ {
client.DefaultRequestHeaders.TryAddWithoutValidation(headerPair.Key, headerPair.Value); _client.DefaultRequestHeaders.TryAddWithoutValidation(headerPair.Key, headerPair.Value);
} }
} }
@ -145,7 +134,7 @@ namespace SpotifyAPI.Web
{ {
Content = new StringContent(body, _encoding) Content = new StringContent(body, _encoding)
}; };
using (HttpResponseMessage response = await client.SendAsync(message)) using (HttpResponseMessage response = await _client.SendAsync(message))
{ {
return new Tuple<ResponseInfo, byte[]>(new ResponseInfo return new Tuple<ResponseInfo, byte[]>(new ResponseInfo
{ {
@ -154,7 +143,6 @@ namespace SpotifyAPI.Web
}, await response.Content.ReadAsByteArrayAsync()); }, await response.Content.ReadAsByteArrayAsync());
} }
} }
}
public Tuple<ResponseInfo, T> UploadJson<T>(string url, string body, string method, Dictionary<string, string> headers = null) public Tuple<ResponseInfo, T> UploadJson<T>(string url, string body, string method, Dictionary<string, string> headers = null)
{ {
@ -170,6 +158,7 @@ namespace SpotifyAPI.Web
public void Dispose() public void Dispose()
{ {
_client.Dispose();
GC.SuppressFinalize(this); GC.SuppressFinalize(this);
} }