Skip to content

Commit fa0f3e2

Browse files
kg73JohnnyCrazy
authored andcommitted
Modified SpotifyWebClient to use a single HttpClient per class lifeti… (JohnnyCrazy#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.
1 parent 81fe176 commit fa0f3e2

2 files changed

Lines changed: 58 additions & 70 deletions

File tree

‎SpotifyAPI.Web/SpotifyWebAPI.cs‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,14 @@ public SpotifyWebAPI(ProxyConfig proxyConfig)
2323
{
2424
_builder = new SpotifyWebBuilder();
2525
UseAuth = true;
26-
WebClient = new SpotifyWebClient
26+
WebClient = new SpotifyWebClient(proxyConfig)
2727
{
2828
JsonSettings =
2929
new JsonSerializerSettings
3030
{
3131
NullValueHandling = NullValueHandling.Ignore,
3232
TypeNameHandling = TypeNameHandling.All
33-
},
34-
ProxyConfig = proxyConfig
33+
}
3534
};
3635
}
3736

‎SpotifyAPI.Web/SpotifyWebClient.cs‎

Lines changed: 56 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,14 @@ namespace SpotifyAPI.Web
1313
internal class SpotifyWebClient : IClient
1414
{
1515
public JsonSerializerSettings JsonSettings { get; set; }
16-
17-
public ProxyConfig ProxyConfig { get; set; }
18-
1916
private readonly Encoding _encoding = Encoding.UTF8;
17+
private readonly HttpClient _client;
18+
19+
public SpotifyWebClient(ProxyConfig proxyConfig = null)
20+
{
21+
HttpClientHandler clientHandler = CreateClientHandler(proxyConfig);
22+
_client = new HttpClient(clientHandler);
23+
}
2024

2125
public Tuple<ResponseInfo, string> Download(string url, Dictionary<string, string> headers = null)
2226
{
@@ -32,47 +36,39 @@ public async Task<Tuple<ResponseInfo, string>> DownloadAsync(string url, Diction
3236

3337
public Tuple<ResponseInfo, byte[]> DownloadRaw(string url, Dictionary<string, string> headers = null)
3438
{
35-
HttpClientHandler clientHandler = CreateClientHandler(ProxyConfig);
36-
using (HttpClient client = new HttpClient(clientHandler))
39+
if (headers != null)
3740
{
38-
if (headers != null)
41+
foreach (KeyValuePair<string, string> headerPair in headers)
3942
{
40-
foreach (KeyValuePair<string, string> headerPair in headers)
41-
{
42-
client.DefaultRequestHeaders.TryAddWithoutValidation(headerPair.Key, headerPair.Value);
43-
}
43+
_client.DefaultRequestHeaders.TryAddWithoutValidation(headerPair.Key, headerPair.Value);
4444
}
45-
using (HttpResponseMessage response = Task.Run(() => client.GetAsync(url)).Result)
45+
}
46+
using (HttpResponseMessage response = Task.Run(() => _client.GetAsync(url)).Result)
47+
{
48+
return new Tuple<ResponseInfo, byte[]>(new ResponseInfo
4649
{
47-
return new Tuple<ResponseInfo, byte[]>(new ResponseInfo
48-
{
49-
StatusCode = response.StatusCode,
50-
Headers = ConvertHeaders(response.Headers)
51-
}, Task.Run(() => response.Content.ReadAsByteArrayAsync()).Result);
52-
}
50+
StatusCode = response.StatusCode,
51+
Headers = ConvertHeaders(response.Headers)
52+
}, Task.Run(() => response.Content.ReadAsByteArrayAsync()).Result);
5353
}
5454
}
5555

5656
public async Task<Tuple<ResponseInfo, byte[]>> DownloadRawAsync(string url, Dictionary<string, string> headers = null)
5757
{
58-
HttpClientHandler clientHandler = CreateClientHandler(ProxyConfig);
59-
using (HttpClient client = new HttpClient(clientHandler))
58+
if (headers != null)
6059
{
61-
if (headers != null)
60+
foreach (KeyValuePair<string, string> headerPair in headers)
6261
{
63-
foreach (KeyValuePair<string, string> headerPair in headers)
64-
{
65-
client.DefaultRequestHeaders.TryAddWithoutValidation(headerPair.Key, headerPair.Value);
66-
}
62+
_client.DefaultRequestHeaders.TryAddWithoutValidation(headerPair.Key, headerPair.Value);
6763
}
68-
using (HttpResponseMessage response = await client.GetAsync(url).ConfigureAwait(false))
64+
}
65+
using (HttpResponseMessage response = await _client.GetAsync(url).ConfigureAwait(false))
66+
{
67+
return new Tuple<ResponseInfo, byte[]>(new ResponseInfo
6968
{
70-
return new Tuple<ResponseInfo, byte[]>(new ResponseInfo
71-
{
72-
StatusCode = response.StatusCode,
73-
Headers = ConvertHeaders(response.Headers)
74-
}, await response.Content.ReadAsByteArrayAsync());
75-
}
69+
StatusCode = response.StatusCode,
70+
Headers = ConvertHeaders(response.Headers)
71+
}, await response.Content.ReadAsByteArrayAsync());
7672
}
7773
}
7874

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

10399
public Tuple<ResponseInfo, byte[]> UploadRaw(string url, string body, string method, Dictionary<string, string> headers = null)
104100
{
105-
HttpClientHandler clientHandler = CreateClientHandler(ProxyConfig);
106-
using (HttpClient client = new HttpClient(clientHandler))
101+
if (headers != null)
107102
{
108-
if (headers != null)
103+
foreach (KeyValuePair<string, string> headerPair in headers)
109104
{
110-
foreach (KeyValuePair<string, string> headerPair in headers)
111-
{
112-
client.DefaultRequestHeaders.TryAddWithoutValidation(headerPair.Key, headerPair.Value);
113-
}
105+
_client.DefaultRequestHeaders.TryAddWithoutValidation(headerPair.Key, headerPair.Value);
114106
}
107+
}
115108

116-
HttpRequestMessage message = new HttpRequestMessage(new HttpMethod(method), url)
117-
{
118-
Content = new StringContent(body, _encoding)
119-
};
120-
using (HttpResponseMessage response = Task.Run(() => client.SendAsync(message)).Result)
109+
HttpRequestMessage message = new HttpRequestMessage(new HttpMethod(method), url)
110+
{
111+
Content = new StringContent(body, _encoding)
112+
};
113+
using (HttpResponseMessage response = Task.Run(() => _client.SendAsync(message)).Result)
114+
{
115+
return new Tuple<ResponseInfo, byte[]>(new ResponseInfo
121116
{
122-
return new Tuple<ResponseInfo, byte[]>(new ResponseInfo
123-
{
124-
StatusCode = response.StatusCode,
125-
Headers = ConvertHeaders(response.Headers)
126-
}, Task.Run(() => response.Content.ReadAsByteArrayAsync()).Result);
127-
}
117+
StatusCode = response.StatusCode,
118+
Headers = ConvertHeaders(response.Headers)
119+
}, Task.Run(() => response.Content.ReadAsByteArrayAsync()).Result);
128120
}
129121
}
130122

131123
public async Task<Tuple<ResponseInfo, byte[]>> UploadRawAsync(string url, string body, string method, Dictionary<string, string> headers = null)
132124
{
133-
HttpClientHandler clientHandler = CreateClientHandler(ProxyConfig);
134-
using (HttpClient client = new HttpClient(clientHandler))
125+
if (headers != null)
135126
{
136-
if (headers != null)
127+
foreach (KeyValuePair<string, string> headerPair in headers)
137128
{
138-
foreach (KeyValuePair<string, string> headerPair in headers)
139-
{
140-
client.DefaultRequestHeaders.TryAddWithoutValidation(headerPair.Key, headerPair.Value);
141-
}
129+
_client.DefaultRequestHeaders.TryAddWithoutValidation(headerPair.Key, headerPair.Value);
142130
}
131+
}
143132

144-
HttpRequestMessage message = new HttpRequestMessage(new HttpMethod(method), url)
145-
{
146-
Content = new StringContent(body, _encoding)
147-
};
148-
using (HttpResponseMessage response = await client.SendAsync(message))
133+
HttpRequestMessage message = new HttpRequestMessage(new HttpMethod(method), url)
134+
{
135+
Content = new StringContent(body, _encoding)
136+
};
137+
using (HttpResponseMessage response = await _client.SendAsync(message))
138+
{
139+
return new Tuple<ResponseInfo, byte[]>(new ResponseInfo
149140
{
150-
return new Tuple<ResponseInfo, byte[]>(new ResponseInfo
151-
{
152-
StatusCode = response.StatusCode,
153-
Headers = ConvertHeaders(response.Headers)
154-
}, await response.Content.ReadAsByteArrayAsync());
155-
}
141+
StatusCode = response.StatusCode,
142+
Headers = ConvertHeaders(response.Headers)
143+
}, await response.Content.ReadAsByteArrayAsync());
156144
}
157145
}
158146

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

171159
public void Dispose()
172160
{
161+
_client.Dispose();
173162
GC.SuppressFinalize(this);
174163
}
175164

0 commit comments

Comments
 (0)