Spotify.NET/SpotifyAPI/Web/SpotifyWebClient.cs

163 lines
4.8 KiB
C#
Raw Normal View History

using Newtonsoft.Json;
using System;
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;
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;
}
public byte[] DownloadRaw(string url)
{
return _webClient.DownloadData(url);
}
2015-11-05 20:20:22 +00:00
public async Task<byte[]> DownloadRawAsync(string url)
{
return await _webClient.DownloadDataTaskAsync(url);
}
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);
}
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;
}
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));
}
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);
}
public void SetHeader(string header, string value)
{
_webClient.Headers[header] = value;
}
public void RemoveHeader(string header)
{
if (_webClient.Headers[header] != null)
_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();
}
}
}