forked from JohnnyCrazy/SpotifyAPI-NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpotifyWebClient.cs
More file actions
211 lines (191 loc) · 7.89 KB
/
SpotifyWebClient.cs
File metadata and controls
211 lines (191 loc) · 7.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using SpotifyAPI.Web.Models;
namespace SpotifyAPI.Web
{
internal class SpotifyWebClient : IClient
{
public JsonSerializerSettings JsonSettings { get; set; }
private readonly WebClient _webClient;
private readonly Encoding _encoding = Encoding.UTF8;
public SpotifyWebClient()
{
_webClient = new WebClient()
{
Proxy = null,
Encoding = _encoding
};
}
public void Dispose()
{
_webClient.Dispose();
}
public Tuple<ResponseInfo, string> Download(string url)
{
Tuple<ResponseInfo, string> response;
try
{
Tuple<ResponseInfo, byte[]> raw = DownloadRaw(url);
response = new Tuple<ResponseInfo, string>(raw.Item1, raw.Item2.Length > 0 ? _encoding.GetString(raw.Item2) : "{}");
}
catch (WebException e)
{
using (StreamReader reader = new StreamReader(e.Response.GetResponseStream()))
{
response = new Tuple<ResponseInfo, string>(new ResponseInfo
{
Headers = _webClient.ResponseHeaders
}, reader.ReadToEnd());
}
}
return response;
}
public async Task<Tuple<ResponseInfo, string>> DownloadAsync(string url)
{
Tuple<ResponseInfo, string> response;
try
{
Tuple<ResponseInfo, byte[]> raw = await DownloadRawAsync(url).ConfigureAwait(false);
response = new Tuple<ResponseInfo, string>(raw.Item1, raw.Item2.Length > 0 ? _encoding.GetString(raw.Item2) : "{}");
}
catch (WebException e)
{
using (StreamReader reader = new StreamReader(e.Response.GetResponseStream()))
{
response = new Tuple<ResponseInfo, string>(new ResponseInfo
{
Headers = _webClient.ResponseHeaders
}, reader.ReadToEnd());
}
}
return response;
}
public Tuple<ResponseInfo, byte[]> DownloadRaw(string url)
{
byte[] data = _webClient.DownloadData(url);
ResponseInfo info = new ResponseInfo()
{
Headers = _webClient.ResponseHeaders
};
return new Tuple<ResponseInfo, byte[]>(info, data);
}
public async Task<Tuple<ResponseInfo, byte[]>> DownloadRawAsync(string url)
{
using (WebClient webClient = new WebClient())
{
webClient.Proxy = null;
webClient.Encoding = _encoding;
webClient.Headers = _webClient.Headers;
byte[] data = await _webClient.DownloadDataTaskAsync(url).ConfigureAwait(false);
ResponseInfo info = new ResponseInfo()
{
Headers = webClient.ResponseHeaders
};
return new Tuple<ResponseInfo, byte[]>(info, data);
}
}
public Tuple<ResponseInfo, T> DownloadJson<T>(string url)
{
Tuple<ResponseInfo, string> response = Download(url);
return new Tuple<ResponseInfo, T>(response.Item1, JsonConvert.DeserializeObject<T>(response.Item2, JsonSettings));
}
public async Task<Tuple<ResponseInfo, T>> DownloadJsonAsync<T>(string url)
{
Tuple<ResponseInfo, string> response = await DownloadAsync(url).ConfigureAwait(false);
return new Tuple<ResponseInfo, T>(response.Item1, JsonConvert.DeserializeObject<T>(response.Item2, JsonSettings));
}
public Tuple<ResponseInfo, string> Upload(string url, string body, string method)
{
Tuple<ResponseInfo, string> response;
try
{
Tuple<ResponseInfo, byte[]> data = UploadRaw(url, body, method);
response = new Tuple<ResponseInfo, string>(data.Item1, data.Item2.Length > 0 ? _encoding.GetString(data.Item2) : "{}");
}
catch (WebException e)
{
using (StreamReader reader = new StreamReader(e.Response.GetResponseStream()))
{
response = new Tuple<ResponseInfo, string>(new ResponseInfo
{
Headers = _webClient.ResponseHeaders
}, reader.ReadToEnd());
}
}
return response;
}
public async Task<Tuple<ResponseInfo, string>> UploadAsync(string url, string body, string method)
{
Tuple<ResponseInfo, string> response;
try
{
Tuple<ResponseInfo, byte[]> data = await UploadRawAsync(url, body, method).ConfigureAwait(false);
response = new Tuple<ResponseInfo, string>(data.Item1, data.Item2.Length > 0 ? _encoding.GetString(data.Item2) : "{}");
}
catch (WebException e)
{
using (StreamReader reader = new StreamReader(e.Response.GetResponseStream()))
{
response = new Tuple<ResponseInfo, string>(new ResponseInfo
{
Headers = _webClient.ResponseHeaders
}, reader.ReadToEnd());
}
}
return response;
}
public Tuple<ResponseInfo, byte[]> UploadRaw(string url, string body, string method)
{
byte[] data = _webClient.UploadData(url, method, _encoding.GetBytes(body));
ResponseInfo info = new ResponseInfo
{
Headers = _webClient.ResponseHeaders
};
return new Tuple<ResponseInfo, byte[]>(info, data);
}
public async Task<Tuple<ResponseInfo, byte[]>> UploadRawAsync(string url, string body, string method)
{
using (WebClient webClient = new WebClient())
{
webClient.Proxy = null;
webClient.Encoding = _encoding;
webClient.Headers = _webClient.Headers;
byte[] data = await _webClient.UploadDataTaskAsync(url, method, _encoding.GetBytes(body)).ConfigureAwait(false);
ResponseInfo info = new ResponseInfo
{
Headers = _webClient.ResponseHeaders
};
return new Tuple<ResponseInfo, byte[]>(info, data);
}
}
public Tuple<ResponseInfo, T> UploadJson<T>(string url, string body, string method)
{
Tuple<ResponseInfo, string> response = Upload(url, body, method);
return new Tuple<ResponseInfo, T>(response.Item1, JsonConvert.DeserializeObject<T>(response.Item2, JsonSettings));
}
public async Task<Tuple<ResponseInfo, T>> UploadJsonAsync<T>(string url, string body, string method)
{
Tuple<ResponseInfo, string> response = await UploadAsync(url, body, method).ConfigureAwait(false);
return new Tuple<ResponseInfo, T>(response.Item1, JsonConvert.DeserializeObject<T>(response.Item2, JsonSettings));
}
public void SetHeader(string header, string value)
{
_webClient.Headers[header] = value;
}
public void RemoveHeader(string header)
{
if (_webClient.Headers[header] != null)
_webClient.Headers.Remove(header);
}
public List<KeyValuePair<string, string>> GetHeaders()
{
return _webClient.Headers.AllKeys.Select(header => new KeyValuePair<string, string>(header, _webClient.Headers[header])).ToList();
}
}
}