Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions SpotifyAPI.Web/SpotifyWebAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,14 @@ public SpotifyWebAPI(ProxyConfig proxyConfig)
{
_builder = new SpotifyWebBuilder();
UseAuth = true;
WebClient = new SpotifyWebClient
WebClient = new SpotifyWebClient(proxyConfig)
{
JsonSettings =
new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore,
TypeNameHandling = TypeNameHandling.All
},
ProxyConfig = proxyConfig
}
};
}

Expand Down
123 changes: 56 additions & 67 deletions SpotifyAPI.Web/SpotifyWebClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -32,47 +36,39 @@ public async Task<Tuple<ResponseInfo, string>> DownloadAsync(string url, Diction

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());
}
}

Expand Down Expand Up @@ -102,57 +98,49 @@ public async Task<Tuple<ResponseInfo, string>> UploadAsync(string url, string bo

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)
{
Content = new StringContent(body, _encoding)
};
using (HttpResponseMessage response = Task.Run(() => client.SendAsync(message)).Result)
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
{
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)
{
Content = new StringContent(body, _encoding)
};
using (HttpResponseMessage response = await client.SendAsync(message))
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
{
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());
}
}

Expand All @@ -170,6 +158,7 @@ public async Task<Tuple<ResponseInfo, T>> UploadJsonAsync<T>(string url, string

public void Dispose()
{
_client.Dispose();
GC.SuppressFinalize(this);
}

Expand Down