diff --git a/SpotifyAPI/SpotifyAPI.csproj b/SpotifyAPI/SpotifyAPI.csproj
index faaaafb5..512ee587 100644
--- a/SpotifyAPI/SpotifyAPI.csproj
+++ b/SpotifyAPI/SpotifyAPI.csproj
@@ -102,6 +102,7 @@
+
diff --git a/SpotifyAPI/Web/IClient.cs b/SpotifyAPI/Web/IClient.cs
index 1ab1ae36..fd198287 100644
--- a/SpotifyAPI/Web/IClient.cs
+++ b/SpotifyAPI/Web/IClient.cs
@@ -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
///
string Download(string url);
+ ///
+ /// Downloads data async from an URL and returns it
+ ///
+ ///
+ ///
+ Task DownloadAsync(string url);
+
///
/// Downloads data from an URL and returns it
///
@@ -22,6 +30,13 @@ namespace SpotifyAPI.Web
///
byte[] DownloadRaw(string url);
+ ///
+ /// Downloads data async from an URL and returns it
+ ///
+ ///
+ ///
+ Task DownloadRawAsync(string url);
+
///
/// Downloads data from an URL and converts it to an object
///
@@ -30,6 +45,14 @@ namespace SpotifyAPI.Web
///
T DownloadJson(string url);
+ ///
+ /// Downloads data async from an URL and converts it to an object
+ ///
+ /// The Type which the object gets converted to
+ /// An URL
+ ///
+ Task DownloadJsonAsync(string url);
+
///
/// Uploads data from an URL and returns the response
///
@@ -39,6 +62,15 @@ namespace SpotifyAPI.Web
///
string Upload(string url, string body, string method);
+ ///
+ /// Uploads data async from an URL and returns the response
+ ///
+ /// An URL
+ /// The Body-Data (most likely a JSON String)
+ /// The Upload-method (POST,DELETE,PUT)
+ ///
+ Task UploadAsync(string url, string body, string method);
+
///
/// Uploads data from an URL and returns the response
///
@@ -48,6 +80,15 @@ namespace SpotifyAPI.Web
///
byte[] UploadRaw(string url, string body, string method);
+ ///
+ /// Uploads data async from an URL and returns the response
+ ///
+ /// An URL
+ /// The Body-Data (most likely a JSON String)
+ /// The Upload-method (POST,DELETE,PUT)
+ ///
+ Task UploadRawAsync(string url, string body, string method);
+
///
/// Uploads data from an URL and converts the response to an object
///
@@ -58,6 +99,16 @@ namespace SpotifyAPI.Web
///
T UploadJson(string url, string body, string method);
+ ///
+ /// Uploads data async from an URL and converts the response to an object
+ ///
+ /// The Type which the object gets converted to
+ /// An URL
+ /// The Body-Data (most likely a JSON String)
+ /// The Upload-method (POST,DELETE,PUT)
+ ///
+ Task UploadJsonAsync(string url, string body, string method);
+
///
/// Sets a specific Header
///
diff --git a/SpotifyAPI/Web/SpotifyWebAPI.cs b/SpotifyAPI/Web/SpotifyWebAPI.cs
index 9d9f3168..2960d665 100644
--- a/SpotifyAPI/Web/SpotifyWebAPI.cs
+++ b/SpotifyAPI/Web/SpotifyWebAPI.cs
@@ -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
///
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(builder.ToString());
+ return DownloadData(_builder.SearchItems(q, type, limit, offset, market));
+ }
+
+ ///
+ /// Get Spotify catalog information about artists, albums, tracks or playlists that match a keyword string asynchronously.
+ ///
+ /// The search query's keywords (and optional field filters and operators), for example q=roadhouse+blues.
+ /// A list of item types to search across.
+ /// The maximum number of items to return. Default: 20. Minimum: 1. Maximum: 50.
+ /// The index of the first result to return. Default: 0
+ /// An ISO 3166-1 alpha-2 country code or the string from_token.
+ ///
+ public Task SearchItemsAsync(String q, SearchType type, int limit = 20, int offset = 0, String market = "")
+ {
+ return DownloadDataAsync(_builder.SearchItems(q, type, limit, offset, market));
}
#endregion Search
@@ -860,6 +871,15 @@ namespace SpotifyAPI.Web
return WebClient.UploadJson(url, uploadData, method);
}
+ public Task UploadDataAsync(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(url, uploadData, method);
+ }
+
public T DownloadData(String url)
{
if (UseAuth)
@@ -869,6 +889,15 @@ namespace SpotifyAPI.Web
return WebClient.DownloadJson(url);
}
+ public Task DownloadDataAsync(String url)
+ {
+ if (UseAuth)
+ WebClient.SetHeader("Authorization", TokenType + " " + AccessToken);
+ else
+ WebClient.RemoveHeader("Authorization");
+ return WebClient.DownloadJsonAsync(url);
+ }
+
#endregion Util
}
}
\ No newline at end of file
diff --git a/SpotifyAPI/Web/SpotifyWebClient.cs b/SpotifyAPI/Web/SpotifyWebClient.cs
index ea9769d7..40533088 100644
--- a/SpotifyAPI/Web/SpotifyWebClient.cs
+++ b/SpotifyAPI/Web/SpotifyWebClient.cs
@@ -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 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 DownloadRawAsync(string url)
+ {
+ return await _webClient.DownloadDataTaskAsync(url);
+ }
+
public T DownloadJson(string url)
{
String response = Download(url);
return JsonConvert.DeserializeObject(response, JsonSettings);
}
+ public async Task DownloadJsonAsync(string url)
+ {
+ String response = await DownloadAsync(url);
+ return JsonConvert.DeserializeObject(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 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 UploadRawAsync(string url, string body, string method)
+ {
+ return await _webClient.UploadDataTaskAsync(url, method, _encoding.GetBytes(body));
+ }
+
public T UploadJson(string url, string body, string method)
{
String response = Upload(url, body, method);
return JsonConvert.DeserializeObject(response, JsonSettings);
}
+ public async Task UploadJsonAsync(string url, string body, string method)
+ {
+ String response = await UploadAsync(url, body, method);
+ return JsonConvert.DeserializeObject(response, JsonSettings);
+ }
+
public void SetHeader(string header, string value)
{
_webClient.Headers[header] = value;