2015-10-16 23:44:35 +01:00
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
using System;
|
2015-07-07 17:11:11 +01:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Net;
|
|
|
|
|
using System.Text;
|
2015-11-05 20:20:22 +00:00
|
|
|
|
using System.Threading.Tasks;
|
2016-07-07 20:23:36 +01:00
|
|
|
|
using SpotifyAPI.Web.Models;
|
2015-07-07 17:11:11 +01:00
|
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-07 20:23:36 +01:00
|
|
|
|
public Tuple<ResponseInfo, string> Download(string url)
|
2015-07-07 17:11:11 +01:00
|
|
|
|
{
|
2016-07-07 20:23:36 +01:00
|
|
|
|
Tuple<ResponseInfo, string> response;
|
2015-07-07 17:11:11 +01:00
|
|
|
|
try
|
|
|
|
|
{
|
2016-07-07 20:23:36 +01:00
|
|
|
|
Tuple<ResponseInfo, byte[]> raw = DownloadRaw(url);
|
2016-07-23 22:35:23 +01:00
|
|
|
|
response = new Tuple<ResponseInfo, string>(raw.Item1, raw.Item2.Length > 0 ? _encoding.GetString(raw.Item2) : "{}");
|
2015-07-07 17:11:11 +01:00
|
|
|
|
}
|
|
|
|
|
catch (WebException e)
|
|
|
|
|
{
|
|
|
|
|
using (StreamReader reader = new StreamReader(e.Response.GetResponseStream()))
|
|
|
|
|
{
|
2016-07-07 20:23:36 +01:00
|
|
|
|
response = new Tuple<ResponseInfo, string>(new ResponseInfo
|
|
|
|
|
{
|
|
|
|
|
Headers = _webClient.ResponseHeaders
|
|
|
|
|
}, reader.ReadToEnd());
|
2015-07-07 17:11:11 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return response;
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-07 20:23:36 +01:00
|
|
|
|
public async Task<Tuple<ResponseInfo, string>> DownloadAsync(string url)
|
2015-11-05 20:20:22 +00:00
|
|
|
|
{
|
2016-07-07 20:23:36 +01:00
|
|
|
|
Tuple<ResponseInfo, string> response;
|
2015-11-05 20:20:22 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
2016-09-08 11:06:24 +01:00
|
|
|
|
Tuple<ResponseInfo, byte[]> raw = await DownloadRawAsync(url).ConfigureAwait(false);
|
2016-07-23 22:35:23 +01:00
|
|
|
|
response = new Tuple<ResponseInfo, string>(raw.Item1, raw.Item2.Length > 0 ? _encoding.GetString(raw.Item2) : "{}");
|
2015-11-05 20:20:22 +00:00
|
|
|
|
}
|
|
|
|
|
catch (WebException e)
|
|
|
|
|
{
|
|
|
|
|
using (StreamReader reader = new StreamReader(e.Response.GetResponseStream()))
|
|
|
|
|
{
|
2016-07-07 20:23:36 +01:00
|
|
|
|
response = new Tuple<ResponseInfo, string>(new ResponseInfo
|
|
|
|
|
{
|
|
|
|
|
Headers = _webClient.ResponseHeaders
|
|
|
|
|
}, reader.ReadToEnd());
|
2015-11-05 20:20:22 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return response;
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-07 20:23:36 +01:00
|
|
|
|
public Tuple<ResponseInfo, byte[]> DownloadRaw(string url)
|
2015-07-07 17:11:11 +01:00
|
|
|
|
{
|
2016-07-07 20:23:36 +01:00
|
|
|
|
byte[] data = _webClient.DownloadData(url);
|
|
|
|
|
ResponseInfo info = new ResponseInfo()
|
|
|
|
|
{
|
|
|
|
|
Headers = _webClient.ResponseHeaders
|
|
|
|
|
};
|
|
|
|
|
return new Tuple<ResponseInfo, byte[]>(info, data);
|
2015-07-07 17:11:11 +01:00
|
|
|
|
}
|
|
|
|
|
|
2016-07-07 20:23:36 +01:00
|
|
|
|
public async Task<Tuple<ResponseInfo, byte[]>> DownloadRawAsync(string url)
|
2015-11-05 20:20:22 +00:00
|
|
|
|
{
|
2015-11-15 19:54:30 +00:00
|
|
|
|
using (WebClient webClient = new WebClient())
|
|
|
|
|
{
|
|
|
|
|
webClient.Proxy = null;
|
|
|
|
|
webClient.Encoding = _encoding;
|
|
|
|
|
webClient.Headers = _webClient.Headers;
|
2016-07-07 20:23:36 +01:00
|
|
|
|
|
2016-09-08 11:06:24 +01:00
|
|
|
|
byte[] data = await _webClient.DownloadDataTaskAsync(url).ConfigureAwait(false);
|
2016-07-07 20:23:36 +01:00
|
|
|
|
ResponseInfo info = new ResponseInfo()
|
|
|
|
|
{
|
|
|
|
|
Headers = webClient.ResponseHeaders
|
|
|
|
|
};
|
|
|
|
|
return new Tuple<ResponseInfo, byte[]>(info, data);
|
2015-11-15 19:54:30 +00:00
|
|
|
|
}
|
2015-11-05 20:20:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-07-07 20:23:36 +01:00
|
|
|
|
public Tuple<ResponseInfo, T> DownloadJson<T>(string url)
|
2015-07-07 17:11:11 +01:00
|
|
|
|
{
|
2016-07-07 20:23:36 +01:00
|
|
|
|
Tuple<ResponseInfo, string> response = Download(url);
|
|
|
|
|
return new Tuple<ResponseInfo, T>(response.Item1, JsonConvert.DeserializeObject<T>(response.Item2, JsonSettings));
|
2015-07-07 17:11:11 +01:00
|
|
|
|
}
|
|
|
|
|
|
2016-07-07 20:23:36 +01:00
|
|
|
|
public async Task<Tuple<ResponseInfo, T>> DownloadJsonAsync<T>(string url)
|
2015-11-05 20:20:22 +00:00
|
|
|
|
{
|
2016-09-08 11:06:24 +01:00
|
|
|
|
Tuple<ResponseInfo, string> response = await DownloadAsync(url).ConfigureAwait(false);
|
2016-07-07 20:23:36 +01:00
|
|
|
|
return new Tuple<ResponseInfo, T>(response.Item1, JsonConvert.DeserializeObject<T>(response.Item2, JsonSettings));
|
2015-11-05 20:20:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-07-07 20:23:36 +01:00
|
|
|
|
public Tuple<ResponseInfo, string> Upload(string url, string body, string method)
|
2015-07-07 17:11:11 +01:00
|
|
|
|
{
|
2016-07-07 20:23:36 +01:00
|
|
|
|
Tuple<ResponseInfo, string> response;
|
2015-07-07 17:11:11 +01:00
|
|
|
|
try
|
|
|
|
|
{
|
2016-07-07 20:23:36 +01:00
|
|
|
|
Tuple<ResponseInfo, byte[]> data = UploadRaw(url, body, method);
|
2016-07-23 22:35:23 +01:00
|
|
|
|
response = new Tuple<ResponseInfo, string>(data.Item1, data.Item2.Length > 0 ? _encoding.GetString(data.Item2) : "{}");
|
2015-07-07 17:11:11 +01:00
|
|
|
|
}
|
|
|
|
|
catch (WebException e)
|
|
|
|
|
{
|
|
|
|
|
using (StreamReader reader = new StreamReader(e.Response.GetResponseStream()))
|
|
|
|
|
{
|
2016-07-07 20:23:36 +01:00
|
|
|
|
response = new Tuple<ResponseInfo, string>(new ResponseInfo
|
|
|
|
|
{
|
|
|
|
|
Headers = _webClient.ResponseHeaders
|
|
|
|
|
}, reader.ReadToEnd());
|
2015-07-07 17:11:11 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return response;
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-07 20:23:36 +01:00
|
|
|
|
public async Task<Tuple<ResponseInfo, string>> UploadAsync(string url, string body, string method)
|
2015-11-05 20:20:22 +00:00
|
|
|
|
{
|
2016-07-07 20:23:36 +01:00
|
|
|
|
Tuple<ResponseInfo, string> response;
|
2015-11-05 20:20:22 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
2016-09-08 11:06:24 +01:00
|
|
|
|
Tuple<ResponseInfo, byte[]> data = await UploadRawAsync(url, body, method).ConfigureAwait(false);
|
2016-07-23 22:35:23 +01:00
|
|
|
|
response = new Tuple<ResponseInfo, string>(data.Item1, data.Item2.Length > 0 ? _encoding.GetString(data.Item2) : "{}");
|
2015-11-05 20:20:22 +00:00
|
|
|
|
}
|
|
|
|
|
catch (WebException e)
|
|
|
|
|
{
|
|
|
|
|
using (StreamReader reader = new StreamReader(e.Response.GetResponseStream()))
|
|
|
|
|
{
|
2016-07-07 20:23:36 +01:00
|
|
|
|
response = new Tuple<ResponseInfo, string>(new ResponseInfo
|
|
|
|
|
{
|
|
|
|
|
Headers = _webClient.ResponseHeaders
|
|
|
|
|
}, reader.ReadToEnd());
|
2015-11-05 20:20:22 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return response;
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-07 20:23:36 +01:00
|
|
|
|
public Tuple<ResponseInfo, byte[]> UploadRaw(string url, string body, string method)
|
2015-07-07 17:11:11 +01:00
|
|
|
|
{
|
2016-07-07 20:23:36 +01:00
|
|
|
|
byte[] data = _webClient.UploadData(url, method, _encoding.GetBytes(body));
|
|
|
|
|
ResponseInfo info = new ResponseInfo
|
|
|
|
|
{
|
|
|
|
|
Headers = _webClient.ResponseHeaders
|
|
|
|
|
};
|
|
|
|
|
return new Tuple<ResponseInfo, byte[]>(info, data);
|
2015-07-07 17:11:11 +01:00
|
|
|
|
}
|
|
|
|
|
|
2016-07-07 20:23:36 +01:00
|
|
|
|
public async Task<Tuple<ResponseInfo, byte[]>> UploadRawAsync(string url, string body, string method)
|
2015-11-05 20:20:22 +00:00
|
|
|
|
{
|
2015-11-15 19:54:30 +00:00
|
|
|
|
using (WebClient webClient = new WebClient())
|
|
|
|
|
{
|
|
|
|
|
webClient.Proxy = null;
|
|
|
|
|
webClient.Encoding = _encoding;
|
|
|
|
|
webClient.Headers = _webClient.Headers;
|
2016-09-08 11:06:24 +01:00
|
|
|
|
byte[] data = await _webClient.UploadDataTaskAsync(url, method, _encoding.GetBytes(body)).ConfigureAwait(false);
|
2016-07-07 20:23:36 +01:00
|
|
|
|
ResponseInfo info = new ResponseInfo
|
|
|
|
|
{
|
|
|
|
|
Headers = _webClient.ResponseHeaders
|
|
|
|
|
};
|
|
|
|
|
return new Tuple<ResponseInfo, byte[]>(info, data);
|
2015-11-15 19:54:30 +00:00
|
|
|
|
}
|
2015-11-05 20:20:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-07-07 20:23:36 +01:00
|
|
|
|
public Tuple<ResponseInfo, T> UploadJson<T>(string url, string body, string method)
|
2015-07-07 17:11:11 +01:00
|
|
|
|
{
|
2016-07-07 20:23:36 +01:00
|
|
|
|
Tuple<ResponseInfo, string> response = Upload(url, body, method);
|
|
|
|
|
return new Tuple<ResponseInfo, T>(response.Item1, JsonConvert.DeserializeObject<T>(response.Item2, JsonSettings));
|
2015-07-07 17:11:11 +01:00
|
|
|
|
}
|
|
|
|
|
|
2016-07-07 20:23:36 +01:00
|
|
|
|
public async Task<Tuple<ResponseInfo, T>> UploadJsonAsync<T>(string url, string body, string method)
|
2015-11-05 20:20:22 +00:00
|
|
|
|
{
|
2016-09-08 11:06:24 +01:00
|
|
|
|
Tuple<ResponseInfo, string> response = await UploadAsync(url, body, method).ConfigureAwait(false);
|
2016-07-07 20:23:36 +01:00
|
|
|
|
return new Tuple<ResponseInfo, T>(response.Item1, JsonConvert.DeserializeObject<T>(response.Item2, JsonSettings));
|
2015-11-05 20:20:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-07-07 17:11:11 +01:00
|
|
|
|
public void SetHeader(string header, string value)
|
|
|
|
|
{
|
|
|
|
|
_webClient.Headers[header] = value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void RemoveHeader(string header)
|
|
|
|
|
{
|
2015-10-16 23:44:35 +01:00
|
|
|
|
if (_webClient.Headers[header] != null)
|
2015-07-07 17:11:11 +01:00
|
|
|
|
_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();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|