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;
|
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();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-05 20:20:22 +00:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-16 23:44:35 +01:00
|
|
|
|
public byte[] DownloadRaw(string url)
|
2015-07-07 17:11:11 +01:00
|
|
|
|
{
|
|
|
|
|
return _webClient.DownloadData(url);
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-05 20:20:22 +00:00
|
|
|
|
public async Task<byte[]> DownloadRawAsync(string url)
|
|
|
|
|
{
|
|
|
|
|
return await _webClient.DownloadDataTaskAsync(url);
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-07 17:11:11 +01:00
|
|
|
|
public T DownloadJson<T>(string url)
|
|
|
|
|
{
|
|
|
|
|
String response = Download(url);
|
|
|
|
|
return JsonConvert.DeserializeObject<T>(response, JsonSettings);
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-05 20:20:22 +00:00
|
|
|
|
public async Task<T> DownloadJsonAsync<T>(string url)
|
|
|
|
|
{
|
|
|
|
|
String response = await DownloadAsync(url);
|
|
|
|
|
return JsonConvert.DeserializeObject<T>(response, JsonSettings);
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-07 17:11:11 +01:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-05 20:20:22 +00:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-07 17:11:11 +01:00
|
|
|
|
public byte[] UploadRaw(string url, string body, string method)
|
|
|
|
|
{
|
|
|
|
|
return _webClient.UploadData(url, method, _encoding.GetBytes(body));
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-05 20:20:22 +00:00
|
|
|
|
public async Task<byte[]> UploadRawAsync(string url, string body, string method)
|
|
|
|
|
{
|
|
|
|
|
return await _webClient.UploadDataTaskAsync(url, method, _encoding.GetBytes(body));
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-07 17:11:11 +01:00
|
|
|
|
public T UploadJson<T>(string url, string body, string method)
|
|
|
|
|
{
|
|
|
|
|
String response = Upload(url, body, method);
|
|
|
|
|
return JsonConvert.DeserializeObject<T>(response, JsonSettings);
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-05 20:20:22 +00:00
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|