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
197 lines (175 loc) · 7.35 KB
/
SpotifyWebClient.cs
File metadata and controls
197 lines (175 loc) · 7.35 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
using Newtonsoft.Json;
using SpotifyAPI.Web.Models;
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
namespace SpotifyAPI.Web;
internal sealed class SpotifyWebClient : IClient
{
public JsonSerializerSettings JsonSettings { get; set; }
private readonly Encoding _encoding = Encoding.UTF8;
private readonly HttpClient _client;
private const string _unknownErrorJson = "{\"error\": { \"status\": 0, \"message\": \"SpotifyAPI.Web - Unkown Spotify Error\" }}";
public SpotifyWebClient(ProxyConfig proxyConfig = null)
{
var clientHandler = ProxyConfig.CreateClientHandler(proxyConfig);
_client = new HttpClient(clientHandler);
}
public Tuple<ResponseInfo, string> Download(string url, Dictionary<string, string> headers = null)
{
var raw = DownloadRaw(url, headers);
return new Tuple<ResponseInfo, string>(raw.Item1, raw.Item2.Length > 0 ? _encoding.GetString(raw.Item2) : "{}");
}
public async Task<Tuple<ResponseInfo, string>> DownloadAsync(string url, Dictionary<string, string> headers = null)
{
var raw = await DownloadRawAsync(url, headers).ConfigureAwait(false);
return new Tuple<ResponseInfo, string>(raw.Item1, raw.Item2.Length > 0 ? _encoding.GetString(raw.Item2) : "{}");
}
public Tuple<ResponseInfo, byte[]> DownloadRaw(string url, Dictionary<string, string> headers = null)
{
if (headers != null)
{
AddHeaders(headers);
}
using var response = Task.Run(() => _client.GetAsync(url)).Result;
return new Tuple<ResponseInfo, byte[]>(new ResponseInfo
{
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)
{
if (headers != null)
{
AddHeaders(headers);
}
using var response = await _client.GetAsync(url).ConfigureAwait(false);
return new Tuple<ResponseInfo, byte[]>(new ResponseInfo
{
StatusCode = response.StatusCode,
Headers = ConvertHeaders(response.Headers)
}, await response.Content.ReadAsByteArrayAsync());
}
public Tuple<ResponseInfo, T> DownloadJson<T>(string url, Dictionary<string, string> headers = null)
{
var response = Download(url, headers);
try
{
return new Tuple<ResponseInfo, T>(response.Item1, JsonConvert.DeserializeObject<T>(response.Item2, JsonSettings));
}
catch (JsonException)
{
return new Tuple<ResponseInfo, T>(response.Item1, JsonConvert.DeserializeObject<T>(_unknownErrorJson, JsonSettings));
}
}
public async Task<Tuple<ResponseInfo, T>> DownloadJsonAsync<T>(string url, Dictionary<string, string> headers = null)
{
var response = await DownloadAsync(url, headers).ConfigureAwait(false);
try
{
return new Tuple<ResponseInfo, T>(response.Item1, JsonConvert.DeserializeObject<T>(response.Item2, JsonSettings));
}
catch (JsonException)
{
return new Tuple<ResponseInfo, T>(response.Item1, JsonConvert.DeserializeObject<T>(_unknownErrorJson, JsonSettings));
}
}
public Tuple<ResponseInfo, string> Upload(string url, string body, string method, Dictionary<string, string> headers = null)
{
var data = UploadRaw(url, body, method, headers);
return new Tuple<ResponseInfo, string>(data.Item1, data.Item2.Length > 0 ? _encoding.GetString(data.Item2) : "{}");
}
public async Task<Tuple<ResponseInfo, string>> UploadAsync(string url, string body, string method, Dictionary<string, string> headers = null)
{
var data = await UploadRawAsync(url, body, method, headers).ConfigureAwait(false);
return new Tuple<ResponseInfo, string>(data.Item1, data.Item2.Length > 0 ? _encoding.GetString(data.Item2) : "{}");
}
public Tuple<ResponseInfo, byte[]> UploadRaw(string url, string body, string method, Dictionary<string, string> headers = null)
{
if (headers != null)
{
AddHeaders(headers);
}
var message = new HttpRequestMessage(new HttpMethod(method), url)
{
Content = new StringContent(body, _encoding)
};
using var response = Task.Run(() => _client.SendAsync(message)).Result;
return new Tuple<ResponseInfo, byte[]>(new ResponseInfo
{
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)
{
if (headers != null)
{
AddHeaders(headers);
}
var message = new HttpRequestMessage(new HttpMethod(method), url)
{
Content = new StringContent(body, _encoding)
};
using var response = await _client.SendAsync(message);
return new Tuple<ResponseInfo, byte[]>(new ResponseInfo
{
StatusCode = response.StatusCode,
Headers = ConvertHeaders(response.Headers)
}, await response.Content.ReadAsByteArrayAsync());
}
public Tuple<ResponseInfo, T> UploadJson<T>(string url, string body, string method, Dictionary<string, string> headers = null)
{
var response = Upload(url, body, method, headers);
try
{
return new Tuple<ResponseInfo, T>(response.Item1, JsonConvert.DeserializeObject<T>(response.Item2, JsonSettings));
}
catch (JsonException)
{
return new Tuple<ResponseInfo, T>(response.Item1, JsonConvert.DeserializeObject<T>(_unknownErrorJson, JsonSettings));
}
}
public async Task<Tuple<ResponseInfo, T>> UploadJsonAsync<T>(string url, string body, string method, Dictionary<string, string> headers = null)
{
var response = await UploadAsync(url, body, method, headers).ConfigureAwait(false);
try
{
return new Tuple<ResponseInfo, T>(response.Item1, JsonConvert.DeserializeObject<T>(response.Item2, JsonSettings));
}
catch (JsonException)
{
return new Tuple<ResponseInfo, T>(response.Item1, JsonConvert.DeserializeObject<T>(_unknownErrorJson, JsonSettings));
}
}
public void Dispose()
{
_client.Dispose();
GC.SuppressFinalize(this);
}
private static WebHeaderCollection ConvertHeaders(HttpResponseHeaders headers)
{
WebHeaderCollection newHeaders = [];
foreach (var headerPair in headers)
{
foreach (var headerValue in headerPair.Value)
{
newHeaders.Add(headerPair.Key, headerValue);
}
}
return newHeaders;
}
private void AddHeaders(Dictionary<string, string> headers)
{
_client.DefaultRequestHeaders.Clear();
foreach (var headerPair in headers)
{
_client.DefaultRequestHeaders.TryAddWithoutValidation(headerPair.Key, headerPair.Value);
}
}
}