From 92aec324ef1af8cf445575b566a39c8d8454e957 Mon Sep 17 00:00:00 2001 From: Rikki Tooley Date: Tue, 23 Jul 2013 22:46:29 +0100 Subject: [PATCH] Tidied up command framework. --- .../Api/Commands/AlbumApi/GetAbumInfoCommand.cs | 6 ++---- .../Api/Commands/ArtistApi/GetArtistInfoCommand.cs | 6 ++---- IF.Lastfm.Core/Api/Commands/GetAsyncCommandBase.cs | 6 +++--- IF.Lastfm.Core/Api/Commands/IAsyncCommand.cs | 13 +++++++++++++ IF.Lastfm.Core/Api/Commands/IAsyncLastCommand.cs | 13 ------------- IF.Lastfm.Core/Api/Commands/PostAsyncCommandBase.cs | 12 +++++++++--- .../Api/Commands/TrackApi/GetTrackInfoCommand.cs | 6 ++---- .../Api/Commands/TrackApi/GetTrackShoutsCommand.cs | 6 ++---- .../Commands/UserApi/GetRecentScrobblesCommand.cs | 6 ++---- .../Api/Commands/UserApi/GetTopAlbumsCommand.cs | 6 ++---- IF.Lastfm.Core/IF.Lastfm.Core.csproj | 2 +- 11 files changed, 38 insertions(+), 44 deletions(-) create mode 100644 IF.Lastfm.Core/Api/Commands/IAsyncCommand.cs delete mode 100644 IF.Lastfm.Core/Api/Commands/IAsyncLastCommand.cs diff --git a/IF.Lastfm.Core/Api/Commands/AlbumApi/GetAbumInfoCommand.cs b/IF.Lastfm.Core/Api/Commands/AlbumApi/GetAbumInfoCommand.cs index 6fa534f..77bff37 100644 --- a/IF.Lastfm.Core/Api/Commands/AlbumApi/GetAbumInfoCommand.cs +++ b/IF.Lastfm.Core/Api/Commands/AlbumApi/GetAbumInfoCommand.cs @@ -25,7 +25,7 @@ public GetAlbumInfoCommand(IAuth auth, string artistname, string albumname) : ba AlbumName = albumname; } - public async override Task> ExecuteAsync() + public override Uri BuildRequestUrl() { var parameters = new Dictionary { @@ -35,9 +35,7 @@ public async override Task> ExecuteAsync() }; var apiUrl = LastFm.FormatApiUrl(Method, Auth.ApiKey, parameters); - Url = new Uri(apiUrl, UriKind.Absolute); - - return await ExecuteInternal(); + return new Uri(apiUrl, UriKind.Absolute); } public async override Task> HandleResponse(HttpResponseMessage response) diff --git a/IF.Lastfm.Core/Api/Commands/ArtistApi/GetArtistInfoCommand.cs b/IF.Lastfm.Core/Api/Commands/ArtistApi/GetArtistInfoCommand.cs index 9f126c5..3295fec 100644 --- a/IF.Lastfm.Core/Api/Commands/ArtistApi/GetArtistInfoCommand.cs +++ b/IF.Lastfm.Core/Api/Commands/ArtistApi/GetArtistInfoCommand.cs @@ -25,7 +25,7 @@ public GetArtistInfoCommand(IAuth auth, string artistname) ArtistName = artistname; } - public async override Task> ExecuteAsync() + public override Uri BuildRequestUrl() { var parameters = new Dictionary { @@ -34,9 +34,7 @@ public async override Task> ExecuteAsync() }; var apiUrl = LastFm.FormatApiUrl(Method, Auth.ApiKey, parameters); - Url = new Uri(apiUrl, UriKind.Absolute); - - return await ExecuteInternal(); + return new Uri(apiUrl, UriKind.Absolute); } public async override Task> HandleResponse(HttpResponseMessage response) diff --git a/IF.Lastfm.Core/Api/Commands/GetAsyncCommandBase.cs b/IF.Lastfm.Core/Api/Commands/GetAsyncCommandBase.cs index d15fc37..ac30122 100644 --- a/IF.Lastfm.Core/Api/Commands/GetAsyncCommandBase.cs +++ b/IF.Lastfm.Core/Api/Commands/GetAsyncCommandBase.cs @@ -5,7 +5,7 @@ namespace IF.Lastfm.Core.Api.Commands { - internal abstract class GetAsyncCommandBase : IAsyncLastCommand + internal abstract class GetAsyncCommandBase : IAsyncCommand { public string Method { get; protected set; } public Uri Url { get; protected set; } @@ -19,9 +19,9 @@ protected GetAsyncCommandBase(IAuth auth) Auth = auth; } - public abstract Task ExecuteAsync(); + public abstract Uri BuildRequestUrl(); - protected async Task ExecuteInternal() + public async Task ExecuteAsync() { var httpClient = new HttpClient(); var response = await httpClient.GetAsync(Url); diff --git a/IF.Lastfm.Core/Api/Commands/IAsyncCommand.cs b/IF.Lastfm.Core/Api/Commands/IAsyncCommand.cs new file mode 100644 index 0000000..c78069a --- /dev/null +++ b/IF.Lastfm.Core/Api/Commands/IAsyncCommand.cs @@ -0,0 +1,13 @@ +using System; +using System.Net.Http; +using System.Threading.Tasks; + +namespace IF.Lastfm.Core.Api.Commands +{ + internal interface IAsyncCommand + { + Uri BuildRequestUrl(); + Task ExecuteAsync(); + Task HandleResponse(HttpResponseMessage response); + } +} diff --git a/IF.Lastfm.Core/Api/Commands/IAsyncLastCommand.cs b/IF.Lastfm.Core/Api/Commands/IAsyncLastCommand.cs deleted file mode 100644 index 3bb6566..0000000 --- a/IF.Lastfm.Core/Api/Commands/IAsyncLastCommand.cs +++ /dev/null @@ -1,13 +0,0 @@ -using System.Text; -using System.Threading.Tasks; - -namespace IF.Lastfm.Core.Api.Commands -{ - internal interface IAsyncLastCommand - { - } - - internal interface IAsyncLastCommand - { - } -} diff --git a/IF.Lastfm.Core/Api/Commands/PostAsyncCommandBase.cs b/IF.Lastfm.Core/Api/Commands/PostAsyncCommandBase.cs index 7745d19..2538881 100644 --- a/IF.Lastfm.Core/Api/Commands/PostAsyncCommandBase.cs +++ b/IF.Lastfm.Core/Api/Commands/PostAsyncCommandBase.cs @@ -5,10 +5,10 @@ namespace IF.Lastfm.Core.Api.Commands { - internal abstract class PostAsyncCommandBase + internal abstract class PostAsyncCommandBase : IAsyncCommand { public string Method { get; protected set; } - public Uri Url { get; protected set; } + public Uri Url { get; private set; } public IAuth Auth { get; protected set; } public int Page { get; set; } @@ -17,13 +17,19 @@ internal abstract class PostAsyncCommandBase protected PostAsyncCommandBase(IAuth auth) { Auth = auth; - Url = new Uri(LastFm.ApiRoot, UriKind.Absolute); + } + + public Uri BuildRequestUrl() + { + return new Uri(LastFm.ApiRoot, UriKind.Absolute); } public abstract Task ExecuteAsync(); protected async Task ExecuteInternal(Dictionary parameters) { + Url = BuildRequestUrl(); + var apisig = Auth.GenerateMethodSignature(Method, parameters); var postContent = LastFm.CreatePostBody(Method, diff --git a/IF.Lastfm.Core/Api/Commands/TrackApi/GetTrackInfoCommand.cs b/IF.Lastfm.Core/Api/Commands/TrackApi/GetTrackInfoCommand.cs index 8c1de08..2503d69 100644 --- a/IF.Lastfm.Core/Api/Commands/TrackApi/GetTrackInfoCommand.cs +++ b/IF.Lastfm.Core/Api/Commands/TrackApi/GetTrackInfoCommand.cs @@ -27,7 +27,7 @@ public GetTrackInfoCommand(IAuth auth, string trackname, string artistname) ArtistName = artistname; } - public async override Task> ExecuteAsync() + public override Uri BuildRequestUrl() { var parameters = new Dictionary { @@ -42,9 +42,7 @@ public async override Task> ExecuteAsync() } var apiUrl = LastFm.FormatApiUrl(Method, Auth.ApiKey, parameters); - Url = new Uri(apiUrl, UriKind.Absolute); - - return await ExecuteInternal(); + return new Uri(apiUrl, UriKind.Absolute); } public async override Task> HandleResponse(HttpResponseMessage response) diff --git a/IF.Lastfm.Core/Api/Commands/TrackApi/GetTrackShoutsCommand.cs b/IF.Lastfm.Core/Api/Commands/TrackApi/GetTrackShoutsCommand.cs index becb49b..1aaa36a 100644 --- a/IF.Lastfm.Core/Api/Commands/TrackApi/GetTrackShoutsCommand.cs +++ b/IF.Lastfm.Core/Api/Commands/TrackApi/GetTrackShoutsCommand.cs @@ -25,7 +25,7 @@ public GetTrackShoutsCommand(IAuth auth, string trackname, string artistname) : ArtistName = artistname; } - public async override Task> ExecuteAsync() + public override Uri BuildRequestUrl() { var parameters = new Dictionary { @@ -37,9 +37,7 @@ public async override Task> ExecuteAsync() base.AddPagingParameters(parameters); var apiUrl = LastFm.FormatApiUrl(Method, Auth.ApiKey, parameters); - Url = new Uri(apiUrl, UriKind.Absolute); - - return await ExecuteInternal(); + return new Uri(apiUrl, UriKind.Absolute); } public async override Task> HandleResponse(HttpResponseMessage response) diff --git a/IF.Lastfm.Core/Api/Commands/UserApi/GetRecentScrobblesCommand.cs b/IF.Lastfm.Core/Api/Commands/UserApi/GetRecentScrobblesCommand.cs index ccf0b90..8ca8315 100644 --- a/IF.Lastfm.Core/Api/Commands/UserApi/GetRecentScrobblesCommand.cs +++ b/IF.Lastfm.Core/Api/Commands/UserApi/GetRecentScrobblesCommand.cs @@ -22,7 +22,7 @@ public GetRecentScrobblesCommand(IAuth auth, string username, DateTime from) : b From = from; } - public async override Task> ExecuteAsync() + public override Uri BuildRequestUrl() { var parameters = new Dictionary { @@ -33,9 +33,7 @@ public async override Task> ExecuteAsync() base.AddPagingParameters(parameters); var uristring = LastFm.FormatApiUrl(Method, Auth.ApiKey, parameters); - Url = new Uri(uristring, UriKind.Absolute); - - return await ExecuteInternal(); + return new Uri(uristring, UriKind.Absolute); } public async override Task> HandleResponse(HttpResponseMessage response) diff --git a/IF.Lastfm.Core/Api/Commands/UserApi/GetTopAlbumsCommand.cs b/IF.Lastfm.Core/Api/Commands/UserApi/GetTopAlbumsCommand.cs index 550600f..7792f8e 100644 --- a/IF.Lastfm.Core/Api/Commands/UserApi/GetTopAlbumsCommand.cs +++ b/IF.Lastfm.Core/Api/Commands/UserApi/GetTopAlbumsCommand.cs @@ -23,7 +23,7 @@ public GetTopAlbumsCommand(IAuth auth, string username, LastStatsTimeSpan span) TimeSpan = span; } - public async override Task> ExecuteAsync() + public override Uri BuildRequestUrl() { var parameters = new Dictionary { @@ -34,9 +34,7 @@ public async override Task> ExecuteAsync() base.AddPagingParameters(parameters); var uristring = LastFm.FormatApiUrl(Method, Auth.ApiKey, parameters); - Url = new Uri(uristring, UriKind.Absolute); - - return await ExecuteInternal(); + return new Uri(uristring, UriKind.Absolute); } public async override Task> HandleResponse(HttpResponseMessage response) diff --git a/IF.Lastfm.Core/IF.Lastfm.Core.csproj b/IF.Lastfm.Core/IF.Lastfm.Core.csproj index d6673f5..9e8b88a 100644 --- a/IF.Lastfm.Core/IF.Lastfm.Core.csproj +++ b/IF.Lastfm.Core/IF.Lastfm.Core.csproj @@ -53,7 +53,7 @@ - +