mirror of
https://github.com/Sarsoo/Spotify.NET.git
synced 2024-12-24 15:06:26 +00:00
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:
parent
81fe1763f4
commit
fa0f3e2d90
@ -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
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
@ -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)
|
||||
{
|
||||
@ -31,18 +35,15 @@ 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)
|
||||
{
|
||||
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
|
||||
{
|
||||
@ -51,21 +52,17 @@ namespace SpotifyAPI.Web
|
||||
}, 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)
|
||||
{
|
||||
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
|
||||
{
|
||||
@ -74,7 +71,6 @@ namespace SpotifyAPI.Web
|
||||
}, await response.Content.ReadAsByteArrayAsync());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
HttpClientHandler clientHandler = CreateClientHandler(ProxyConfig);
|
||||
using (HttpClient client = new HttpClient(clientHandler))
|
||||
{
|
||||
if (headers != null)
|
||||
{
|
||||
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)
|
||||
};
|
||||
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
|
||||
{
|
||||
@ -126,18 +119,14 @@ namespace SpotifyAPI.Web
|
||||
}, 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)
|
||||
{
|
||||
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)
|
||||
};
|
||||
using (HttpResponseMessage response = await client.SendAsync(message))
|
||||
using (HttpResponseMessage response = await _client.SendAsync(message))
|
||||
{
|
||||
return new Tuple<ResponseInfo, byte[]>(new ResponseInfo
|
||||
{
|
||||
@ -154,7 +143,6 @@ namespace SpotifyAPI.Web
|
||||
}, await response.Content.ReadAsByteArrayAsync());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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()
|
||||
{
|
||||
_client.Dispose();
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user