Skip to content

Commit 1253d02

Browse files
author
Jonas Dellinger
committed
Switched from WebClient to HTTPClient
1 parent 2543ed2 commit 1253d02

6 files changed

Lines changed: 138 additions & 174 deletions

File tree

‎SpotifyAPI.Example/WebControl.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ private async void InitialSetup()
3838
}
3939

4040
authButton.Enabled = false;
41-
_profile = _spotify.GetPrivateProfile();
41+
_profile = await _spotify.GetPrivateProfileAsync();
4242

4343
_savedTracks = GetSavedTracks();
4444
savedTracksCountLabel.Text = _savedTracks.Count.ToString();

‎SpotifyAPI.Tests/TestClass.cs‎

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using SpotifyAPI.Web;
55
using SpotifyAPI.Web.Models;
66
using System;
7+
using System.Collections.Generic;
78
using System.IO;
89
using System.Linq;
910

@@ -50,23 +51,29 @@ public void ShouldGetPrivateProfile_WithoutAuth()
5051
public void ShouldGetPrivateProfile_WithAuth()
5152
{
5253
PrivateProfile profile = GetFixture<PrivateProfile>("private-user.json");
53-
_mock.Setup(client => client.DownloadJson<PrivateProfile>(It.IsAny<string>()))
54+
_mock.Setup(client => client.DownloadJson<PrivateProfile>(It.IsAny<string>(), It.IsAny<Dictionary<string, string>>()))
5455
.Returns(new Tuple<ResponseInfo, PrivateProfile>(ResponseInfo.Empty, profile));
5556

5657
_spotify.UseAuth = true;
5758
Assert.AreEqual(profile, _spotify.GetPrivateProfile());
58-
_mock.Verify(client => client.DownloadJson<PrivateProfile>(It.Is<string>(str => ContainsValues(str, "/me"))), Times.Exactly(1));
59+
_mock.Verify(client => client.DownloadJson<PrivateProfile>(
60+
It.Is<string>(str => ContainsValues(str, "/me")),
61+
It.IsNotNull<Dictionary<string, string>>()), Times.Exactly(1));
5962
}
6063

6164
[Test]
6265
public void ShouldGetPublicProfile()
6366
{
6467
PublicProfile profile = GetFixture<PublicProfile>("public-user.json");
65-
_mock.Setup(client => client.DownloadJson<PublicProfile>(It.IsAny<string>()))
68+
_mock.Setup(client => client.DownloadJson<PublicProfile>(It.IsAny<string>(), It.IsAny<Dictionary<string, string>>()))
6669
.Returns(new Tuple<ResponseInfo, PublicProfile>(ResponseInfo.Empty, profile));
6770

71+
72+
_spotify.UseAuth = false;
6873
Assert.AreEqual(profile, _spotify.GetPublicProfile("wizzler"));
69-
_mock.Verify(client => client.DownloadJson<PublicProfile>(It.Is<string>(str => ContainsValues(str, "/users/wizzler"))), Times.Exactly(1));
74+
_mock.Verify(client => client.DownloadJson<PublicProfile>(
75+
It.Is<string>(str => ContainsValues(str, "/users/wizzler")),
76+
It.Is<Dictionary<string, string>>(headers => headers.Count == 0)), Times.Exactly(1));
7077
}
7178

7279
//Will add more tests once I decided if this is worth the effort (propably not?)

‎SpotifyAPI/SpotifyAPI.csproj‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
<Reference Include="System.ComponentModel.DataAnnotations" />
4646
<Reference Include="System.Core" />
4747
<Reference Include="System.Drawing" />
48+
<Reference Include="System.Net.Http" />
4849
<Reference Include="System.Web" />
4950
<Reference Include="System.Windows.Forms" />
5051
<Reference Include="System.Xml.Linq" />

‎SpotifyAPI/Web/IClient.cs‎

Lines changed: 13 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using Newtonsoft.Json;
22
using System;
33
using System.Collections.Generic;
4+
using System.Net.Http.Headers;
45
using System.Threading.Tasks;
56
using SpotifyAPI.Web.Models;
67

@@ -15,44 +16,44 @@ public interface IClient : IDisposable
1516
/// </summary>
1617
/// <param name="url">An URL</param>
1718
/// <returns></returns>
18-
Tuple<ResponseInfo, string> Download(string url);
19+
Tuple<ResponseInfo, string> Download(string url, Dictionary<string, string> headers = null);
1920

2021
/// <summary>
2122
/// Downloads data async from an URL and returns it
2223
/// </summary>
2324
/// <param name="url"></param>
2425
/// <returns></returns>
25-
Task<Tuple<ResponseInfo, string>> DownloadAsync(string url);
26+
Task<Tuple<ResponseInfo, string>> DownloadAsync(string url, Dictionary<string, string> headers = null);
2627

2728
/// <summary>
2829
/// Downloads data from an URL and returns it
2930
/// </summary>
3031
/// <param name="url">An URL</param>
3132
/// <returns></returns>
32-
Tuple<ResponseInfo, byte[]> DownloadRaw(string url);
33+
Tuple<ResponseInfo, byte[]> DownloadRaw(string url, Dictionary<string, string> headers = null);
3334

3435
/// <summary>
3536
/// Downloads data async from an URL and returns it
3637
/// </summary>
3738
/// <param name="url"></param>
3839
/// <returns></returns>
39-
Task<Tuple<ResponseInfo, byte[]>> DownloadRawAsync(string url);
40+
Task<Tuple<ResponseInfo, byte[]>> DownloadRawAsync(string url, Dictionary<string, string> headers = null);
4041

4142
/// <summary>
4243
/// Downloads data from an URL and converts it to an object
4344
/// </summary>
4445
/// <typeparam name="T">The Type which the object gets converted to</typeparam>
4546
/// <param name="url">An URL</param>
4647
/// <returns></returns>
47-
Tuple<ResponseInfo, T> DownloadJson<T>(string url);
48+
Tuple<ResponseInfo, T> DownloadJson<T>(string url, Dictionary<string, string> headers = null);
4849

4950
/// <summary>
5051
/// Downloads data async from an URL and converts it to an object
5152
/// </summary>
5253
/// <typeparam name="T">The Type which the object gets converted to</typeparam>
5354
/// <param name="url">An URL</param>
5455
/// <returns></returns>
55-
Task<Tuple<ResponseInfo, T>> DownloadJsonAsync<T>(string url);
56+
Task<Tuple<ResponseInfo, T>> DownloadJsonAsync<T>(string url, Dictionary<string, string> headers = null);
5657

5758
/// <summary>
5859
/// Uploads data from an URL and returns the response
@@ -61,7 +62,7 @@ public interface IClient : IDisposable
6162
/// <param name="body">The Body-Data (most likely a JSON String)</param>
6263
/// <param name="method">The Upload-method (POST,DELETE,PUT)</param>
6364
/// <returns></returns>
64-
Tuple<ResponseInfo, string> Upload(string url, string body, string method);
65+
Tuple<ResponseInfo, string> Upload(string url, string body, string method, Dictionary<string, string> headers = null);
6566

6667
/// <summary>
6768
/// Uploads data async from an URL and returns the response
@@ -70,7 +71,7 @@ public interface IClient : IDisposable
7071
/// <param name="body">The Body-Data (most likely a JSON String)</param>
7172
/// <param name="method">The Upload-method (POST,DELETE,PUT)</param>
7273
/// <returns></returns>
73-
Task<Tuple<ResponseInfo, string>> UploadAsync(string url, string body, string method);
74+
Task<Tuple<ResponseInfo, string>> UploadAsync(string url, string body, string method, Dictionary<string, string> headers = null);
7475

7576
/// <summary>
7677
/// Uploads data from an URL and returns the response
@@ -79,7 +80,7 @@ public interface IClient : IDisposable
7980
/// <param name="body">The Body-Data (most likely a JSON String)</param>
8081
/// <param name="method">The Upload-method (POST,DELETE,PUT)</param>
8182
/// <returns></returns>
82-
Tuple<ResponseInfo, byte[]> UploadRaw(string url, string body, string method);
83+
Tuple<ResponseInfo, byte[]> UploadRaw(string url, string body, string method, Dictionary<string, string> headers = null);
8384

8485
/// <summary>
8586
/// Uploads data async from an URL and returns the response
@@ -88,7 +89,7 @@ public interface IClient : IDisposable
8889
/// <param name="body">The Body-Data (most likely a JSON String)</param>
8990
/// <param name="method">The Upload-method (POST,DELETE,PUT)</param>
9091
/// <returns></returns>
91-
Task<Tuple<ResponseInfo, byte[]>> UploadRawAsync(string url, string body, string method);
92+
Task<Tuple<ResponseInfo, byte[]>> UploadRawAsync(string url, string body, string method, Dictionary<string, string> headers = null);
9293

9394
/// <summary>
9495
/// Uploads data from an URL and converts the response to an object
@@ -98,7 +99,7 @@ public interface IClient : IDisposable
9899
/// <param name="body">The Body-Data (most likely a JSON String)</param>
99100
/// <param name="method">The Upload-method (POST,DELETE,PUT)</param>
100101
/// <returns></returns>
101-
Tuple<ResponseInfo, T> UploadJson<T>(string url, string body, string method);
102+
Tuple<ResponseInfo, T> UploadJson<T>(string url, string body, string method, Dictionary<string, string> headers = null);
102103

103104
/// <summary>
104105
/// Uploads data async from an URL and converts the response to an object
@@ -108,25 +109,6 @@ public interface IClient : IDisposable
108109
/// <param name="body">The Body-Data (most likely a JSON String)</param>
109110
/// <param name="method">The Upload-method (POST,DELETE,PUT)</param>
110111
/// <returns></returns>
111-
Task<Tuple<ResponseInfo, T>> UploadJsonAsync<T>(string url, string body, string method);
112-
113-
/// <summary>
114-
/// Sets a specific Header
115-
/// </summary>
116-
/// <param name="header">Header name</param>
117-
/// <param name="value">Header value</param>
118-
void SetHeader(string header, string value);
119-
120-
/// <summary>
121-
/// Removes a specific Header
122-
/// </summary>
123-
/// <param name="header">Header name</param>
124-
void RemoveHeader(string header);
125-
126-
/// <summary>
127-
/// Gets all current Headers
128-
/// </summary>
129-
/// <returns>A collection of Header KeyValue Pairs</returns>
130-
List<KeyValuePair<string, string>> GetHeaders();
112+
Task<Tuple<ResponseInfo, T>> UploadJsonAsync<T>(string url, string body, string method, Dictionary<string, string> headers = null);
131113
}
132114
}

‎SpotifyAPI/Web/SpotifyWebAPI.cs‎

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2115,11 +2115,14 @@ public T UploadData<T>(string url, string uploadData, string method = "POST") wh
21152115
Tuple<ResponseInfo, T> response = null;
21162116
do
21172117
{
2118-
WebClient.SetHeader("Authorization", TokenType + " " + AccessToken);
2119-
WebClient.SetHeader("Content-Type", "application/json");
2118+
Dictionary<string, string> headers = new Dictionary<string, string>
2119+
{
2120+
{ "Authorization", TokenType + " " + AccessToken},
2121+
{ "Content-Type", "application/json" }
2122+
};
21202123

21212124
if (response != null) { Thread.Sleep(RetryAfter); }
2122-
response = WebClient.UploadJson<T>(url, uploadData, method);
2125+
response = WebClient.UploadJson<T>(url, uploadData, method, headers);
21232126

21242127
response.Item2.AddResponseInfo(response.Item1);
21252128
lastError = response.Item2.Error;
@@ -2142,11 +2145,14 @@ public async Task<T> UploadDataAsync<T>(string url, string uploadData, string me
21422145
Tuple<ResponseInfo, T> response = null;
21432146
do
21442147
{
2145-
WebClient.SetHeader("Authorization", TokenType + " " + AccessToken);
2146-
WebClient.SetHeader("Content-Type", "application/json");
2148+
Dictionary<string, string> headers = new Dictionary<string, string>
2149+
{
2150+
{ "Authorization", TokenType + " " + AccessToken},
2151+
{ "Content-Type", "application/json" }
2152+
};
21472153

21482154
if (response != null) { await Task.Delay(RetryAfter).ConfigureAwait(false); }
2149-
response = await WebClient.UploadJsonAsync<T>(url, uploadData, method).ConfigureAwait(false);
2155+
response = await WebClient.UploadJsonAsync<T>(url, uploadData, method, headers).ConfigureAwait(false);
21502156

21512157
response.Item2.AddResponseInfo(response.Item1);
21522158
lastError = response.Item2.Error;
@@ -2204,20 +2210,18 @@ public async Task<T> DownloadDataAsync<T>(string url) where T : BasicModel
22042210

22052211
private Tuple<ResponseInfo, T> DownloadDataAlt<T>(string url)
22062212
{
2213+
Dictionary<string, string> headers = new Dictionary<string, string>();
22072214
if (UseAuth)
2208-
WebClient.SetHeader("Authorization", TokenType + " " + AccessToken);
2209-
else
2210-
WebClient.RemoveHeader("Authorization");
2211-
return WebClient.DownloadJson<T>(url);
2215+
headers.Add("Authorization", TokenType + " " + AccessToken);
2216+
return WebClient.DownloadJson<T>(url, headers);
22122217
}
22132218

22142219
private Task<Tuple<ResponseInfo, T>> DownloadDataAltAsync<T>(string url)
22152220
{
2221+
Dictionary<string, string> headers = new Dictionary<string, string>();
22162222
if (UseAuth)
2217-
WebClient.SetHeader("Authorization", TokenType + " " + AccessToken);
2218-
else
2219-
WebClient.RemoveHeader("Authorization");
2220-
return WebClient.DownloadJsonAsync<T>(url);
2223+
headers.Add("Authorization", TokenType + " " + AccessToken);
2224+
return WebClient.DownloadJsonAsync<T>(url, headers);
22212225
}
22222226

22232227
#endregion Util

0 commit comments

Comments
 (0)