Updated to support latest features (Changelog-date: February 11th, 2015)

This commit is contained in:
Johnny @PC 2015-02-13 00:23:07 +01:00
parent 9d01d3a964
commit 3afbea32a6
16 changed files with 262 additions and 58 deletions

View File

@ -63,8 +63,13 @@
<Compile Include="SpotifyWebAPI\AlbumType.cs" />
<Compile Include="SpotifyWebAPI\AutorizationCodeAuth.cs" />
<Compile Include="SpotifyWebAPI\ClientCredentialsAuth.cs" />
<Compile Include="SpotifyWebAPI\FollowType.cs" />
<Compile Include="SpotifyWebAPI\ImplicitGrantAuth.cs" />
<Compile Include="SpotifyWebAPI\Models\ArrayResponse.cs" />
<Compile Include="SpotifyWebAPI\Models\BasicModel.cs" />
<Compile Include="SpotifyWebAPI\Models\Category.cs" />
<Compile Include="SpotifyWebAPI\Models\CategoryList.cs" />
<Compile Include="SpotifyWebAPI\Models\CategoryPlaylist.cs" />
<Compile Include="SpotifyWebAPI\Models\FeaturedPlaylists.cs" />
<Compile Include="SpotifyWebAPI\Models\FullAlbum.cs" />
<Compile Include="SpotifyWebAPI\Models\FullArtist.cs" />

View File

@ -67,7 +67,7 @@ namespace SpotifyAPI.SpotifyWebAPI
builder.Append("&response_type=code");
builder.Append("&redirect_uri=" + RedirectUri);
builder.Append("&state=" + State);
builder.Append("&scope=" + Scope.GetScopeValue(" "));
builder.Append("&scope=" + Scope.GetStringAttribute(" "));
builder.Append("&show_dialog=" + ShowDialog.ToString());
return builder.ToString();
}

View File

@ -26,7 +26,7 @@ namespace SpotifyAPI.SpotifyWebAPI
NameValueCollection col = new NameValueCollection();
col.Add("grant_type","client_credentials");
col.Add("scope", Scope.GetScopeValue(" "));
col.Add("scope", Scope.GetStringAttribute(" "));
byte[] data = null;
try

View File

@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SpotifyAPI.SpotifyWebAPI
{
[Flags]
public enum FollowType
{
[StringAttribute("artist")]
ARTIST = 1,
[StringAttribute("user")]
USER = 2
}
}

View File

@ -36,7 +36,7 @@ namespace SpotifyAPI.SpotifyWebAPI
builder.Append("&response_type=token");
builder.Append("&redirect_uri=" + RedirectUri);
builder.Append("&state=" + State);
builder.Append("&scope=" + Scope.GetScopeValue(" "));
builder.Append("&scope=" + Scope.GetStringAttribute(" "));
builder.Append("&show_dialog=" + ShowDialog.ToString());
return builder.ToString();
}

View File

@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
namespace SpotifyAPI.SpotifyWebAPI.Models
{
public class ListResponse<T> : BasicModel
{
public List<T> List { get; set; }
}
}

View File

@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
namespace SpotifyAPI.SpotifyWebAPI.Models
{
public class Category : BasicModel
{
[JsonProperty("href")]
public String Href { get; set; }
[JsonProperty("icons")]
public List<Image> Icons { get; set; }
[JsonProperty("id")]
public String Id { get; set; }
[JsonProperty("name")]
public String Name { get; set; }
}
}

View File

@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
namespace SpotifyAPI.SpotifyWebAPI.Models
{
public class CategoryList : BasicModel
{
[JsonProperty("categories")]
public Paging<Category> Categories { get; set; }
}
}

View File

@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
namespace SpotifyAPI.SpotifyWebAPI.Models
{
public class CategoryPlaylist : BasicModel
{
[JsonProperty("playlists")]
public Paging<SimplePlaylist> Playlists { get; set; }
}
}

View File

@ -44,5 +44,17 @@ namespace SpotifyAPI.SpotifyWebAPI.Models
public String Type { get; set; }
[JsonProperty("uri")]
public String Uri { get; set; }
/// <summary>
/// Only filled when the "market"-parameter was supplied!
/// </summary>
[JsonProperty("is_playable")]
public Boolean? IsPlayable { get; set; }
/// <summary>
/// Only filled when the "market"-parameter was supplied!
/// </summary>
[JsonProperty("linked_from")]
public LinkedFrom LinkedFrom { get; set; }
}
}

View File

@ -100,8 +100,17 @@ namespace SpotifyAPI.SpotifyWebAPI.Models
public String Type { get; set; }
}
public class CheckUserTracks : BasicModel
public class LinkedFrom
{
public List<Boolean> Checked { get; set; }
[JsonProperty("external_urls")]
public Dictionary<String, String> ExternalUrls { get; set; }
[JsonProperty("href")]
public String Href { get; set; }
[JsonProperty("id")]
public String Id { get; set; }
[JsonProperty("type")]
public String Type { get; set; }
[JsonProperty("uri")]
public String Uri { get; set; }
}
}

View File

@ -9,6 +9,8 @@ namespace SpotifyAPI.SpotifyWebAPI.Models
{
public class PrivateProfile : BasicModel
{
[JsonProperty("birthdate")]
public String Birthdate { get; set; }
[JsonProperty("country")]
public String Country { get; set; }
[JsonProperty("display_name")]

View File

@ -26,6 +26,12 @@ namespace SpotifyAPI.SpotifyWebAPI
[StringAttribute("user-library-read")]
USER_LIBRARAY_READ = 128,
[StringAttribute("user-library-modify")]
USER_LIBRARY_MODIFY = 256
USER_LIBRARY_MODIFY = 256,
[StringAttribute("user-follow-modify")]
USER_FOLLOW_MODIFY = 512,
[StringAttribute("user-follow-read")]
USER_FOLLOW_READ = 1024,
[StringAttribute("user-read-birthdate")]
USER_READ_BIRTHDATE = 2048
}
}

View File

@ -46,6 +46,36 @@ namespace SpotifyAPI.SpotifyWebAPI
return DownloadData<PublicProfile>("https://api.spotify.com/v1/users/" + userId);
}
public ErrorResponse Follow(FollowType followType, List<String> ids)
{
JObject ob = new JObject();
ob.Add("ids", new JArray(ids.ToArray()));
return UploadData<ErrorResponse>("https://api.spotify.com/v1/me/following?type=" + followType.GetStringAttribute(""), ob.ToString(Formatting.None), "PUT");
}
public ErrorResponse Unfollow(FollowType followType, List<String> ids)
{
JObject ob = new JObject();
ob.Add("ids", new JArray(ids.ToArray()));
return UploadData<ErrorResponse>("https://api.spotify.com/v1/me/following?type=" + followType.GetStringAttribute(""), ob.ToString(Formatting.None), "DELETE");
}
/// <summary>
///
/// </summary>
public ListResponse<Boolean> IsFollowing(FollowType followType, List<String> ids)
{
JToken res = DownloadData<JToken>("https://api.spotify.com/v1/me/following/contains?type=" + followType.GetStringAttribute("") + "&ids=" + string.Join(",", ids));
if (res is JArray)
{
return new ListResponse<Boolean> { List = res.ToObject<List<Boolean>>(), ErrorResponse = null };
}
else
{
return new ListResponse<Boolean> { List = null, ErrorResponse = res.ToObject<Error>() };
}
}
#endregion
#region User-Library
@ -54,26 +84,29 @@ namespace SpotifyAPI.SpotifyWebAPI
JArray array = new JArray(ids.ToArray());
return UploadData<ErrorResponse>("https://api.spotify.com/v1/me/tracks/", array.ToString(Formatting.None), "PUT");
}
public Paging<FullTrack> GetSavedTracks()
public Paging<FullTrack> GetSavedTracks(String market = "")
{
if(market == "")
return DownloadData<Paging<FullTrack>>("https://api.spotify.com/v1/me/tracks");
else
return DownloadData<Paging<FullTrack>>("https://api.spotify.com/v1/me/tracks?market=" + market);
}
public ErrorResponse RemoveSavedTracks(List<String> ids)
{
JArray array = new JArray(ids.ToArray());
return UploadData<ErrorResponse>("https://api.spotify.com/v1/me/tracks/", array.ToString(Formatting.None), "DELETE");
}
public CheckUserTracks CheckSavedTracks(List<String> ids)
public ListResponse<Boolean> CheckSavedTracks(List<String> ids)
{
String resp = DownloadString("https://api.spotify.com/v1/me/tracks/contains?ids=" + string.Join(",", ids));
JToken res = JToken.Parse(resp);
if (res is JArray)
{
return new CheckUserTracks { Checked = res.ToObject<List<Boolean>>(), ErrorResponse = null };
return new ListResponse<Boolean> { List = res.ToObject<List<Boolean>>(), ErrorResponse = null };
}
else
{
return new CheckUserTracks { Checked = null, ErrorResponse = res.ToObject<Error>() };
return new ListResponse<Boolean> { List = null, ErrorResponse = res.ToObject<Error>() };
}
}
#endregion
@ -87,9 +120,12 @@ namespace SpotifyAPI.SpotifyWebAPI
{
return DownloadData<FullPlaylist>("https://api.spotify.com/v1/users/" + userId + "/playlists/" + playlistId);
}
public Paging<PlaylistTrack> GetPlaylistTracks(String userId, String playlistId)
public Paging<PlaylistTrack> GetPlaylistTracks(String userId, String playlistId, String market = "")
{
if(market == "")
return DownloadData<Paging<PlaylistTrack>>("https://api.spotify.com/v1/users/" + userId + "/playlists/" + playlistId + "/tracks");
else
return DownloadData<Paging<PlaylistTrack>>("https://api.spotify.com/v1/users/" + userId + "/playlists/" + playlistId + "/tracks?market=" + market);
}
public FullPlaylist CreatePlaylist(String userId, String playlistName, Boolean isPublic = true)
{
@ -147,6 +183,28 @@ namespace SpotifyAPI.SpotifyWebAPI
builder.Append("&timestamp=" + timestamp.ToString("yyyy-MM-ddTHH:mm:ss"));
return DownloadData<FeaturedPlaylists>(builder.ToString());
}
public ErrorResponse FollowPlaylist(String ownerId, String playlistId, bool showPublic)
{
JObject ob = new JObject();
ob.Add("public", showPublic);
return UploadData<ErrorResponse>("https://api.spotify.com/v1/users/" + ownerId + "/playlists/" + playlistId + "/followers", ob.ToString(Formatting.None), "PUT");
}
public ErrorResponse UnfollowPlaylist(String ownerId, String playlistId)
{
return UploadData<ErrorResponse>("https://api.spotify.com/v1/users/" + ownerId + "/playlists/" + playlistId + "/followers", "", "DELETE");
}
public ListResponse<Boolean> IsFollowingPlaylist(String ownerId, String playlistId, List<String> ids)
{
JToken res = DownloadData<JToken>("https://api.spotify.com/v1/users/" + ownerId + "/playlists/" + playlistId + "/followers/contains?ids=" + string.Join(",", ids));
if (res is JArray)
return new ListResponse<Boolean>() { List = res.ToObject<List<Boolean>>(), ErrorResponse = null };
else
return new ListResponse<Boolean>() { List = null, ErrorResponse = res.ToObject<Error>() };
}
#endregion
#region Search and Fetch
@ -166,27 +224,36 @@ namespace SpotifyAPI.SpotifyWebAPI
limit = Math.Min(50, limit);
StringBuilder builder = new StringBuilder("https://api.spotify.com/v1/search");
builder.Append("?q=" + q);
builder.Append("&type=" + type.GetSearchValue(","));
builder.Append("&type=" + type.GetStringAttribute(","));
builder.Append("&limit=" + limit);
builder.Append("&offset=" + offset);
return DownloadData<SearchItem>(builder.ToString());
}
public SeveralTracks GetSeveralTracks(List<String> ids)
public SeveralTracks GetSeveralTracks(List<String> ids, String market = "")
{
if(market == "")
return DownloadData<SeveralTracks>("https://api.spotify.com/v1/tracks?ids=" + string.Join(",", ids));
else
return DownloadData<SeveralTracks>("https://api.spotify.com/v1/tracks?market=" + market + "&ids=" + string.Join(",", ids));
}
public SeveralAlbums GetSeveralAlbums(List<String> ids)
public SeveralAlbums GetSeveralAlbums(List<String> ids, String market = "")
{
if(market == "")
return DownloadData<SeveralAlbums>("https://api.spotify.com/v1/albums?ids=" + string.Join(",", ids));
else
return DownloadData<SeveralAlbums>("https://api.spotify.com/v1/albums?market=" + market + "&ids=" + string.Join(",", ids));
}
public SeveralArtists GetSeveralArtists(List<String> ids)
{
return DownloadData<SeveralArtists>("https://api.spotify.com/v1/artists?ids=" + string.Join(",", ids));
}
public FullTrack GetTrack(String id)
public FullTrack GetTrack(String id, String market = "")
{
if(market == "")
return DownloadData<FullTrack>("https://api.spotify.com/v1/tracks/" + id);
else
return DownloadData<FullTrack>("https://api.spotify.com/v1/tracks/" + id + "?market=" + market);
}
public SeveralArtists GetRelatedArtists(String id)
{
@ -200,7 +267,7 @@ namespace SpotifyAPI.SpotifyWebAPI
{
limit = Math.Min(50, limit);
StringBuilder builder = new StringBuilder("https://api.spotify.com/v1/artists/" + id + "/albums");
builder.Append("?type=" + type.GetAlbumValue(","));
builder.Append("?type=" + type.GetStringAttribute(","));
builder.Append("&limit=" + limit);
builder.Append("&offset=" + offset);
if (market != "")
@ -211,20 +278,68 @@ namespace SpotifyAPI.SpotifyWebAPI
{
return DownloadData<FullArtist>("https://api.spotify.com/v1/artists/" + id);
}
public Paging<SimpleTrack> GetAlbumTracks(String id, int limit = 20, int offset = 0)
public Paging<SimpleTrack> GetAlbumTracks(String id, String market, int limit = 20, int offset = 0)
{
limit = Math.Min(50, limit);
StringBuilder builder = new StringBuilder("https://api.spotify.com/v1/albums/" + id + "/tracks");
builder.Append("?limit=" + limit);
builder.Append("&offset=" + offset);
if (market != "")
builder.Append("&market=" + market);
return DownloadData<Paging<SimpleTrack>>(builder.ToString());
}
public FullAlbum GetAlbum(String id)
public FullAlbum GetAlbum(String id, String market = "")
{
if(market == "")
return DownloadData<FullAlbum>("https://api.spotify.com/v1/albums/" + id);
else
return DownloadData<FullAlbum>("https://api.spotify.com/v1/albums/" + id + "?market=" + market);
}
#endregion
#region Category
public CategoryList GetCategories(String country = "", String locale = "", int limit = 20, int offset = 0)
{
limit = Math.Min(50, limit);
StringBuilder builder = new StringBuilder("https://api.spotify.com/v1/browse/categories");
builder.Append("?limit=" + limit);
builder.Append("&offset=" + offset);
if (country != "")
builder.Append("&country=" + country);
if (locale != "")
builder.Append("&locale=" + locale);
return DownloadData<CategoryList>(builder.ToString());
}
public Category GetCategory(String categoryId, String country = "", String locale = "")
{
StringBuilder builder = new StringBuilder("https://api.spotify.com/v1/browse/categories/" + categoryId);
if(country != "")
builder.Append("?country=" + country);
if(locale != "")
{
if (country != "")
builder.Append("&locale=" + locale);
else
builder.Append("?locale=" + locale);
}
return DownloadData<Category>(builder.ToString());
}
public CategoryPlaylist GetCategoryPlaylists(String categoryId, String country = "", int limit = 20, int offset = 0)
{
limit = Math.Min(50, limit);
StringBuilder builder = new StringBuilder("https://api.spotify.com/v1/browse/categories/" + categoryId + "/playlists");
builder.Append("?limit=" + limit);
builder.Append("&offset=" + offset);
if (country != "")
builder.Append("&country=" + country);
return DownloadData<CategoryPlaylist>(builder.ToString());
}
#endregion
#region Util
public T UploadData<T>(String url, String uploadData, String method = "POST")
{
@ -263,7 +378,6 @@ namespace SpotifyAPI.SpotifyWebAPI
{
response = new StreamReader(e.Response.GetResponseStream()).ReadToEnd();
}
Debug.WriteLine(response);
return response;
}
#endregion

View File

@ -10,41 +10,14 @@ namespace SpotifyAPI.SpotifyWebAPI
{
public static class Util
{
public static string GetScopeValue(this Scope en,String separator)
public static string GetStringAttribute<T>(this T en, String separator) where T : struct, IConvertible
{
Enum e = (Enum)(object)en;
IEnumerable<StringAttribute> attributes =
Enum.GetValues(typeof(Scope))
.Cast<Scope>()
.Where(v => en.HasFlag(v))
.Select(v => typeof(Scope).GetField(v.ToString()))
.Select(f => f.GetCustomAttributes(typeof(StringAttribute), false)[0])
.Cast<StringAttribute>();
List<String> list = new List<String>();
attributes.ToList().ForEach((element) => list.Add(element.Text));
return string.Join(" ", list);
}
public static string GetSearchValue(this SearchType en, String separator)
{
IEnumerable<StringAttribute> attributes =
Enum.GetValues(typeof(SearchType))
.Cast<SearchType>()
.Where(v => en.HasFlag(v))
.Select(v => typeof(SearchType).GetField(v.ToString()))
.Select(f => f.GetCustomAttributes(typeof(StringAttribute), false)[0])
.Cast<StringAttribute>();
List<String> list = new List<String>();
attributes.ToList().ForEach((element) => list.Add(element.Text));
return string.Join(" ", list);
}
public static string GetAlbumValue(this AlbumType en, String separator)
{
IEnumerable<StringAttribute> attributes =
Enum.GetValues(typeof(AlbumType))
.Cast<AlbumType>()
.Where(v => en.HasFlag(v))
.Select(v => typeof(AlbumType).GetField(v.ToString()))
Enum.GetValues(typeof(T))
.Cast<T>()
.Where(v => e.HasFlag((Enum)(object)v))
.Select(v => typeof(T).GetField(v.ToString()))
.Select(f => f.GetCustomAttributes(typeof(StringAttribute), false)[0])
.Cast<StringAttribute>();

View File

@ -25,6 +25,7 @@ namespace SpotifyWebAPIExample
RedirectUri = "http://localhost",
//How many permissions we need?
Scope = Scope.USER_READ_PRIVATE | Scope.USER_READ_EMAIL | Scope.PLAYLIST_READ_PRIVATE | Scope.USER_LIBRARAY_READ | Scope.USER_LIBRARY_MODIFY | Scope.USER_READ_PRIVATE
| Scope.USER_FOLLOW_MODIFY | Scope.USER_FOLLOW_READ | Scope.PLAYLIST_MODIFY_PRIVATE | Scope.USER_READ_BIRTHDATE
};
//Start the internal http server
auth.StartHttpServer();