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
89 lines (75 loc) · 3.64 KB
/
SpotifyWebClient.cs
File metadata and controls
89 lines (75 loc) · 3.64 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
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using SpotifyAPI.Web.Models;
namespace SpotifyAPI.Web
{
internal class SpotifyWebClient : IClient
{
public JsonSerializerSettings JsonSettings { get; set; }
private static readonly Encoding Encoding = Encoding.UTF8;
private HttpClient _httpClient;
public SpotifyWebClient()
{
_httpClient = new HttpClient();
}
public void Dispose()
{
_httpClient.Dispose();
}
public async Task<Tuple<ResponseInfo, string>> Download(string url, params KeyValuePair<string, string>[] headers)
{
Tuple<ResponseInfo, byte[]> raw = await DownloadRaw(url).ConfigureAwait(false);
return new Tuple<ResponseInfo, string>(raw.Item1, raw.Item2.Length > 0 ? Encoding.GetString(raw.Item2) : "{}");
}
public async Task<Tuple<ResponseInfo, byte[]>> DownloadRaw(string url, params KeyValuePair<string, string>[] headers)
{
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, url);
foreach (KeyValuePair<string, string> header in headers)
request.Headers.Add(header.Key, header.Value);
HttpResponseMessage result = await _httpClient.SendAsync(request).ConfigureAwait(false);
ResponseInfo info = new ResponseInfo
{
Headers = result.Headers
};
return new Tuple<ResponseInfo, byte[]>(info, await result.Content.ReadAsByteArrayAsync());
}
public async Task<Tuple<ResponseInfo, T>> DownloadJson<T>(string url, params KeyValuePair<string, string>[] headers)
{
Tuple<ResponseInfo, string> response = await Download(url).ConfigureAwait(false);
return new Tuple<ResponseInfo, T>(response.Item1, JsonConvert.DeserializeObject<T>(response.Item2, JsonSettings));
}
public async Task<Tuple<ResponseInfo, string>> Upload(string url, string body, string method, params KeyValuePair<string, string>[] headers)
{
Tuple<ResponseInfo, byte[]> data = await UploadRaw(url, body, method).ConfigureAwait(false);
return new Tuple<ResponseInfo, string>(data.Item1, data.Item2.Length > 0 ? Encoding.GetString(data.Item2) : "{}");
}
public async Task<Tuple<ResponseInfo, byte[]>> UploadRaw(string url, string body, string method, params KeyValuePair<string, string>[] headers)
{
var httpRequest = new HttpRequestMessage(new HttpMethod(method), url)
{
Content = new StringContent(url, Encoding)
};
foreach (KeyValuePair<string, string> header in headers)
{
httpRequest.Headers.Add(header.Key, header.Value);
}
HttpResponseMessage result = await _httpClient.SendAsync(httpRequest).ConfigureAwait(false);
ResponseInfo info = new ResponseInfo
{
Headers = result.Headers
};
return new Tuple<ResponseInfo, byte[]>(info, await result.Content.ReadAsByteArrayAsync());
}
public async Task<Tuple<ResponseInfo, T>> UploadJson<T>(string url, string body, string method, params KeyValuePair<string, string>[] headers)
{
Tuple<ResponseInfo, string> response = await Upload(url, body, method).ConfigureAwait(false);
return new Tuple<ResponseInfo, T>(response.Item1, JsonConvert.DeserializeObject<T>(response.Item2, JsonSettings));
}
}
}