Add charts api

plus renaming some files
This commit is contained in:
Harry 2014-10-07 15:56:30 -04:00
parent 1bdcafbf9f
commit e1ad4f7a10
7 changed files with 182 additions and 2 deletions

View File

@ -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<PageResponse<LastArtist>> GetTopArtistAsync(int page = 1, int itemsPerPage = LastFm.DefaultPageLength)
{
var command = new GetTopArtistsCommand(Auth)
{
Page = page,
Count = itemsPerPage
};
return await command.ExecuteAsync();
}
public async Task<PageResponse<LastTrack>> GetTopTracksAsync(int page = 1, int itemsPerPage = LastFm.DefaultPageLength)
{
var command = new GetTopTracksCommand(Auth)
{
Page = page,
Count = itemsPerPage
};
return await command.ExecuteAsync();
}
}
}

View File

@ -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<PageResponse<LastArtist>>
{
public GetTopArtistsCommand(IAuth auth)
: base(auth)
{
Method = "chart.getTopArtists";
}
public override void SetParameters()
{
AddPagingParameters();
DisableCaching();
}
public async override Task<PageResponse<LastArtist>> HandleResponse(HttpResponseMessage response)
{
string json = await response.Content.ReadAsStringAsync();
LastFmApiError error;
if (LastFm.IsResponseValid(json, out error) && response.IsSuccessStatusCode)
{
var jtoken = JsonConvert.DeserializeObject<JToken>(json);
var artists = jtoken.SelectToken("artists")
.SelectToken("artist").Children()
.Select(LastArtist.ParseJToken)
.ToList();
var pageresponse = PageResponse<LastArtist>.CreateSuccessResponse(artists);
var attrToken = jtoken.SelectToken("artists").SelectToken("@attr");
pageresponse.AddPageInfoFromJToken(attrToken);
return pageresponse;
}
else
{
return LastResponse.CreateErrorResponse<PageResponse<LastArtist>>(error);
}
}
}
}

View File

@ -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<PageResponse<LastTrack>>
{
public GetTopTracksCommand(IAuth auth)
: base(auth)
{
Method = "chart.getTopTracks";
}
public override void SetParameters()
{
AddPagingParameters();
DisableCaching();
}
public async override Task<PageResponse<LastTrack>> HandleResponse(HttpResponseMessage response)
{
string json = await response.Content.ReadAsStringAsync();
LastFmApiError error;
if (LastFm.IsResponseValid(json, out error) && response.IsSuccessStatusCode)
{
var jtoken = JsonConvert.DeserializeObject<JToken>(json);
var tracks = jtoken.SelectToken("tracks")
.SelectToken("track").Children()
.Select(LastTrack.ParseJToken)
.ToList();
var pageresponse = PageResponse<LastTrack>.CreateSuccessResponse(tracks);
var attrToken = jtoken.SelectToken("tracks").SelectToken("@attr");
pageresponse.AddPageInfoFromJToken(attrToken);
return pageresponse;
}
else
{
return LastResponse.CreateErrorResponse<PageResponse<LastTrack>>(error);
}
}
}
}

View File

@ -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<PageResponse<LastArtist>> GetTopArtistAsync(
int page = 1,
int itemsPerPage = LastFm.DefaultPageLength);
Task<PageResponse<LastTrack>> GetTopTracksAsync(
int page = 1,
int itemsPerPage = LastFm.DefaultPageLength);
}
}

View File

@ -40,17 +40,20 @@
<Compile Include="Api\AlbumApi.cs" />
<Compile Include="Api\ArtistApi.cs" />
<Compile Include="Api\Auth.cs" />
<Compile Include="Api\ChartApi.cs" />
<Compile Include="Api\Commands\AlbumApi\AddShoutCommand.cs" />
<Compile Include="Api\Commands\AlbumApi\GetAlbumInfoCommand.cs" />
<Compile Include="Api\Commands\AlbumApi\GetAlbumShoutsCommand.cs" />
<Compile Include="Api\Commands\ArtistApi\AddShoutCommand.cs" />
<Compile Include="Api\Commands\ArtistApi\GetArtistInfoCommand.cs" />
<Compile Include="Api\Commands\ArtistApi\GetArtistShoutsCommand.cs" />
<Compile Include="Api\Commands\ArtistApi\GetArtistTopAlbums.cs" />
<Compile Include="Api\Commands\ArtistApi\GetArtistTopTracks.cs" />
<Compile Include="Api\Commands\ArtistApi\GetArtistTopAlbumsCommand.cs" />
<Compile Include="Api\Commands\ArtistApi\GetArtistTopTracksCommand.cs" />
<Compile Include="Api\Commands\ArtistApi\GetSimilarArtistsCommand.cs" />
<Compile Include="Api\Commands\ArtistApi\SearchArtistsCommand.cs" />
<Compile Include="Api\Commands\AuthApi\GetMobileSessionCommand.cs" />
<Compile Include="Api\Commands\ChartApi\GetTopArtistsCommand.cs" />
<Compile Include="Api\Commands\ChartApi\GetTopTracksCommand.cs" />
<Compile Include="Api\Commands\GetAsyncCommandBase.cs" />
<Compile Include="Api\Commands\IAsyncCommand.cs" />
<Compile Include="Api\Commands\LastAsyncCommandBase.cs" />
@ -79,6 +82,7 @@
<Compile Include="Api\IAlbumApi.cs" />
<Compile Include="Api\IArtistApi.cs" />
<Compile Include="Api\IAuth.cs" />
<Compile Include="Api\IChartApi.cs" />
<Compile Include="Api\ITrackApi.cs" />
<Compile Include="Api\IUserApi.cs" />
<Compile Include="Api\Scrobble.cs" />