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\Enums\SearchType.cs" />
<Compile Include="Web\SimpleHttpServer.cs" /> <Compile Include="Web\SimpleHttpServer.cs" />
<Compile Include="Web\SpotifyWebAPI.cs" /> <Compile Include="Web\SpotifyWebAPI.cs" />
<Compile Include="Web\SpotifyWebBuilder.cs" />
<Compile Include="Web\Util.cs" /> <Compile Include="Web\Util.cs" />
<Compile Include="Web\SpotifyWebClient.cs" /> <Compile Include="Web\SpotifyWebClient.cs" />
</ItemGroup> </ItemGroup>

View File

@ -1,6 +1,7 @@
using Newtonsoft.Json; using Newtonsoft.Json;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Threading.Tasks;
namespace SpotifyAPI.Web namespace SpotifyAPI.Web
{ {
@ -15,6 +16,13 @@ namespace SpotifyAPI.Web
/// <returns></returns> /// <returns></returns>
string Download(string url); 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> /// <summary>
/// Downloads data from an URL and returns it /// Downloads data from an URL and returns it
/// </summary> /// </summary>
@ -22,6 +30,13 @@ namespace SpotifyAPI.Web
/// <returns></returns> /// <returns></returns>
byte[] DownloadRaw(string url); 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> /// <summary>
/// Downloads data from an URL and converts it to an object /// Downloads data from an URL and converts it to an object
/// </summary> /// </summary>
@ -30,6 +45,14 @@ namespace SpotifyAPI.Web
/// <returns></returns> /// <returns></returns>
T DownloadJson<T>(string url); 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> /// <summary>
/// Uploads data from an URL and returns the response /// Uploads data from an URL and returns the response
/// </summary> /// </summary>
@ -39,6 +62,15 @@ namespace SpotifyAPI.Web
/// <returns></returns> /// <returns></returns>
string Upload(string url, string body, string method); 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> /// <summary>
/// Uploads data from an URL and returns the response /// Uploads data from an URL and returns the response
/// </summary> /// </summary>
@ -48,6 +80,15 @@ namespace SpotifyAPI.Web
/// <returns></returns> /// <returns></returns>
byte[] UploadRaw(string url, string body, string method); 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> /// <summary>
/// Uploads data from an URL and converts the response to an object /// Uploads data from an URL and converts the response to an object
/// </summary> /// </summary>
@ -58,6 +99,16 @@ namespace SpotifyAPI.Web
/// <returns></returns> /// <returns></returns>
T UploadJson<T>(string url, string body, string method); 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> /// <summary>
/// Sets a specific Header /// Sets a specific Header
/// </summary> /// </summary>

View File

@ -6,15 +6,20 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks;
namespace SpotifyAPI.Web namespace SpotifyAPI.Web
{ {
public sealed class SpotifyWebAPI : IDisposable 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() public SpotifyWebAPI()
{ {
_builder = new SpotifyWebBuilder();
UseAuth = true; UseAuth = true;
WebClient = new SpotifyWebClient WebClient = new SpotifyWebClient
{ {
@ -51,15 +56,21 @@ namespace SpotifyAPI.Web
/// <returns></returns> /// <returns></returns>
public SearchItem SearchItems(String q, SearchType type, int limit = 20, int offset = 0, String market = "") public SearchItem SearchItems(String q, SearchType type, int limit = 20, int offset = 0, String market = "")
{ {
limit = Math.Min(50, limit); return DownloadData<SearchItem>(_builder.SearchItems(q, type, limit, offset, market));
StringBuilder builder = new StringBuilder(APIBase + "/search"); }
builder.Append("?q=" + q);
builder.Append("&type=" + type.GetStringAttribute(",")); /// <summary>
builder.Append("&limit=" + limit); /// Get Spotify catalog information about artists, albums, tracks or playlists that match a keyword string asynchronously.
builder.Append("&offset=" + offset); /// </summary>
if (!String.IsNullOrEmpty(market)) /// <param name="q">The search query's keywords (and optional field filters and operators), for example q=roadhouse+blues.</param>
builder.Append("&market=" + market); /// <param name="type">A list of item types to search across.</param>
return DownloadData<SearchItem>(builder.ToString()); /// <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 #endregion Search
@ -860,6 +871,15 @@ namespace SpotifyAPI.Web
return WebClient.UploadJson<T>(url, uploadData, method); 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) public T DownloadData<T>(String url)
{ {
if (UseAuth) if (UseAuth)
@ -869,6 +889,15 @@ namespace SpotifyAPI.Web
return WebClient.DownloadJson<T>(url); 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 #endregion Util
} }
} }

View File

@ -5,6 +5,7 @@ using System.IO;
using System.Linq; using System.Linq;
using System.Net; using System.Net;
using System.Text; using System.Text;
using System.Threading.Tasks;
namespace SpotifyAPI.Web namespace SpotifyAPI.Web
{ {
@ -46,17 +47,45 @@ namespace SpotifyAPI.Web
return response; 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) public byte[] DownloadRaw(string url)
{ {
return _webClient.DownloadData(url); return _webClient.DownloadData(url);
} }
public async Task<byte[]> DownloadRawAsync(string url)
{
return await _webClient.DownloadDataTaskAsync(url);
}
public T DownloadJson<T>(string url) public T DownloadJson<T>(string url)
{ {
String response = Download(url); String response = Download(url);
return JsonConvert.DeserializeObject<T>(response, JsonSettings); 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) public string Upload(string url, string body, string method)
{ {
String response; String response;
@ -75,17 +104,46 @@ namespace SpotifyAPI.Web
return response; 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) public byte[] UploadRaw(string url, string body, string method)
{ {
return _webClient.UploadData(url, method, _encoding.GetBytes(body)); 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) public T UploadJson<T>(string url, string body, string method)
{ {
String response = Upload(url, body, method); String response = Upload(url, body, method);
return JsonConvert.DeserializeObject<T>(response, JsonSettings); 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) public void SetHeader(string header, string value)
{ {
_webClient.Headers[header] = value; _webClient.Headers[header] = value;