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 (73 loc) · 3.72 KB
/
Copy pathSpotifyWebClient.cs
File metadata and controls
89 lines (73 loc) · 3.72 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
namespace SpotifyAPI.PCL.Web
{
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using SpotifyAPI.PCL.Web.Models;
internal class SpotifyWebClient : IClient
{
public JsonSerializerSettings JsonSettings { get; set; }
private static readonly Encoding Encoding = Encoding.UTF8;
private HttpClient _httpClient;
public SpotifyWebClient()
{
this._httpClient = new HttpClient();
}
public void Dispose()
{
this._httpClient.Dispose();
}
public async Task<Tuple<ResponseInfo, string>> Download(string url, params KeyValuePair<string, string>[] headers)
{
Tuple<ResponseInfo, byte[]> raw = await this.DownloadRaw(url).ConfigureAwait(false);
return new Tuple<ResponseInfo, string>(raw.Item1, raw.Item2.Length > 0 ? Encoding.GetString(raw.Item2, 0 ,raw.Item2.Length) : "{}");
}
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 this._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 this.Download(url).ConfigureAwait(false);
return new Tuple<ResponseInfo, T>(response.Item1, JsonConvert.DeserializeObject<T>(response.Item2, this.JsonSettings));
}
public async Task<Tuple<ResponseInfo, string>> Upload(string url, string body, string method, params KeyValuePair<string, string>[] headers)
{
Tuple<ResponseInfo, byte[]> data = await this.UploadRaw(url, body, method).ConfigureAwait(false);
return new Tuple<ResponseInfo, string>(data.Item1, data.Item2.Length > 0 ? Encoding.GetString(data.Item2,0, data.Item2.Length) : "{}");
}
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 this._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 this.Upload(url, body, method).ConfigureAwait(false);
return new Tuple<ResponseInfo, T>(response.Item1, JsonConvert.DeserializeObject<T>(response.Item2, this.JsonSettings));
}
}
}