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();
UseAuth = true;
WebClient = new SpotifyWebClient
WebClient = new SpotifyWebClient(proxyConfig)
{
JsonSettings =
new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore,
TypeNameHandling = TypeNameHandling.All
},
ProxyConfig = proxyConfig
}
};
}

View File

@ -13,10 +13,14 @@ namespace SpotifyAPI.Web
internal class SpotifyWebClient : IClient
{
public JsonSerializerSettings JsonSettings { get; set; }
public ProxyConfig ProxyConfig { get; set; }
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)
{
@ -32,47 +36,39 @@ namespace SpotifyAPI.Web
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
{
StatusCode = response.StatusCode,
Headers = ConvertHeaders(response.Headers)
}, Task.Run(() => response.Content.ReadAsByteArrayAsync()).Result);
}
StatusCode = response.StatusCode,
Headers = ConvertHeaders(response.Headers)
}, Task.Run(() => response.Content.ReadAsByteArrayAsync()).Result);
}
}
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
{
StatusCode = response.StatusCode,
Headers = ConvertHeaders(response.Headers)
}, await response.Content.ReadAsByteArrayAsync());
}
StatusCode = response.StatusCode,
Headers = ConvertHeaders(response.Headers)
}, await response.Content.ReadAsByteArrayAsync());
}
}
@ -102,57 +98,49 @@ namespace SpotifyAPI.Web
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);
}
}
HttpRequestMessage message = new HttpRequestMessage(new HttpMethod(method), url)
HttpRequestMessage message = new HttpRequestMessage(new HttpMethod(method), url)
{
Content = new StringContent(body, _encoding)
};
using (HttpResponseMessage response = Task.Run(() => _client.SendAsync(message)).Result)
{
return new Tuple<ResponseInfo, byte[]>(new ResponseInfo
{
Content = new StringContent(body, _encoding)
};
using (HttpResponseMessage response = Task.Run(() => client.SendAsync(message)).Result)
{
return new Tuple<ResponseInfo, byte[]>(new ResponseInfo
{
StatusCode = response.StatusCode,
Headers = ConvertHeaders(response.Headers)
}, Task.Run(() => response.Content.ReadAsByteArrayAsync()).Result);
}
StatusCode = response.StatusCode,
Headers = ConvertHeaders(response.Headers)
}, Task.Run(() => response.Content.ReadAsByteArrayAsync()).Result);
}
}
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);
}
}
HttpRequestMessage message = new HttpRequestMessage(new HttpMethod(method), url)
HttpRequestMessage message = new HttpRequestMessage(new HttpMethod(method), url)
{
Content = new StringContent(body, _encoding)
};
using (HttpResponseMessage response = await _client.SendAsync(message))
{
return new Tuple<ResponseInfo, byte[]>(new ResponseInfo
{
Content = new StringContent(body, _encoding)
};
using (HttpResponseMessage response = await client.SendAsync(message))
{
return new Tuple<ResponseInfo, byte[]>(new ResponseInfo
{
StatusCode = response.StatusCode,
Headers = ConvertHeaders(response.Headers)
}, await response.Content.ReadAsByteArrayAsync());
}
StatusCode = response.StatusCode,
Headers = ConvertHeaders(response.Headers)
}, await response.Content.ReadAsByteArrayAsync());
}
}
@ -170,6 +158,7 @@ namespace SpotifyAPI.Web
public void Dispose()
{
_client.Dispose();
GC.SuppressFinalize(this);
}