diff --git a/SpotifyAPI.Web/SpotifyWebAPI.cs b/SpotifyAPI.Web/SpotifyWebAPI.cs index 657f74f2..2e628dc2 100644 --- a/SpotifyAPI.Web/SpotifyWebAPI.cs +++ b/SpotifyAPI.Web/SpotifyWebAPI.cs @@ -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 + } }; } diff --git a/SpotifyAPI.Web/SpotifyWebClient.cs b/SpotifyAPI.Web/SpotifyWebClient.cs index 3789aa50..b8c5c094 100644 --- a/SpotifyAPI.Web/SpotifyWebClient.cs +++ b/SpotifyAPI.Web/SpotifyWebClient.cs @@ -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 Download(string url, Dictionary headers = null) { @@ -32,47 +36,39 @@ namespace SpotifyAPI.Web public Tuple DownloadRaw(string url, Dictionary headers = null) { - HttpClientHandler clientHandler = CreateClientHandler(ProxyConfig); - using (HttpClient client = new HttpClient(clientHandler)) + if (headers != null) { - if (headers != null) + foreach (KeyValuePair headerPair in headers) { - foreach (KeyValuePair 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(new ResponseInfo { - return new Tuple(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> DownloadRawAsync(string url, Dictionary headers = null) { - HttpClientHandler clientHandler = CreateClientHandler(ProxyConfig); - using (HttpClient client = new HttpClient(clientHandler)) + if (headers != null) { - if (headers != null) + foreach (KeyValuePair headerPair in headers) { - foreach (KeyValuePair 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(new ResponseInfo { - return new Tuple(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 UploadRaw(string url, string body, string method, Dictionary headers = null) { - HttpClientHandler clientHandler = CreateClientHandler(ProxyConfig); - using (HttpClient client = new HttpClient(clientHandler)) + if (headers != null) { - if (headers != null) + foreach (KeyValuePair headerPair in headers) { - foreach (KeyValuePair 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(new ResponseInfo { - Content = new StringContent(body, _encoding) - }; - using (HttpResponseMessage response = Task.Run(() => client.SendAsync(message)).Result) - { - return new Tuple(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> UploadRawAsync(string url, string body, string method, Dictionary headers = null) { - HttpClientHandler clientHandler = CreateClientHandler(ProxyConfig); - using (HttpClient client = new HttpClient(clientHandler)) + if (headers != null) { - if (headers != null) + foreach (KeyValuePair headerPair in headers) { - foreach (KeyValuePair 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(new ResponseInfo { - Content = new StringContent(body, _encoding) - }; - using (HttpResponseMessage response = await client.SendAsync(message)) - { - return new Tuple(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); }