Skip to content

Commit

Permalink
Merge upstream/master
Browse files Browse the repository at this point in the history
  • Loading branch information
Ewan authored and Ewan committed Jul 10, 2017
2 parents d4ac6e8 + c0cb56a commit 3f7c289
Show file tree
Hide file tree
Showing 14 changed files with 302 additions and 45 deletions.
4 changes: 2 additions & 2 deletions E.Deezer/Api/Album.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ public string ArtistName
public DeezerClient Client { get; set; }
public void Deserialize(DeezerClient aClient) { Client = aClient; }

[Obsolete("Please use GetPicture instead.")]
[Obsolete("Please use GetPicture instead.", true)]
public string GetCover(PictureSize aSize) { return GetPicture(aSize); }

[Obsolete("Please use HasPicture instead.")]
[Obsolete("Please use HasPicture instead.", true)]
public bool HasCover(PictureSize aSize) { return HasPicture(aSize); }


Expand Down
16 changes: 16 additions & 0 deletions E.Deezer/Api/Internal/DeezerCreateResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using RestSharp.Deserializers;

namespace E.Deezer.Api
{
//Retrun value of all Deezer API calls
internal class DeezerCreateResponse
{
[DeserializeAs(Name = "id")]
public uint Id { get; set; }
}
}
46 changes: 37 additions & 9 deletions E.Deezer/Api/Internal/Permissions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,47 @@ internal class OAuthPermissions : IPermissions
[DeserializeAs(Name="listening_history")]
public bool HasListeningHistory {get; set; }

//TODO - Check a method that has multiple permissions...
public bool HasPermission(DeezerPermissions aPermission)
{
switch (aPermission)
bool permission = true;

if (aPermission.HasFlag(DeezerPermissions.BasicAccess))
{
permission &= HasBasicAccess;
}

if (aPermission.HasFlag(DeezerPermissions.DeleteLibrary))
{
permission &= HasDeleteLibrary;
}

if (aPermission.HasFlag(DeezerPermissions.Email))
{
permission &= HasEmail;
}

if (aPermission.HasFlag(DeezerPermissions.ListeningHistory))
{
case DeezerPermissions.BasicAccess: { return HasBasicAccess; }
case DeezerPermissions.DeleteLibrary: { return HasDeleteLibrary; }
case DeezerPermissions.Email: { return HasEmail; }
case DeezerPermissions.ListeningHistory: { return HasListeningHistory; }
case DeezerPermissions.ManageCommunity: { return HasManageCommunity; }
case DeezerPermissions.ManageLibrary: { return HasManageLibrary; }
case DeezerPermissions.OfflineAccess: { return HasOfflineAccess; }
default: { return false; }
permission &= HasListeningHistory;
}

if (aPermission.HasFlag(DeezerPermissions.ManageCommunity))
{
permission &= HasManageCommunity;
}

if (aPermission.HasFlag(DeezerPermissions.ManageLibrary))
{
permission &= HasManageLibrary;
}

if (aPermission.HasFlag(DeezerPermissions.OfflineAccess))
{
permission &= HasOfflineAccess;
}

return permission;
}
}
}
101 changes: 98 additions & 3 deletions E.Deezer/Api/Playlist.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,22 @@ public interface IPlaylist : IObjectWithImage
Task<IEnumerable<ITrack>> GetTracks(uint aStart, uint aCount);

Task<bool> Rate(int aRating);
}

//Manage Tracks
Task<bool> AddTrack(ITrack aTrack);
Task<bool> AddTrack(Int64 aTrackId);

Task<bool> AddTracks(IEnumerable<ITrack> aTracks);
Task<bool> AddTracks(IEnumerable<Int64> aTrackIds);
Task<bool> AddTracks(string aTrackIds);

Task<bool> RemoveTrack(ITrack aTrack);
Task<bool> RemoveTrack(Int64 aTrackId);

Task<bool> RemoveTracks(IEnumerable<ITrack> aTracks);
Task<bool> RemoveTracks(IEnumerable<Int64> aTrackIds);
Task<bool> RemoveTracks(string aTrackIds);
}

internal class Playlist : ObjectWithImage, IPlaylist, IDeserializable<DeezerClient>
{
Expand Down Expand Up @@ -76,7 +91,6 @@ public Task<IEnumerable<ITrack>> GetTracks(uint aStart, uint aCount)
}, Client.CancellationToken, TaskContinuationOptions.NotOnCanceled, TaskScheduler.Default);
}


public Task<bool> Rate(int aRating)
{
if (aRating < 1 || aRating > 5) { throw new ArgumentOutOfRangeException("aRating", "Rating value should be between 1 and 5 (inclusive)"); }
Expand All @@ -91,7 +105,88 @@ public Task<bool> Rate(int aRating)
}


public override string ToString()
public Task<bool> AddTrack(ITrack aTrack) { return AddTrack(aTrack.Id); }
public Task<bool> AddTrack(Int64 aTrackId) { return AddTracks(aTrackId.ToString()); }

public Task<bool> AddTracks(IEnumerable<Int64> aTrackIds)
{
if (aTrackIds.Count() > 0)
{
return AddTracks(string.Join(",", aTrackIds.Select((v) => v.ToString())));
}
else
{
throw new ArgumentException("Must provide at least one track ID", "aTrackIds");
}

}

public Task<bool> AddTracks(IEnumerable<ITrack> aTracks)
{
if (aTracks.Count() > 0)
{
return AddTracks(string.Join(",", aTracks.Select((v) => v.Id.ToString())));
}
else
{
throw new ArgumentException("Must provide at least one track ID", "aTrackIds");
}
}

public Task<bool> AddTracks(string aTrackIds)
{
List<IRequestParameter> parms = new List<IRequestParameter>()
{
RequestParameter.GetNewUrlSegmentParamter("playlist_id", Id),
RequestParameter.GetNewQueryStringParameter("songs", aTrackIds)
};

return Client.Post("playlist/{playlist_id}/tracks", parms, DeezerPermissions.ManageLibrary);
}


public Task<bool> RemoveTrack(ITrack aTrack) { return RemoveTrack(aTrack.Id); }
public Task<bool> RemoveTrack(Int64 aTrackId) { return RemoveTracks(aTrackId.ToString()); }

public Task<bool> RemoveTracks(IEnumerable<Int64> aTrackIds)
{
if (aTrackIds.Count() > 0)
{
return RemoveTracks(string.Join(",", aTrackIds.Select((v) => v.ToString())));
}
else
{
throw new ArgumentException("Must provide at least one track ID", "aTrackIds");
}

}

public Task<bool> RemoveTracks(IEnumerable<ITrack> aTracks)
{
if (aTracks.Count() > 0)
{
return RemoveTracks(string.Join(",", aTracks.Select((v) => v.Id.ToString())));
}
else
{
throw new ArgumentException("Must provide at least one track ID", "aTrackIds");
}
}

public Task<bool> RemoveTracks(string aTrackIds)
{
List<IRequestParameter> parms = new List<IRequestParameter>()
{
RequestParameter.GetNewUrlSegmentParamter("playlist_id", Id),
RequestParameter.GetNewQueryStringParameter("songs", aTrackIds)
};

return Client.Delete("playlist/{playlist_id}/tracks", parms, DeezerPermissions.ManageLibrary | DeezerPermissions.DeleteLibrary);
}



public override string ToString()
{
return string.Format("E.Deezer: Playlist({0} [{1}])", Title, CreatorName);
}
Expand Down
19 changes: 15 additions & 4 deletions E.Deezer/Api/Track.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,20 @@ namespace E.Deezer.Api
{
public interface ITrack : IObjectWithImage
{
uint Id { get; set; }
Int64 Id { get; set; }
string Title { get; set; }
string Link { get; set; }
uint Duration { get; set; }
DateTime ReleaseDate { get; set; }
DateTime TimeAdd { get; set; }
bool Explicit { get; set; }
string Preview { get; set; }
string ArtistName { get; }
string AlbumName { get; }
IArtist Artist { get; }
IAlbum Album { get; }
uint Number { get; }
uint Disc { get; }

string GetCover(PictureSize aSize);
bool HasCover(PictureSize aSize);
Expand All @@ -30,9 +33,11 @@ public interface ITrack : IObjectWithImage

internal class Track : ObjectWithImage, ITrack, IDeserializable<DeezerClient>
{
public uint Id { get; set; }
public Int64 Id { get; set; }
public string Title { get; set; }
public string Link { get; set; }
[DeserializeAs(Name = "time_add")]
public DateTime TimeAdd { get; set; }
public uint Duration { get; set; }
public DateTime ReleaseDate { get; set; }
public string Artwork { get; set; }
Expand All @@ -41,6 +46,12 @@ internal class Track : ObjectWithImage, ITrack, IDeserializable<DeezerClient>
public IArtist Artist { get { return ArtistInternal; } }
public IAlbum Album { get { return AlbumInternal; } }

[DeserializeAs(Name = "track_position")]
public uint Number { get; set; }

[DeserializeAs(Name = "disc_number")]
public uint Disc { get; set; }

public string ArtistName
{
get
Expand Down Expand Up @@ -72,10 +83,10 @@ public void Deserialize(DeezerClient aClient)
Client = aClient;
}

[Obsolete("Please use GetPicture instead.")]
[Obsolete("Please use GetPicture instead.", true)]
public string GetCover(PictureSize aSize) { return GetPicture(aSize); }

[Obsolete("Please use HasPicture instead.")]
[Obsolete("Please use HasPicture instead.", true)]
public bool HasCover(PictureSize aSize) { return HasPicture(aSize); }

public override string ToString()
Expand Down
28 changes: 27 additions & 1 deletion E.Deezer/Api/User.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,11 @@ public interface IUser : IObjectWithImage
Task<IEnumerable<IPlaylist>> GetRecommendedPlaylists();
Task<IEnumerable<IPlaylist>> GetRecommendedPlaylists(uint aCount);
Task<IEnumerable<IPlaylist>> GetRecommendedPlaylists(uint aStart, uint aCount);
}

Task<uint> CreatePlaylist(string title);

Task<bool> AddToPlaylist(uint playlistId, string songids);
}

internal class User : ObjectWithImage, IUser, IDeserializable<DeezerClient>
{
Expand Down Expand Up @@ -145,5 +149,27 @@ private Task<IEnumerable<TDest>> Get<TSource, TDest>(string aMethod, DeezerPermi
public Task<IEnumerable<ITrack>> GetRecommendedTracks(uint aCount) { return GetRecommendedTracks(0, aCount); }
public Task<IEnumerable<ITrack>> GetRecommendedTracks(uint aStart, uint aCount) { return Get<Track, ITrack>("recommendations/tracks", DeezerPermissions.BasicAccess, aStart, aCount); }

public Task<uint> CreatePlaylist(string title)
{
List<IRequestParameter> parms = new List<IRequestParameter>()
{
RequestParameter.GetNewUrlSegmentParamter("id", Id),
RequestParameter.GetNewQueryStringParameter("title", title)
};

return Client.Post<DeezerCreateResponse>("user/{id}/playlists", parms, DeezerPermissions.ManageLibrary).ContinueWith(t => t.Result.Id);
}

[Obsolete("Preferable to use IPlaylist.AddTrack(s) methods instead")]
public Task<bool> AddToPlaylist(uint playlistId, string songids)
{
List<IRequestParameter> parms = new List<IRequestParameter>()
{
RequestParameter.GetNewUrlSegmentParamter("playlist_id", playlistId),
RequestParameter.GetNewQueryStringParameter("songs", songids)
};

return Client.Post("playlist/{playlist_id}/tracks", parms, DeezerPermissions.ManageLibrary);
}
}
}
16 changes: 10 additions & 6 deletions E.Deezer/Deezer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,19 @@ namespace E.Deezer
/// </summary>
public class Deezer : IDisposable
{
private string iVersion;

private DeezerSession iSession;
private IBrowseEndpoint iBrowse;
private ISearchEndpoint iSearch;
private IUserEndpoint iUser;
private IRadioEndpoint iRadio;
private DeezerClient iClient;
private readonly IBrowseEndpoint iBrowse;
private readonly ISearchEndpoint iSearch;
private readonly IUserEndpoint iUser;
private readonly IRadioEndpoint iRadio;
private readonly DeezerClient iClient;

internal Deezer(DeezerSession aSession, bool underTest = false)
{
//iVersion = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();

iSession = aSession;
if (underTest) { iClient = new DeezerClient(iSession, true); }
else { iClient = new DeezerClient(iSession); }
Expand All @@ -43,7 +47,7 @@ internal Deezer(DeezerSession aSession, bool underTest = false)

public Task<IInfos> GetServiceInformation()
{
return iClient.Get<Infos>("infos").ContinueWith<IInfos>((aTask) => { return aTask.Result; }, iClient.CancellationToken, TaskContinuationOptions.NotOnCanceled, TaskScheduler.Default);
return iClient.GetPlain<Infos>("infos").ContinueWith<IInfos>((aTask) => { return aTask.Result; }, iClient.CancellationToken, TaskContinuationOptions.NotOnCanceled, TaskScheduler.Default);
}

//'OAuth'
Expand Down
Loading

0 comments on commit 3f7c289

Please sign in to comment.