user.getRecentStations now uses the new PostAsyncCommand pattern

This commit is contained in:
Rikki Tooley 2013-07-23 22:34:51 +01:00
parent 99a61e50f8
commit 5134b6c2cc
6 changed files with 125 additions and 4 deletions

View File

@ -0,0 +1,47 @@
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
namespace IF.Lastfm.Core.Api.Commands
{
internal abstract class PostAsyncCommandBase<T>
{
public string Method { get; protected set; }
public Uri Url { get; protected set; }
public IAuth Auth { get; protected set; }
public int Page { get; set; }
public int Count { get; set; }
protected PostAsyncCommandBase(IAuth auth)
{
Auth = auth;
Url = new Uri(LastFm.ApiRoot, UriKind.Absolute);
}
public abstract Task<T> ExecuteAsync();
protected async Task<T> ExecuteInternal(Dictionary<string, string> parameters)
{
var apisig = Auth.GenerateMethodSignature(Method, parameters);
var postContent = LastFm.CreatePostBody(Method,
Auth.ApiKey,
apisig,
parameters);
var httpClient = new HttpClient();
var response = await httpClient.PostAsync(Url, postContent);
return await HandleResponse(response);
}
public abstract Task<T> HandleResponse(HttpResponseMessage response);
protected void AddPagingParameters(Dictionary<string, string> parameters)
{
parameters.Add("page", Page.ToString());
parameters.Add("limit", Count.ToString());
}
}
}

View File

@ -0,0 +1,62 @@
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
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.UserApi
{
internal class GetRecentStationsCommand : PostAsyncCommandBase<PageResponse<Station>>
{
public string Username { get; private set; }
public GetRecentStationsCommand(IAuth auth, string username) : base(auth)
{
Method = "user.getRecentStations";
Username = username;
}
public async override Task<PageResponse<Station>> ExecuteAsync()
{
var parameters = new Dictionary<string, string>
{
{"user", Username},
{"sk", Auth.User.Token}
};
AddPagingParameters(parameters);
return await ExecuteInternal(parameters);
}
public async override Task<PageResponse<Station>> HandleResponse(HttpResponseMessage response)
{
string json = await response.Content.ReadAsStringAsync();
LastFmApiError error;
if (LastFm.IsResponseValid(json, out error) && response.IsSuccessStatusCode)
{
JToken jtoken = JsonConvert.DeserializeObject<JToken>(json).SelectToken("recentstations");
var stationsToken = jtoken.SelectToken("station");
var stations = stationsToken.Children().Select(Station.ParseJToken).ToList();
var pageresponse = PageResponse<Station>.CreateSuccessResponse(stations);
var attrToken = jtoken.SelectToken("@attr");
pageresponse.AddPageInfoFromJToken(attrToken);
return pageresponse;
}
else
{
return PageResponse<Station>.CreateErrorResponse(error);
}
}
}
}

View File

@ -15,11 +15,13 @@ Task<PageResponse<Album>> GetTopAlbums(string username,
int startIndex = 0,
int endIndex = LastFm.DefaultPageLength);
Task<PageResponse<Track>> GetRecentScrobbles(string username, DateTime since,
Task<PageResponse<Track>> GetRecentScrobbles(string username,
DateTime since,
int startIndex = 0,
int endIndex = LastFm.DefaultPageLength);
Task<PageResponse<Station>> GetRecentStations(int pagenumber,
Task<PageResponse<Station>> GetRecentStations(string username,
int pagenumber,
int count = LastFm.DefaultPageLength);
}
}

View File

@ -59,8 +59,16 @@ public async Task<PageResponse<Track>> GetRecentScrobbles(string username, DateT
return await command.ExecuteAsync();
}
public async Task<PageResponse<Station>> GetRecentStations(int pagenumber = 0, int count = LastFm.DefaultPageLength)
public async Task<PageResponse<Station>> GetRecentStations(string username, int pagenumber = 0, int count = LastFm.DefaultPageLength)
{
var command = new GetRecentStationsCommand(Auth, username)
{
Page = pagenumber,
Count = count
};
return await command.ExecuteAsync();
const string apiMethod = "user.getRecentStations";
var methodParameters = new Dictionary<string, string>

View File

@ -44,9 +44,11 @@
<Compile Include="Api\Commands\AlbumApi\GetAbumInfoCommand.cs" />
<Compile Include="Api\Commands\ArtistApi\GetArtistInfoCommand.cs" />
<Compile Include="Api\Commands\GetAsyncCommandBase.cs" />
<Compile Include="Api\Commands\PostAsyncCommandBase.cs" />
<Compile Include="Api\Commands\TrackApi\GetTrackInfoCommand.cs" />
<Compile Include="Api\Commands\TrackApi\GetTrackShoutsCommand.cs" />
<Compile Include="Api\Commands\UserApi\GetRecentScrobblesCommand.cs" />
<Compile Include="Api\Commands\UserApi\GetRecentStationsCommand.cs" />
<Compile Include="Api\Commands\UserApi\GetTopAlbumsCommand.cs" />
<Compile Include="Api\Helpers\ApiExtensions.cs" />
<Compile Include="Api\Helpers\ApiNameAttribute.cs" />

View File

@ -101,7 +101,7 @@ public async Task GetRecentStations()
var userApi = new Core.Api.UserApi(Auth);
var response = await userApi.GetRecentStations(_stationPageProgress.ExpectedPage, 5);
var response = await userApi.GetRecentStations(Auth.User.Username, _stationPageProgress.ExpectedPage, 5);
_stationPageProgress.PageLoaded(response.Success);