Implemented Async-Structure.

This commit is contained in:
Johnny @PC 2015-11-05 21:20:22 +01:00
parent 96bcb7bd36
commit 0dd94eaf41
4 changed files with 149 additions and 10 deletions

View File

@ -102,6 +102,7 @@
<Compile Include="Web\Enums\SearchType.cs" />
<Compile Include="Web\SimpleHttpServer.cs" />
<Compile Include="Web\SpotifyWebAPI.cs" />
<Compile Include="Web\SpotifyWebBuilder.cs" />
<Compile Include="Web\Util.cs" />
<Compile Include="Web\SpotifyWebClient.cs" />
</ItemGroup>

View File

@ -1,6 +1,7 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace SpotifyAPI.Web
{
@ -15,6 +16,13 @@ namespace SpotifyAPI.Web
/// <returns></returns>
string Download(string url);
/// <summary>
/// Downloads data async from an URL and returns it
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
Task<string> DownloadAsync(string url);
/// <summary>
/// Downloads data from an URL and returns it
/// </summary>
@ -22,6 +30,13 @@ namespace SpotifyAPI.Web
/// <returns></returns>
byte[] DownloadRaw(string url);
/// <summary>
/// Downloads data async from an URL and returns it
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
Task<byte[]> DownloadRawAsync(string url);
/// <summary>
/// Downloads data from an URL and converts it to an object
/// </summary>
@ -30,6 +45,14 @@ namespace SpotifyAPI.Web
/// <returns></returns>
T DownloadJson<T>(string url);
/// <summary>
/// Downloads data async from an URL and converts it to an object
/// </summary>
/// <typeparam name="T">The Type which the object gets converted to</typeparam>
/// <param name="url">An URL</param>
/// <returns></returns>
Task<T> DownloadJsonAsync<T>(string url);
/// <summary>
/// Uploads data from an URL and returns the response
/// </summary>
@ -39,6 +62,15 @@ namespace SpotifyAPI.Web
/// <returns></returns>
string Upload(string url, string body, string method);
/// <summary>
/// Uploads data async from an URL and returns the response
/// </summary>
/// <param name="url">An URL</param>
/// <param name="body">The Body-Data (most likely a JSON String)</param>
/// <param name="method">The Upload-method (POST,DELETE,PUT)</param>
/// <returns></returns>
Task<string> UploadAsync(string url, string body, string method);
/// <summary>
/// Uploads data from an URL and returns the response
/// </summary>
@ -48,6 +80,15 @@ namespace SpotifyAPI.Web
/// <returns></returns>
byte[] UploadRaw(string url, string body, string method);
/// <summary>
/// Uploads data async from an URL and returns the response
/// </summary>
/// <param name="url">An URL</param>
/// <param name="body">The Body-Data (most likely a JSON String)</param>
/// <param name="method">The Upload-method (POST,DELETE,PUT)</param>
/// <returns></returns>
Task<byte[]> UploadRawAsync(string url, string body, string method);
/// <summary>
/// Uploads data from an URL and converts the response to an object
/// </summary>
@ -58,6 +99,16 @@ namespace SpotifyAPI.Web
/// <returns></returns>
T UploadJson<T>(string url, string body, string method);
/// <summary>
/// Uploads data async from an URL and converts the response to an object
/// </summary>
/// <typeparam name="T">The Type which the object gets converted to</typeparam>
/// <param name="url">An URL</param>
/// <param name="body">The Body-Data (most likely a JSON String)</param>
/// <param name="method">The Upload-method (POST,DELETE,PUT)</param>
/// <returns></returns>
Task<T> UploadJsonAsync<T>(string url, string body, string method);
/// <summary>
/// Sets a specific Header
/// </summary>

View File

@ -6,15 +6,20 @@ using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SpotifyAPI.Web
{
public sealed class SpotifyWebAPI : IDisposable
{
public const String APIBase = "https://api.spotify.com/v1";
[Obsolete("This Property will be removed soon. Please use SpotifyWebBuilder.APIBase")]
public const String APIBase = SpotifyWebBuilder.APIBase;
private readonly SpotifyWebBuilder _builder;
public SpotifyWebAPI()
{
_builder = new SpotifyWebBuilder();
UseAuth = true;
WebClient = new SpotifyWebClient
{
@ -51,15 +56,21 @@ namespace SpotifyAPI.Web
/// <returns></returns>
public SearchItem SearchItems(String q, SearchType type, int limit = 20, int offset = 0, String market = "")
{
limit = Math.Min(50, limit);
StringBuilder builder = new StringBuilder(APIBase + "/search");
builder.Append("?q=" + q);
builder.Append("&type=" + type.GetStringAttribute(","));
builder.Append("&limit=" + limit);
builder.Append("&offset=" + offset);
if (!String.IsNullOrEmpty(market))
builder.Append("&market=" + market);
return DownloadData<SearchItem>(builder.ToString());
return DownloadData<SearchItem>(_builder.SearchItems(q, type, limit, offset, market));
}
/// <summary>
/// Get Spotify catalog information about artists, albums, tracks or playlists that match a keyword string asynchronously.
/// </summary>
/// <param name="q">The search query's keywords (and optional field filters and operators), for example q=roadhouse+blues.</param>
/// <param name="type">A list of item types to search across.</param>
/// <param name="limit">The maximum number of items to return. Default: 20. Minimum: 1. Maximum: 50.</param>
/// <param name="offset">The index of the first result to return. Default: 0</param>
/// <param name="market">An ISO 3166-1 alpha-2 country code or the string from_token.</param>
/// <returns></returns>
public Task<SearchItem> SearchItemsAsync(String q, SearchType type, int limit = 20, int offset = 0, String market = "")
{
return DownloadDataAsync<SearchItem>(_builder.SearchItems(q, type, limit, offset, market));
}
#endregion Search
@ -860,6 +871,15 @@ namespace SpotifyAPI.Web
return WebClient.UploadJson<T>(url, uploadData, method);
}
public Task<T> UploadDataAsync<T>(String url, String uploadData, String method = "POST")
{
if (!UseAuth)
throw new InvalidOperationException("Auth is required for all Upload-Actions");
WebClient.SetHeader("Authorization", TokenType + " " + AccessToken);
WebClient.SetHeader("Content-Type", "application/json");
return WebClient.UploadJsonAsync<T>(url, uploadData, method);
}
public T DownloadData<T>(String url)
{
if (UseAuth)
@ -869,6 +889,15 @@ namespace SpotifyAPI.Web
return WebClient.DownloadJson<T>(url);
}
public Task<T> DownloadDataAsync<T>(String url)
{
if (UseAuth)
WebClient.SetHeader("Authorization", TokenType + " " + AccessToken);
else
WebClient.RemoveHeader("Authorization");
return WebClient.DownloadJsonAsync<T>(url);
}
#endregion Util
}
}

View File

@ -5,6 +5,7 @@ using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
namespace SpotifyAPI.Web
{
@ -46,17 +47,45 @@ namespace SpotifyAPI.Web
return response;
}
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);
}
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);
}
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;
@ -75,17 +104,46 @@ namespace SpotifyAPI.Web
return response;
}
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));
}
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);
}
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;