From 5134b6c2cc875e6fac218b01b622ee4924fe9762 Mon Sep 17 00:00:00 2001 From: Rikki Tooley Date: Tue, 23 Jul 2013 22:34:51 +0100 Subject: [PATCH] user.getRecentStations now uses the new PostAsyncCommand pattern --- .../Api/Commands/PostAsyncCommandBase.cs | 47 ++++++++++++++ .../UserApi/GetRecentStationsCommand.cs | 62 +++++++++++++++++++ IF.Lastfm.Core/Api/IUserApi.cs | 6 +- IF.Lastfm.Core/Api/UserApi.cs | 10 ++- IF.Lastfm.Core/IF.Lastfm.Core.csproj | 2 + .../UserApi/RecentStationsTestViewModel.cs | 2 +- 6 files changed, 125 insertions(+), 4 deletions(-) create mode 100644 IF.Lastfm.Core/Api/Commands/PostAsyncCommandBase.cs create mode 100644 IF.Lastfm.Core/Api/Commands/UserApi/GetRecentStationsCommand.cs diff --git a/IF.Lastfm.Core/Api/Commands/PostAsyncCommandBase.cs b/IF.Lastfm.Core/Api/Commands/PostAsyncCommandBase.cs new file mode 100644 index 0000000..7745d19 --- /dev/null +++ b/IF.Lastfm.Core/Api/Commands/PostAsyncCommandBase.cs @@ -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 + { + 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 ExecuteAsync(); + + protected async Task ExecuteInternal(Dictionary 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 HandleResponse(HttpResponseMessage response); + + protected void AddPagingParameters(Dictionary parameters) + { + parameters.Add("page", Page.ToString()); + parameters.Add("limit", Count.ToString()); + } + } +} \ No newline at end of file diff --git a/IF.Lastfm.Core/Api/Commands/UserApi/GetRecentStationsCommand.cs b/IF.Lastfm.Core/Api/Commands/UserApi/GetRecentStationsCommand.cs new file mode 100644 index 0000000..7f3bc63 --- /dev/null +++ b/IF.Lastfm.Core/Api/Commands/UserApi/GetRecentStationsCommand.cs @@ -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> + { + public string Username { get; private set; } + + public GetRecentStationsCommand(IAuth auth, string username) : base(auth) + { + Method = "user.getRecentStations"; + Username = username; + } + + public async override Task> ExecuteAsync() + { + var parameters = new Dictionary + { + {"user", Username}, + {"sk", Auth.User.Token} + }; + + AddPagingParameters(parameters); + + return await ExecuteInternal(parameters); + } + + public async override Task> HandleResponse(HttpResponseMessage response) + { + string json = await response.Content.ReadAsStringAsync(); + + LastFmApiError error; + if (LastFm.IsResponseValid(json, out error) && response.IsSuccessStatusCode) + { + JToken jtoken = JsonConvert.DeserializeObject(json).SelectToken("recentstations"); + + var stationsToken = jtoken.SelectToken("station"); + + var stations = stationsToken.Children().Select(Station.ParseJToken).ToList(); + + var pageresponse = PageResponse.CreateSuccessResponse(stations); + + var attrToken = jtoken.SelectToken("@attr"); + pageresponse.AddPageInfoFromJToken(attrToken); + + return pageresponse; + } + else + { + return PageResponse.CreateErrorResponse(error); + } + } + } +} \ No newline at end of file diff --git a/IF.Lastfm.Core/Api/IUserApi.cs b/IF.Lastfm.Core/Api/IUserApi.cs index 7009477..23c5138 100644 --- a/IF.Lastfm.Core/Api/IUserApi.cs +++ b/IF.Lastfm.Core/Api/IUserApi.cs @@ -15,11 +15,13 @@ Task> GetTopAlbums(string username, int startIndex = 0, int endIndex = LastFm.DefaultPageLength); - Task> GetRecentScrobbles(string username, DateTime since, + Task> GetRecentScrobbles(string username, + DateTime since, int startIndex = 0, int endIndex = LastFm.DefaultPageLength); - Task> GetRecentStations(int pagenumber, + Task> GetRecentStations(string username, + int pagenumber, int count = LastFm.DefaultPageLength); } } \ No newline at end of file diff --git a/IF.Lastfm.Core/Api/UserApi.cs b/IF.Lastfm.Core/Api/UserApi.cs index 4e90306..f8d8760 100644 --- a/IF.Lastfm.Core/Api/UserApi.cs +++ b/IF.Lastfm.Core/Api/UserApi.cs @@ -59,8 +59,16 @@ public async Task> GetRecentScrobbles(string username, DateT return await command.ExecuteAsync(); } - public async Task> GetRecentStations(int pagenumber = 0, int count = LastFm.DefaultPageLength) + public async Task> 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 diff --git a/IF.Lastfm.Core/IF.Lastfm.Core.csproj b/IF.Lastfm.Core/IF.Lastfm.Core.csproj index d301b39..d6673f5 100644 --- a/IF.Lastfm.Core/IF.Lastfm.Core.csproj +++ b/IF.Lastfm.Core/IF.Lastfm.Core.csproj @@ -44,9 +44,11 @@ + + diff --git a/IF.Lastfm.Demo.Apollo/ViewModels/UserApi/RecentStationsTestViewModel.cs b/IF.Lastfm.Demo.Apollo/ViewModels/UserApi/RecentStationsTestViewModel.cs index 0af0266..49f8e4a 100644 --- a/IF.Lastfm.Demo.Apollo/ViewModels/UserApi/RecentStationsTestViewModel.cs +++ b/IF.Lastfm.Demo.Apollo/ViewModels/UserApi/RecentStationsTestViewModel.cs @@ -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);