diff --git a/src/IF.Lastfm.Core/Api/ChartApi.cs b/src/IF.Lastfm.Core/Api/ChartApi.cs new file mode 100644 index 0000000..6f72d1a --- /dev/null +++ b/src/IF.Lastfm.Core/Api/ChartApi.cs @@ -0,0 +1,41 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using IF.Lastfm.Core.Api.Commands.ChartApi; +using IF.Lastfm.Core.Api.Helpers; +using IF.Lastfm.Core.Objects; + +namespace IF.Lastfm.Core.Api +{ + public class ChartApi : IChartApi + { + public IAuth Auth { get; private set; } + + public ChartApi(IAuth auth) + { + Auth = auth; + } + + public async Task> GetTopArtistAsync(int page = 1, int itemsPerPage = LastFm.DefaultPageLength) + { + var command = new GetTopArtistsCommand(Auth) + { + Page = page, + Count = itemsPerPage + }; + return await command.ExecuteAsync(); + } + + public async Task> GetTopTracksAsync(int page = 1, int itemsPerPage = LastFm.DefaultPageLength) + { + var command = new GetTopTracksCommand(Auth) + { + Page = page, + Count = itemsPerPage + }; + return await command.ExecuteAsync(); + } + } +} diff --git a/src/IF.Lastfm.Core/Api/Commands/ArtistApi/GetArtistTopAlbums.cs b/src/IF.Lastfm.Core/Api/Commands/ArtistApi/GetArtistTopAlbumsCommand.cs similarity index 100% rename from src/IF.Lastfm.Core/Api/Commands/ArtistApi/GetArtistTopAlbums.cs rename to src/IF.Lastfm.Core/Api/Commands/ArtistApi/GetArtistTopAlbumsCommand.cs diff --git a/src/IF.Lastfm.Core/Api/Commands/ArtistApi/GetArtistTopTracks.cs b/src/IF.Lastfm.Core/Api/Commands/ArtistApi/GetArtistTopTracksCommand.cs similarity index 100% rename from src/IF.Lastfm.Core/Api/Commands/ArtistApi/GetArtistTopTracks.cs rename to src/IF.Lastfm.Core/Api/Commands/ArtistApi/GetArtistTopTracksCommand.cs diff --git a/src/IF.Lastfm.Core/Api/Commands/ChartApi/GetTopArtistsCommand.cs b/src/IF.Lastfm.Core/Api/Commands/ChartApi/GetTopArtistsCommand.cs new file mode 100644 index 0000000..ff4d79b --- /dev/null +++ b/src/IF.Lastfm.Core/Api/Commands/ChartApi/GetTopArtistsCommand.cs @@ -0,0 +1,56 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net.Http; +using System.Text; +using System.Threading.Tasks; +using IF.Lastfm.Core.Api.Enums; +using IF.Lastfm.Core.Api.Helpers; +using IF.Lastfm.Core.Objects; +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; + +namespace IF.Lastfm.Core.Api.Commands.ChartApi +{ + internal class GetTopArtistsCommand : GetAsyncCommandBase> + { + public GetTopArtistsCommand(IAuth auth) + : base(auth) + { + Method = "chart.getTopArtists"; + } + + public override void SetParameters() + { + AddPagingParameters(); + DisableCaching(); + } + + public async override Task> HandleResponse(HttpResponseMessage response) + { + string json = await response.Content.ReadAsStringAsync(); + + LastFmApiError error; + if (LastFm.IsResponseValid(json, out error) && response.IsSuccessStatusCode) + { + var jtoken = JsonConvert.DeserializeObject(json); + + var artists = jtoken.SelectToken("artists") + .SelectToken("artist").Children() + .Select(LastArtist.ParseJToken) + .ToList(); + + var pageresponse = PageResponse.CreateSuccessResponse(artists); + + var attrToken = jtoken.SelectToken("artists").SelectToken("@attr"); + pageresponse.AddPageInfoFromJToken(attrToken); + + return pageresponse; + } + else + { + return LastResponse.CreateErrorResponse>(error); + } + } + } +} diff --git a/src/IF.Lastfm.Core/Api/Commands/ChartApi/GetTopTracksCommand.cs b/src/IF.Lastfm.Core/Api/Commands/ChartApi/GetTopTracksCommand.cs new file mode 100644 index 0000000..7df37ac --- /dev/null +++ b/src/IF.Lastfm.Core/Api/Commands/ChartApi/GetTopTracksCommand.cs @@ -0,0 +1,56 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net.Http; +using System.Text; +using System.Threading.Tasks; +using IF.Lastfm.Core.Api.Enums; +using IF.Lastfm.Core.Api.Helpers; +using IF.Lastfm.Core.Objects; +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; + +namespace IF.Lastfm.Core.Api.Commands.ChartApi +{ + internal class GetTopTracksCommand : GetAsyncCommandBase> + { + public GetTopTracksCommand(IAuth auth) + : base(auth) + { + Method = "chart.getTopTracks"; + } + + public override void SetParameters() + { + AddPagingParameters(); + DisableCaching(); + } + + public async override Task> HandleResponse(HttpResponseMessage response) + { + string json = await response.Content.ReadAsStringAsync(); + + LastFmApiError error; + if (LastFm.IsResponseValid(json, out error) && response.IsSuccessStatusCode) + { + var jtoken = JsonConvert.DeserializeObject(json); + + var tracks = jtoken.SelectToken("tracks") + .SelectToken("track").Children() + .Select(LastTrack.ParseJToken) + .ToList(); + + var pageresponse = PageResponse.CreateSuccessResponse(tracks); + + var attrToken = jtoken.SelectToken("tracks").SelectToken("@attr"); + pageresponse.AddPageInfoFromJToken(attrToken); + + return pageresponse; + } + else + { + return LastResponse.CreateErrorResponse>(error); + } + } + } +} diff --git a/src/IF.Lastfm.Core/Api/IChartApi.cs b/src/IF.Lastfm.Core/Api/IChartApi.cs new file mode 100644 index 0000000..552805e --- /dev/null +++ b/src/IF.Lastfm.Core/Api/IChartApi.cs @@ -0,0 +1,23 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using IF.Lastfm.Core.Api.Helpers; +using IF.Lastfm.Core.Objects; + +namespace IF.Lastfm.Core.Api +{ + public interface IChartApi + { + IAuth Auth { get; } + + Task> GetTopArtistAsync( + int page = 1, + int itemsPerPage = LastFm.DefaultPageLength); + + Task> GetTopTracksAsync( + int page = 1, + int itemsPerPage = LastFm.DefaultPageLength); + } +} diff --git a/src/IF.Lastfm.Core/IF.Lastfm.Core.csproj b/src/IF.Lastfm.Core/IF.Lastfm.Core.csproj index 81de446..02f36c6 100644 --- a/src/IF.Lastfm.Core/IF.Lastfm.Core.csproj +++ b/src/IF.Lastfm.Core/IF.Lastfm.Core.csproj @@ -40,17 +40,20 @@ + - - + + + + @@ -79,6 +82,7 @@ +