- Updated. (Changelog-date: November 11th, 2014)

- Improved HTTPServer
- Ordered methods in SpotifyWebApiCass
- Added various models for the new endpoints
This commit is contained in:
Johnny @PC 2014-12-04 18:18:11 +01:00
parent 1476c4b866
commit cbd248a2fe
17 changed files with 275 additions and 73 deletions

View File

@ -7,7 +7,7 @@
<ProjectGuid>{EBBE35E2-7B91-4D7D-B8FC-3A0472F5119D}</ProjectGuid> <ProjectGuid>{EBBE35E2-7B91-4D7D-B8FC-3A0472F5119D}</ProjectGuid>
<OutputType>Library</OutputType> <OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder> <AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>SpotifyAPI.SpotifyLocalAPI</RootNamespace> <RootNamespace>SpotifyAPI</RootNamespace>
<AssemblyName>SpotifyAPI</AssemblyName> <AssemblyName>SpotifyAPI</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion> <TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment> <FileAlignment>512</FileAlignment>
@ -65,9 +65,11 @@
<Compile Include="SpotifyWebAPI\ClientCredentialsAuth.cs" /> <Compile Include="SpotifyWebAPI\ClientCredentialsAuth.cs" />
<Compile Include="SpotifyWebAPI\ImplicitGrantAuth.cs" /> <Compile Include="SpotifyWebAPI\ImplicitGrantAuth.cs" />
<Compile Include="SpotifyWebAPI\Models\BasicModel.cs" /> <Compile Include="SpotifyWebAPI\Models\BasicModel.cs" />
<Compile Include="SpotifyWebAPI\Models\FeaturedPlaylists.cs" />
<Compile Include="SpotifyWebAPI\Models\FullAlbum.cs" /> <Compile Include="SpotifyWebAPI\Models\FullAlbum.cs" />
<Compile Include="SpotifyWebAPI\Models\FullArtist.cs" /> <Compile Include="SpotifyWebAPI\Models\FullArtist.cs" />
<Compile Include="SpotifyWebAPI\Models\FullTrack.cs" /> <Compile Include="SpotifyWebAPI\Models\FullTrack.cs" />
<Compile Include="SpotifyWebAPI\Models\NewAlbumReleases.cs" />
<Compile Include="SpotifyWebAPI\Models\SearchItem.cs" /> <Compile Include="SpotifyWebAPI\Models\SearchItem.cs" />
<Compile Include="SpotifyWebAPI\Models\PrivateProfile.cs" /> <Compile Include="SpotifyWebAPI\Models\PrivateProfile.cs" />
<Compile Include="SpotifyWebAPI\Models\GeneralModels.cs" /> <Compile Include="SpotifyWebAPI\Models\GeneralModels.cs" />

View File

@ -58,13 +58,7 @@ namespace SpotifyAPI.SpotifyWebAPI
} }
public void StopHttpServer() public void StopHttpServer()
{ {
try httpServer.Dispose();
{
httpThread.Abort();
}catch(ThreadAbortException e)
{
}
httpServer = null; httpServer = null;
} }
} }

View File

@ -10,11 +10,11 @@ namespace SpotifyAPI.SpotifyWebAPI.Models
public abstract class BasicModel public abstract class BasicModel
{ {
[JsonProperty("error")] [JsonProperty("error")]
public Error Error { get; set; } public Error ErrorResponse { get; set; }
public Boolean HasError() public Boolean HasError()
{ {
return Error != null; return ErrorResponse != null;
} }
} }
} }

View File

@ -0,0 +1,17 @@
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 FeaturedPlaylists : BasicModel
{
[JsonProperty("message")]
public String Message { get; set; }
[JsonProperty("playlists")]
public Paging<SimplePlaylist> Playlists { get; set; }
}
}

View File

@ -15,6 +15,8 @@ namespace SpotifyAPI.SpotifyWebAPI.Models
public List<SimpleArtist> Artists { get; set; } public List<SimpleArtist> Artists { get; set; }
[JsonProperty("available_markets")] [JsonProperty("available_markets")]
public List<String> AvailableMarkets { get; set; } public List<String> AvailableMarkets { get; set; }
[JsonProperty("copyrights")]
public List<Copyright> Copyrights { get; set; }
[JsonProperty("external_ids")] [JsonProperty("external_ids")]
public Dictionary<String, String> ExternalIds { get; set; } public Dictionary<String, String> ExternalIds { get; set; }
[JsonProperty("external_urls")] [JsonProperty("external_urls")]

View File

@ -11,6 +11,8 @@ namespace SpotifyAPI.SpotifyWebAPI.Models
{ {
[JsonProperty("external_urls")] [JsonProperty("external_urls")]
public Dictionary<String, String> ExternalUrls { get; set; } public Dictionary<String, String> ExternalUrls { get; set; }
[JsonProperty("followers")]
public Followers Followers { get; set; }
[JsonProperty("genres")] [JsonProperty("genres")]
public List<String> Genres { get; set; } public List<String> Genres { get; set; }
[JsonProperty("href")] [JsonProperty("href")]

View File

@ -14,8 +14,8 @@ namespace SpotifyAPI.SpotifyWebAPI.Models
/// </summary> /// </summary>
[JsonProperty("album")] [JsonProperty("album")]
public SimpleAlbum Album { get; set; } public SimpleAlbum Album { get; set; }
[JsonProperty("artist")] [JsonProperty("artists")]
public SimpleArtist Artist { get; set; } public List<SimpleArtist> Artists { get; set; }
[JsonProperty("available_markets")] [JsonProperty("available_markets")]
public List<String> AvailableMarkets { get; set; } public List<String> AvailableMarkets { get; set; }
[JsonProperty("disc_number")] [JsonProperty("disc_number")]

View File

@ -16,6 +16,12 @@ namespace SpotifyAPI.SpotifyWebAPI.Models
[JsonProperty("height")] [JsonProperty("height")]
public int Height { get; set; } public int Height { get; set; }
} }
public class ErrorResponse
{
[JsonProperty("error")]
public Error Error { get; set; }
}
public class Error public class Error
{ {
[JsonProperty("status")] [JsonProperty("status")]
@ -46,13 +52,31 @@ namespace SpotifyAPI.SpotifyWebAPI.Models
[JsonProperty("track")] [JsonProperty("track")]
public FullTrack Track { get; set; } public FullTrack Track { get; set; }
} }
internal class CreatePlaylistArgs public class CreatePlaylistArgs
{ {
[JsonProperty("name")] [JsonProperty("name")]
public String Name { get; set; } public String Name { get; set; }
[JsonProperty("public")] [JsonProperty("public")]
public Boolean Public { get; set; } public Boolean Public { get; set; }
} }
public class DeleteTrackArg
{
[JsonProperty("uri")]
public String Uri { get; set; }
[JsonProperty("positions")]
public List<int> Positions { get; set; }
public DeleteTrackArg(String uri, params int[] positions)
{
this.Positions = positions.ToList();
this.Uri = uri;
}
public bool ShouldSerializePositions()
{
// don't serialize the Manager property if an employee is their own manager
return (Positions.Count > 0);
}
}
public class SeveralTracks public class SeveralTracks
{ {
[JsonProperty("tracks")] [JsonProperty("tracks")]
@ -68,4 +92,16 @@ namespace SpotifyAPI.SpotifyWebAPI.Models
[JsonProperty("albums")] [JsonProperty("albums")]
public List<FullAlbum> Albums { get; set; } public List<FullAlbum> Albums { get; set; }
} }
public class Copyright
{
[JsonProperty("text")]
public String Text { get; set; }
[JsonProperty("type")]
public String Type { get; set; }
}
public class CheckUserTracks : BasicModel
{
public List<Boolean> Checked { 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 NewAlbumReleases : BasicModel
{
[JsonProperty("albums")]
public Paging<SimpleAlbum> Albums { get; set; }
}
}

View File

@ -15,6 +15,8 @@ namespace SpotifyAPI.SpotifyWebAPI.Models
public String DisplayName { get; set; } public String DisplayName { get; set; }
[JsonProperty("external_urls")] [JsonProperty("external_urls")]
public Dictionary<string, string> ExternalUrls { get; set; } public Dictionary<string, string> ExternalUrls { get; set; }
[JsonProperty("followers")]
public Followers Followers { get; set; }
[JsonProperty("href")] [JsonProperty("href")]
public String Href { get; set; } public String Href { get; set; }
[JsonProperty("id")] [JsonProperty("id")]

View File

@ -9,12 +9,18 @@ namespace SpotifyAPI.SpotifyWebAPI.Models
{ {
public class PublicProfile : BasicModel public class PublicProfile : BasicModel
{ {
[JsonProperty("display_name")]
public String DisplayName { get; set; }
[JsonProperty("external_urls")] [JsonProperty("external_urls")]
public Dictionary<string, string> ExternalUrls { get; set; } public Dictionary<string, string> ExternalUrls { get; set; }
[JsonProperty("followers")]
public Followers Followers { get; set; }
[JsonProperty("href")] [JsonProperty("href")]
public String Href { get; set; } public String Href { get; set; }
[JsonProperty("id")] [JsonProperty("id")]
public String Id { get; set; } public String Id { get; set; }
[JsonProperty("images")]
public List<Image> Images { get; set; }
[JsonProperty("type")] [JsonProperty("type")]
public String Type { get; set; } public String Type { get; set; }
[JsonProperty("uri")] [JsonProperty("uri")]

View File

@ -17,6 +17,8 @@ namespace SpotifyAPI.SpotifyWebAPI.Models
public String Href { get; set; } public String Href { get; set; }
[JsonProperty("id")] [JsonProperty("id")]
public String Id { get; set; } public String Id { get; set; }
[JsonProperty("images")]
public List<Image> Images { get; set; }
[JsonProperty("name")] [JsonProperty("name")]
public String Name { get; set; } public String Name { get; set; }
[JsonProperty("owner")] [JsonProperty("owner")]

View File

@ -11,6 +11,8 @@ namespace SpotifyAPI.SpotifyWebAPI.Models
{ {
[JsonProperty("artist")] [JsonProperty("artist")]
public SimpleArtist Artist { get; set; } public SimpleArtist Artist { get; set; }
[JsonProperty("available_markets")]
public List<String> AvailableMarkets { get; set; }
[JsonProperty("disc_number")] [JsonProperty("disc_number")]
public int DiscNumber { get; set; } public int DiscNumber { get; set; }
[JsonProperty("duration_ms")] [JsonProperty("duration_ms")]

View File

@ -11,8 +11,8 @@ namespace SpotifyAPI.SpotifyWebAPI
{ {
[StringAttribute("")] [StringAttribute("")]
NONE = 1, NONE = 1,
[StringAttribute("playlist-modify")] [StringAttribute("playlist-modify-public")]
PLAYLIST_MODIFY = 2, PLAYLIST_MODIFY_PUBLIC = 2,
[StringAttribute("playlist-modify-private")] [StringAttribute("playlist-modify-private")]
PLAYLIST_MODIFY_PRIVATE = 4, PLAYLIST_MODIFY_PRIVATE = 4,
[StringAttribute("playlist-read-private")] [StringAttribute("playlist-read-private")]
@ -22,6 +22,10 @@ namespace SpotifyAPI.SpotifyWebAPI
[StringAttribute("user-read-private")] [StringAttribute("user-read-private")]
USER_READ_PRIVATE = 32, USER_READ_PRIVATE = 32,
[StringAttribute("user-read-email")] [StringAttribute("user-read-email")]
USER_READ_EMAIL = 64 USER_READ_EMAIL = 64,
[StringAttribute("user-library-read")]
USER_LIBRARAY_READ = 128,
[StringAttribute("user-library-modify")]
USER_LIBRARY_MODIFY = 256
} }
} }

View File

@ -162,32 +162,49 @@ namespace SpotifyAPI.SpotifyWebAPI
} }
} }
public abstract class HttpServer { public abstract class HttpServer : IDisposable {
protected int port; protected int port;
TcpListener listener; TcpListener listener;
bool is_active = true; public bool IsActive { get; set; }
public HttpServer(int port) { public HttpServer(int port)
{
this.IsActive = true;
this.port = port; this.port = port;
} }
public void listen() public void listen()
{ {
listener = new TcpListener(port); try
listener.Start(); {
while (is_active) { listener = new TcpListener(port);
TcpClient s = listener.AcceptTcpClient(); listener.Start();
HttpProcessor processor = new HttpProcessor(s, this); while (IsActive)
Thread thread = new Thread(new ThreadStart(processor.process)); {
thread.Start(); TcpClient s = listener.AcceptTcpClient();
Thread.Sleep(1); HttpProcessor processor = new HttpProcessor(s, this);
Thread thread = new Thread(new ThreadStart(processor.process));
thread.Start();
Thread.Sleep(1);
}
}catch(Exception)
{
} }
} }
public void Dispose()
{
IsActive = false;
listener.Stop();
}
public abstract void handleGETRequest(HttpProcessor p); public abstract void handleGETRequest(HttpProcessor p);
public abstract void handlePOSTRequest(HttpProcessor p, StreamReader inputData); public abstract void handlePOSTRequest(HttpProcessor p, StreamReader inputData);
}
}
public class SimpleHttpServer : HttpServer { public class SimpleHttpServer : HttpServer {

View File

@ -2,17 +2,18 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Diagnostics;
using System.Net; using System.Net;
using SpotifyAPI.SpotifyWebAPI; using SpotifyAPI.SpotifyWebAPI;
using SpotifyAPI.SpotifyWebAPI.Models; using SpotifyAPI.SpotifyWebAPI.Models;
using Newtonsoft.Json; using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Collections.Specialized; using System.Collections.Specialized;
using System.IO; using System.IO;
namespace SpotifyAPI.SpotifyWebAPI namespace SpotifyAPI.SpotifyWebAPI
{ {
public class SpotifyWebAPIClass public class SpotifyWebAPIClass : IDisposable
{ {
public String TokenType { get; set; } public String TokenType { get; set; }
public String AccessToken { get; set; } public String AccessToken { get; set; }
@ -28,53 +29,139 @@ namespace SpotifyAPI.SpotifyWebAPI
UseAuth = true; UseAuth = true;
webclient = new WebClient(); webclient = new WebClient();
webclient.Proxy = null; webclient.Proxy = null;
settings = new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }; settings = new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore, TypeNameHandling = TypeNameHandling.All };
} }
#region User
public PrivateProfile GetPrivateProfile() public PrivateProfile GetPrivateProfile()
{ {
return DownloadString<PrivateProfile>("https://api.spotify.com/v1/me"); return DownloadData<PrivateProfile>("https://api.spotify.com/v1/me");
} }
public PublicProfile GetPublicProfile(String userId = "") public PublicProfile GetPublicProfile(String userId = "")
{ {
if(userId.Length == 0) if (userId.Length == 0)
return DownloadString<PublicProfile>("https://api.spotify.com/v1/me"); return DownloadData<PublicProfile>("https://api.spotify.com/v1/me");
else else
return DownloadString<PublicProfile>("https://api.spotify.com/v1/users/" + userId); return DownloadData<PublicProfile>("https://api.spotify.com/v1/users/" + userId);
} }
#endregion
#region User-Library
public ErrorResponse SaveTracks(List<String> ids)
{
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()
{
return DownloadData<Paging<FullTrack>>("https://api.spotify.com/v1/me/tracks");
}
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)
{
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 };
}
else
{
return new CheckUserTracks { Checked = null, ErrorResponse = res.ToObject<Error>() };
}
}
#endregion
#region Playlist
public Paging<SimplePlaylist> GetUserPlaylists(String userId) public Paging<SimplePlaylist> GetUserPlaylists(String userId)
{ {
return DownloadString<Paging<SimplePlaylist>>("https://api.spotify.com/v1/users/" + userId + "/playlists"); return DownloadData<Paging<SimplePlaylist>>("https://api.spotify.com/v1/users/" + userId + "/playlists");
} }
public FullPlaylist GetPlaylist(String userId,String playlistId) public FullPlaylist GetPlaylist(String userId, String playlistId)
{ {
return DownloadString<FullPlaylist>("https://api.spotify.com/v1/users/" + userId + "/playlists/" + playlistId); 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)
{ {
return DownloadString<Paging<PlaylistTrack>>("https://api.spotify.com/v1/users/" + userId + "/playlists/" + playlistId + "/tracks"); return DownloadData<Paging<PlaylistTrack>>("https://api.spotify.com/v1/users/" + userId + "/playlists/" + playlistId + "/tracks");
} }
public FullPlaylist CreatePlaylist(String userId,String playlistName,Boolean isPublic = true) public FullPlaylist CreatePlaylist(String userId, String playlistName, Boolean isPublic = true)
{ {
CreatePlaylistArgs args = new CreatePlaylistArgs() CreatePlaylistArgs args = new CreatePlaylistArgs()
{ {
Name = playlistName, Name = playlistName,
Public = isPublic Public = isPublic
}; };
return UploadData<FullPlaylist>("https://api.spotify.com/v1/users/" + userId + "/playlists",JsonConvert.SerializeObject(args)); return UploadData<FullPlaylist>("https://api.spotify.com/v1/users/" + userId + "/playlists", JsonConvert.SerializeObject(args));
} }
public Error AddTracks(String userId,String playlistId,List<String> uris,int position = int.MaxValue) public ErrorResponse UpdatePlaylist(String userId, String playlistId, String newName = null, Boolean? newPublic = null)
{ {
if(position == int.MaxValue) JObject ob = new JObject();
return UploadData<Error>("https://api.spotify.com/v1/users/" + userId + "/playlists/" + playlistId + "/tracks", JsonConvert.SerializeObject(uris)); if (newName != null)
ob.Add("name", newName);
if (newPublic != null)
ob.Add("public", newPublic);
return UploadData<ErrorResponse>("https://api.spotify.com/v1/users/" + userId + "/playlists/" + playlistId, ob.ToString(Formatting.None), "PUT");
}
public ErrorResponse ReplacePlaylistTracks(String userId, String playlistId, List<String> uris)
{
JObject ob = new JObject();
ob.Add("uris", new JArray(uris));
return UploadData<ErrorResponse>("https://api.spotify.com/v1/users/" + userId + "/playlists/" + playlistId + "/tracks", ob.ToString(Formatting.None), "PUT");
}
public ErrorResponse DeletePlaylistTracks(String userId, String playlistId, List<DeleteTrackArg> args)
{
JObject ob = new JObject();
ob.Add("tracks", JArray.FromObject(args));
return UploadData<ErrorResponse>("https://api.spotify.com/v1/users/" + userId + "/playlists/" + playlistId + "/tracks", ob.ToString(Formatting.None), "DELETE");
}
public ErrorResponse AddTracks(String userId, String playlistId, List<String> uris, int position = int.MaxValue)
{
if (position == int.MaxValue)
return UploadData<ErrorResponse>("https://api.spotify.com/v1/users/" + userId + "/playlists/" + playlistId + "/tracks", JsonConvert.SerializeObject(uris));
else else
{ {
String tracks = string.Join(",", uris); String tracks = string.Join(",", uris);
return UploadData<Error>("https://api.spotify.com/v1/users/" + userId + "/playlists/" + playlistId + "/tracks?position=" + position return UploadData<ErrorResponse>("https://api.spotify.com/v1/users/" + userId + "/playlists/" + playlistId + "/tracks?position=" + position
+ "&ids=" + tracks + "&ids=" + tracks
, JsonConvert.SerializeObject(uris)); , JsonConvert.SerializeObject(uris));
} }
} }
public SearchItem SearchItems(String q,SearchType type,int limit = 20,int offset = 0) public FeaturedPlaylists GetFeaturedPlaylists(String locale = "", String country = "", DateTime timestamp = default(DateTime), int limit = 20, int offset = 0)
{
limit = Math.Max(50, limit);
StringBuilder builder = new StringBuilder("https://api.spotify.com/v1/browse/featured-playlists");
builder.Append("?limit=" + limit);
builder.Append("&offset=" + offset);
if(locale != "")
builder.Append("&locale=" + locale);
if(country != "")
builder.Append("&country=" + country);
if (timestamp != default(DateTime))
builder.Append("&timestamp=" + timestamp.ToString("yyyy-MM-ddTHH:mm:ss"));
return DownloadData<FeaturedPlaylists>(builder.ToString());
}
#endregion
#region Search and Fetch
public NewAlbumReleases GetNewAlbumReleases(String country = "", int limit = 50, int offset = 0)
{
limit = Math.Max(50, limit);
StringBuilder builder = new StringBuilder("https://api.spotify.com/v1/browse/new-releases");
builder.Append("?limit=" + limit);
builder.Append("&offset=" + offset);
if (country != "")
builder.Append("&country=" + country);
return DownloadData<NewAlbumReleases>(builder.ToString());
}
public SearchItem SearchItems(String q, SearchType type, int limit = 20, int offset = 0)
{ {
limit = Math.Min(50, limit); limit = Math.Min(50, limit);
StringBuilder builder = new StringBuilder("https://api.spotify.com/v1/search"); StringBuilder builder = new StringBuilder("https://api.spotify.com/v1/search");
@ -83,67 +170,72 @@ namespace SpotifyAPI.SpotifyWebAPI
builder.Append("&limit=" + limit); builder.Append("&limit=" + limit);
builder.Append("&offset=" + offset); builder.Append("&offset=" + offset);
return DownloadString<SearchItem>(builder.ToString()); return DownloadData<SearchItem>(builder.ToString());
} }
public SeveralTracks GetSeveralTracks(List<String> ids) public SeveralTracks GetSeveralTracks(List<String> ids)
{ {
return DownloadString<SeveralTracks>("https://api.spotify.com/v1/tracks?ids=" + string.Join(",",ids)); return DownloadData<SeveralTracks>("https://api.spotify.com/v1/tracks?ids=" + string.Join(",", ids));
} }
public SeveralAlbums GetSeveralAlbums(List<String> ids) public SeveralAlbums GetSeveralAlbums(List<String> ids)
{ {
return DownloadString<SeveralAlbums>("https://api.spotify.com/v1/albums?ids=" + string.Join(",", ids)); return DownloadData<SeveralAlbums>("https://api.spotify.com/v1/albums?ids=" + string.Join(",", ids));
} }
public SeveralArtists GetSeveralArtists(List<String> ids) public SeveralArtists GetSeveralArtists(List<String> ids)
{ {
return DownloadString<SeveralArtists>("https://api.spotify.com/v1/artists?ids=" + string.Join(",", ids)); return DownloadData<SeveralArtists>("https://api.spotify.com/v1/artists?ids=" + string.Join(",", ids));
} }
public FullTrack GetTrack(String id) public FullTrack GetTrack(String id)
{ {
return DownloadString<FullTrack>("https://api.spotify.com/v1/tracks/" + id); return DownloadData<FullTrack>("https://api.spotify.com/v1/tracks/" + id);
} }
public SeveralArtists GetRelatedArtists(String id) public SeveralArtists GetRelatedArtists(String id)
{ {
return DownloadString<SeveralArtists>("https://api.spotify.com/v1/artists/" + id + "/related-artists"); return DownloadData<SeveralArtists>("https://api.spotify.com/v1/artists/" + id + "/related-artists");
} }
public SeveralTracks GetArtistsTopTracks(String id, String country) public SeveralTracks GetArtistsTopTracks(String id, String country)
{ {
return DownloadString<SeveralTracks>("https://api.spotify.com/v1/artists/" + id + "/top-tracks?country=" + country); return DownloadData<SeveralTracks>("https://api.spotify.com/v1/artists/" + id + "/top-tracks?country=" + country);
} }
public Paging<SimpleAlbum> GetArtistsAlbums(String id,AlbumType type = AlbumType.ALL,String country = "",int limit = 20,int offset = 0) public Paging<SimpleAlbum> GetArtistsAlbums(String id, AlbumType type = AlbumType.ALL, String market = "", int limit = 20, int offset = 0)
{ {
limit = Math.Min(50, limit); limit = Math.Min(50, limit);
StringBuilder builder = new StringBuilder("https://api.spotify.com/v1/artists/" + id + "/albums"); StringBuilder builder = new StringBuilder("https://api.spotify.com/v1/artists/" + id + "/albums");
builder.Append("?type=" + type.GetAlbumValue(",")); builder.Append("?type=" + type.GetAlbumValue(","));
builder.Append("&limit=" + limit); builder.Append("&limit=" + limit);
builder.Append("&offset=" + offset); builder.Append("&offset=" + offset);
return DownloadString<Paging<SimpleAlbum>>(builder.ToString()); if (market != "")
builder.Append("&market=" + market);
return DownloadData<Paging<SimpleAlbum>>(builder.ToString());
} }
public FullArtist GetArtist(String id) public FullArtist GetArtist(String id)
{ {
return DownloadString<FullArtist>("https://api.spotify.com/v1/artists/" + id); 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, int limit = 20, int offset = 0)
{ {
limit = Math.Min(50, limit); limit = Math.Min(50, limit);
StringBuilder builder = new StringBuilder("https://api.spotify.com/v1/albums/" + id + "/tracks"); StringBuilder builder = new StringBuilder("https://api.spotify.com/v1/albums/" + id + "/tracks");
builder.Append("?limit=" + limit); builder.Append("?limit=" + limit);
builder.Append("&offset=" + offset); builder.Append("&offset=" + offset);
return DownloadString<Paging<SimpleTrack>>(builder.ToString()); return DownloadData<Paging<SimpleTrack>>(builder.ToString());
} }
public FullAlbum GetAlbum(String id ) public FullAlbum GetAlbum(String id)
{ {
return DownloadString<FullAlbum>("https://api.spotify.com/v1/albums/" + id); return DownloadData<FullAlbum>("https://api.spotify.com/v1/albums/" + id);
} }
public T UploadData<T>(String url,String uploadData) #endregion
#region Util
public T UploadData<T>(String url, String uploadData, String method = "POST")
{ {
if (!UseAuth) if (!UseAuth)
throw new Exception("UseAuth required for 'UploadData'"); throw new Exception("UseAuth required for 'UploadData'");
webclient.Headers.Add("Authorization", TokenType + " " + AccessToken); webclient.Headers.Add("Authorization", TokenType + " " + AccessToken);
webclient.Headers.Add("Content-Type","application/json"); webclient.Headers.Add("Content-Type", "application/json");
String response = ""; String response = "";
try try
{ {
byte[] data = webclient.UploadData(url,Encoding.UTF8.GetBytes(uploadData)); byte[] data = webclient.UploadData(url, method, Encoding.UTF8.GetBytes(uploadData));
response = Encoding.UTF8.GetString(data); response = Encoding.UTF8.GetString(data);
} }
catch (WebException e) catch (WebException e)
@ -152,21 +244,30 @@ namespace SpotifyAPI.SpotifyWebAPI
} }
return JsonConvert.DeserializeObject<T>(response, settings); return JsonConvert.DeserializeObject<T>(response, settings);
} }
public T DownloadString<T>(String url) public T DownloadData<T>(String url)
{ {
if(UseAuth) return JsonConvert.DeserializeObject<T>(DownloadString(url), settings);
}
public String DownloadString(String url)
{
if (UseAuth)
webclient.Headers.Add("Authorization", TokenType + " " + AccessToken); webclient.Headers.Add("Authorization", TokenType + " " + AccessToken);
String response = ""; String response = "";
try try
{ {
byte [] data = webclient.DownloadData(url); byte[] data = webclient.DownloadData(url);
response = Encoding.UTF8.GetString(data); response = Encoding.UTF8.GetString(data);
}catch(WebException e) }
catch (WebException e)
{ {
response = new StreamReader(e.Response.GetResponseStream()).ReadToEnd(); response = new StreamReader(e.Response.GetResponseStream()).ReadToEnd();
} }
return JsonConvert.DeserializeObject<T>(response, settings); Debug.WriteLine(response);
return response;
} }
#endregion
public void Dispose() public void Dispose()
{ {
webclient.Dispose(); webclient.Dispose();

View File

@ -24,7 +24,7 @@ namespace SpotifyWebAPIExample
//Set this to localhost if you want to use the built-in HTTP Server //Set this to localhost if you want to use the built-in HTTP Server
RedirectUri = "http://localhost", RedirectUri = "http://localhost",
//How many permissions we need? //How many permissions we need?
Scope = Scope.USER_READ_PRIVATE | Scope.USER_READ_EMAIL | Scope.PLAYLIST_READ_PRIVATE 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
}; };
//Start the internal http server //Start the internal http server
auth.StartHttpServer(); auth.StartHttpServer();
@ -108,7 +108,7 @@ namespace SpotifyWebAPIExample
Console.WriteLine(playlist.Name + " (" + playlist.Id + ")"); Console.WriteLine(playlist.Name + " (" + playlist.Id + ")");
while(playlists.Next != null) while(playlists.Next != null)
{ {
playlists = spotify.DownloadString<Paging<SimplePlaylist>>(playlists.Next); playlists = spotify.DownloadData<Paging<SimplePlaylist>>(playlists.Next);
foreach (SimplePlaylist playlist in playlists.Items) foreach (SimplePlaylist playlist in playlists.Items)
Console.WriteLine(playlist.Name + " (" + playlist.Id + ")"); Console.WriteLine(playlist.Name + " (" + playlist.Id + ")");
} }
@ -127,7 +127,7 @@ namespace SpotifyWebAPIExample
Paging<PlaylistTrack> col = spotify.GetPlaylistTracks(spotify.GetPrivateProfile().Id, id); Paging<PlaylistTrack> col = spotify.GetPlaylistTracks(spotify.GetPrivateProfile().Id, id);
if(col.HasError()) if(col.HasError())
{ {
Console.WriteLine("ERROR: " + col.Error.Message); Console.WriteLine("ERROR: " + col.ErrorResponse.Message);
DisplayMenu(spotify); DisplayMenu(spotify);
return; return;
} }
@ -135,7 +135,7 @@ namespace SpotifyWebAPIExample
Console.WriteLine(track.Track.Name + " (" + track.Track.Id + ")"); Console.WriteLine(track.Track.Name + " (" + track.Track.Id + ")");
while (col.Next != null) while (col.Next != null)
{ {
col = spotify.DownloadString<Paging<PlaylistTrack>>(col.Next); col = spotify.DownloadData<Paging<PlaylistTrack>>(col.Next);
foreach (PlaylistTrack track in col.Items) foreach (PlaylistTrack track in col.Items)
Console.WriteLine(track.Track.Name + " (" + track.Track.Id + ")"); Console.WriteLine(track.Track.Name + " (" + track.Track.Id + ")");
} }