Skip to content

Commit 0dd94ea

Browse files
author
Johnny @PC
committed
Implemented Async-Structure.
1 parent 96bcb7b commit 0dd94ea

4 files changed

Lines changed: 149 additions & 10 deletions

File tree

‎SpotifyAPI/SpotifyAPI.csproj‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@
102102
<Compile Include="Web\Enums\SearchType.cs" />
103103
<Compile Include="Web\SimpleHttpServer.cs" />
104104
<Compile Include="Web\SpotifyWebAPI.cs" />
105+
<Compile Include="Web\SpotifyWebBuilder.cs" />
105106
<Compile Include="Web\Util.cs" />
106107
<Compile Include="Web\SpotifyWebClient.cs" />
107108
</ItemGroup>

‎SpotifyAPI/Web/IClient.cs‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using Newtonsoft.Json;
22
using System;
33
using System.Collections.Generic;
4+
using System.Threading.Tasks;
45

56
namespace SpotifyAPI.Web
67
{
@@ -15,13 +16,27 @@ public interface IClient : IDisposable
1516
/// <returns></returns>
1617
string Download(string url);
1718

19+
/// <summary>
20+
/// Downloads data async from an URL and returns it
21+
/// </summary>
22+
/// <param name="url"></param>
23+
/// <returns></returns>
24+
Task<string> DownloadAsync(string url);
25+
1826
/// <summary>
1927
/// Downloads data from an URL and returns it
2028
/// </summary>
2129
/// <param name="url">An URL</param>
2230
/// <returns></returns>
2331
byte[] DownloadRaw(string url);
2432

33+
/// <summary>
34+
/// Downloads data async from an URL and returns it
35+
/// </summary>
36+
/// <param name="url"></param>
37+
/// <returns></returns>
38+
Task<byte[]> DownloadRawAsync(string url);
39+
2540
/// <summary>
2641
/// Downloads data from an URL and converts it to an object
2742
/// </summary>
@@ -30,6 +45,14 @@ public interface IClient : IDisposable
3045
/// <returns></returns>
3146
T DownloadJson<T>(string url);
3247

48+
/// <summary>
49+
/// Downloads data async from an URL and converts it to an object
50+
/// </summary>
51+
/// <typeparam name="T">The Type which the object gets converted to</typeparam>
52+
/// <param name="url">An URL</param>
53+
/// <returns></returns>
54+
Task<T> DownloadJsonAsync<T>(string url);
55+
3356
/// <summary>
3457
/// Uploads data from an URL and returns the response
3558
/// </summary>
@@ -39,6 +62,15 @@ public interface IClient : IDisposable
3962
/// <returns></returns>
4063
string Upload(string url, string body, string method);
4164

65+
/// <summary>
66+
/// Uploads data async from an URL and returns the response
67+
/// </summary>
68+
/// <param name="url">An URL</param>
69+
/// <param name="body">The Body-Data (most likely a JSON String)</param>
70+
/// <param name="method">The Upload-method (POST,DELETE,PUT)</param>
71+
/// <returns></returns>
72+
Task<string> UploadAsync(string url, string body, string method);
73+
4274
/// <summary>
4375
/// Uploads data from an URL and returns the response
4476
/// </summary>
@@ -48,6 +80,15 @@ public interface IClient : IDisposable
4880
/// <returns></returns>
4981
byte[] UploadRaw(string url, string body, string method);
5082

83+
/// <summary>
84+
/// Uploads data async from an URL and returns the response
85+
/// </summary>
86+
/// <param name="url">An URL</param>
87+
/// <param name="body">The Body-Data (most likely a JSON String)</param>
88+
/// <param name="method">The Upload-method (POST,DELETE,PUT)</param>
89+
/// <returns></returns>
90+
Task<byte[]> UploadRawAsync(string url, string body, string method);
91+
5192
/// <summary>
5293
/// Uploads data from an URL and converts the response to an object
5394
/// </summary>
@@ -58,6 +99,16 @@ public interface IClient : IDisposable
5899
/// <returns></returns>
59100
T UploadJson<T>(string url, string body, string method);
60101

102+
/// <summary>
103+
/// Uploads data async from an URL and converts the response to an object
104+
/// </summary>
105+
/// <typeparam name="T">The Type which the object gets converted to</typeparam>
106+
/// <param name="url">An URL</param>
107+
/// <param name="body">The Body-Data (most likely a JSON String)</param>
108+
/// <param name="method">The Upload-method (POST,DELETE,PUT)</param>
109+
/// <returns></returns>
110+
Task<T> UploadJsonAsync<T>(string url, string body, string method);
111+
61112
/// <summary>
62113
/// Sets a specific Header
63114
/// </summary>

‎SpotifyAPI/Web/SpotifyWebAPI.cs‎

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,20 @@
66
using System.Collections.Generic;
77
using System.Linq;
88
using System.Text;
9+
using System.Threading.Tasks;
910

1011
namespace SpotifyAPI.Web
1112
{
1213
public sealed class SpotifyWebAPI : IDisposable
1314
{
14-
public const String APIBase = "https://api.spotify.com/v1";
15+
[Obsolete("This Property will be removed soon. Please use SpotifyWebBuilder.APIBase")]
16+
public const String APIBase = SpotifyWebBuilder.APIBase;
17+
18+
private readonly SpotifyWebBuilder _builder;
1519

1620
public SpotifyWebAPI()
1721
{
22+
_builder = new SpotifyWebBuilder();
1823
UseAuth = true;
1924
WebClient = new SpotifyWebClient
2025
{
@@ -51,15 +56,21 @@ public void Dispose()
5156
/// <returns></returns>
5257
public SearchItem SearchItems(String q, SearchType type, int limit = 20, int offset = 0, String market = "")
5358
{
54-
limit = Math.Min(50, limit);
55-
StringBuilder builder = new StringBuilder(APIBase + "/search");
56-
builder.Append("?q=" + q);
57-
builder.Append("&type=" + type.GetStringAttribute(","));
58-
builder.Append("&limit=" + limit);
59-
builder.Append("&offset=" + offset);
60-
if (!String.IsNullOrEmpty(market))
61-
builder.Append("&market=" + market);
62-
return DownloadData<SearchItem>(builder.ToString());
59+
return DownloadData<SearchItem>(_builder.SearchItems(q, type, limit, offset, market));
60+
}
61+
62+
/// <summary>
63+
/// Get Spotify catalog information about artists, albums, tracks or playlists that match a keyword string asynchronously.
64+
/// </summary>
65+
/// <param name="q">The search query's keywords (and optional field filters and operators), for example q=roadhouse+blues.</param>
66+
/// <param name="type">A list of item types to search across.</param>
67+
/// <param name="limit">The maximum number of items to return. Default: 20. Minimum: 1. Maximum: 50.</param>
68+
/// <param name="offset">The index of the first result to return. Default: 0</param>
69+
/// <param name="market">An ISO 3166-1 alpha-2 country code or the string from_token.</param>
70+
/// <returns></returns>
71+
public Task<SearchItem> SearchItemsAsync(String q, SearchType type, int limit = 20, int offset = 0, String market = "")
72+
{
73+
return DownloadDataAsync<SearchItem>(_builder.SearchItems(q, type, limit, offset, market));
6374
}
6475

6576
#endregion Search
@@ -860,6 +871,15 @@ public T UploadData<T>(String url, String uploadData, String method = "POST")
860871
return WebClient.UploadJson<T>(url, uploadData, method);
861872
}
862873

874+
public Task<T> UploadDataAsync<T>(String url, String uploadData, String method = "POST")
875+
{
876+
if (!UseAuth)
877+
throw new InvalidOperationException("Auth is required for all Upload-Actions");
878+
WebClient.SetHeader("Authorization", TokenType + " " + AccessToken);
879+
WebClient.SetHeader("Content-Type", "application/json");
880+
return WebClient.UploadJsonAsync<T>(url, uploadData, method);
881+
}
882+
863883
public T DownloadData<T>(String url)
864884
{
865885
if (UseAuth)
@@ -869,6 +889,15 @@ public T DownloadData<T>(String url)
869889
return WebClient.DownloadJson<T>(url);
870890
}
871891

892+
public Task<T> DownloadDataAsync<T>(String url)
893+
{
894+
if (UseAuth)
895+
WebClient.SetHeader("Authorization", TokenType + " " + AccessToken);
896+
else
897+
WebClient.RemoveHeader("Authorization");
898+
return WebClient.DownloadJsonAsync<T>(url);
899+
}
900+
872901
#endregion Util
873902
}
874903
}

‎SpotifyAPI/Web/SpotifyWebClient.cs‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Linq;
66
using System.Net;
77
using System.Text;
8+
using System.Threading.Tasks;
89

910
namespace SpotifyAPI.Web
1011
{
@@ -46,17 +47,45 @@ public string Download(string url)
4647
return response;
4748
}
4849

50+
public async Task<string> DownloadAsync(string url)
51+
{
52+
String response;
53+
try
54+
{
55+
response = _encoding.GetString(await DownloadRawAsync(url));
56+
}
57+
catch (WebException e)
58+
{
59+
using (StreamReader reader = new StreamReader(e.Response.GetResponseStream()))
60+
{
61+
response = reader.ReadToEnd();
62+
}
63+
}
64+
return response;
65+
}
66+
4967
public byte[] DownloadRaw(string url)
5068
{
5169
return _webClient.DownloadData(url);
5270
}
5371

72+
public async Task<byte[]> DownloadRawAsync(string url)
73+
{
74+
return await _webClient.DownloadDataTaskAsync(url);
75+
}
76+
5477
public T DownloadJson<T>(string url)
5578
{
5679
String response = Download(url);
5780
return JsonConvert.DeserializeObject<T>(response, JsonSettings);
5881
}
5982

83+
public async Task<T> DownloadJsonAsync<T>(string url)
84+
{
85+
String response = await DownloadAsync(url);
86+
return JsonConvert.DeserializeObject<T>(response, JsonSettings);
87+
}
88+
6089
public string Upload(string url, string body, string method)
6190
{
6291
String response;
@@ -75,17 +104,46 @@ public string Upload(string url, string body, string method)
75104
return response;
76105
}
77106

107+
public async Task<string> UploadAsync(string url, string body, string method)
108+
{
109+
String response;
110+
try
111+
{
112+
byte[] data = await UploadRawAsync(url, body, method);
113+
response = _encoding.GetString(data);
114+
}
115+
catch (WebException e)
116+
{
117+
using (StreamReader reader = new StreamReader(e.Response.GetResponseStream()))
118+
{
119+
response = reader.ReadToEnd();
120+
}
121+
}
122+
return response;
123+
}
124+
78125
public byte[] UploadRaw(string url, string body, string method)
79126
{
80127
return _webClient.UploadData(url, method, _encoding.GetBytes(body));
81128
}
82129

130+
public async Task<byte[]> UploadRawAsync(string url, string body, string method)
131+
{
132+
return await _webClient.UploadDataTaskAsync(url, method, _encoding.GetBytes(body));
133+
}
134+
83135
public T UploadJson<T>(string url, string body, string method)
84136
{
85137
String response = Upload(url, body, method);
86138
return JsonConvert.DeserializeObject<T>(response, JsonSettings);
87139
}
88140

141+
public async Task<T> UploadJsonAsync<T>(string url, string body, string method)
142+
{
143+
String response = await UploadAsync(url, body, method);
144+
return JsonConvert.DeserializeObject<T>(response, JsonSettings);
145+
}
146+
89147
public void SetHeader(string header, string value)
90148
{
91149
_webClient.Headers[header] = value;

0 commit comments

Comments
 (0)