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
175 lines (156 loc) · 5.22 KB
/
SpotifyWebClient.cs
File metadata and controls
175 lines (156 loc) · 5.22 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
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;
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 string Download(string url)
{
String response;
try
{
response = _encoding.GetString(DownloadRaw(url));
}
catch (WebException e)
{
using (StreamReader reader = new StreamReader(e.Response.GetResponseStream()))
{
response = reader.ReadToEnd();
}
}
return response;
}
public async Task<string> DownloadAsync(string url)
{
String response;
try
{
response = _encoding.GetString(await DownloadRawAsync(url));
}
catch (WebException e)
{
using (StreamReader reader = new StreamReader(e.Response.GetResponseStream()))
{
response = reader.ReadToEnd();
}
}
return response;
}
public byte[] DownloadRaw(string url)
{
return _webClient.DownloadData(url);
}
public async Task<byte[]> DownloadRawAsync(string url)
{
using (WebClient webClient = new WebClient())
{
webClient.Proxy = null;
webClient.Encoding = _encoding;
webClient.Headers = _webClient.Headers;
return await _webClient.DownloadDataTaskAsync(url);
}
}
public T DownloadJson<T>(string url)
{
String response = Download(url);
return JsonConvert.DeserializeObject<T>(response, JsonSettings);
}
public async Task<T> DownloadJsonAsync<T>(string url)
{
String response = await DownloadAsync(url);
return JsonConvert.DeserializeObject<T>(response, JsonSettings);
}
public string Upload(string url, string body, string method)
{
String response;
try
{
byte[] data = UploadRaw(url, body, method);
response = _encoding.GetString(data);
}
catch (WebException e)
{
using (StreamReader reader = new StreamReader(e.Response.GetResponseStream()))
{
response = reader.ReadToEnd();
}
}
return response;
}
public async Task<string> UploadAsync(string url, string body, string method)
{
String response;
try
{
byte[] data = await UploadRawAsync(url, body, method);
response = _encoding.GetString(data);
}
catch (WebException e)
{
using (StreamReader reader = new StreamReader(e.Response.GetResponseStream()))
{
response = reader.ReadToEnd();
}
}
return response;
}
public byte[] UploadRaw(string url, string body, string method)
{
return _webClient.UploadData(url, method, _encoding.GetBytes(body));
}
public async Task<byte[]> UploadRawAsync(string url, string body, string method)
{
using (WebClient webClient = new WebClient())
{
webClient.Proxy = null;
webClient.Encoding = _encoding;
webClient.Headers = _webClient.Headers;
return await _webClient.UploadDataTaskAsync(url, method, _encoding.GetBytes(body));
}
}
public T UploadJson<T>(string url, string body, string method)
{
String response = Upload(url, body, method);
return JsonConvert.DeserializeObject<T>(response, JsonSettings);
}
public async Task<T> UploadJsonAsync<T>(string url, string body, string method)
{
String response = await UploadAsync(url, body, method);
return JsonConvert.DeserializeObject<T>(response, 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();
}
}
}