Tidied up command framework.

This commit is contained in:
Rikki Tooley 2013-07-23 22:46:29 +01:00
parent 5134b6c2cc
commit 92aec324ef
11 changed files with 38 additions and 44 deletions

View File

@ -25,7 +25,7 @@ public GetAlbumInfoCommand(IAuth auth, string artistname, string albumname) : ba
AlbumName = albumname; AlbumName = albumname;
} }
public async override Task<LastResponse<Album>> ExecuteAsync() public override Uri BuildRequestUrl()
{ {
var parameters = new Dictionary<string, string> var parameters = new Dictionary<string, string>
{ {
@ -35,9 +35,7 @@ public async override Task<LastResponse<Album>> ExecuteAsync()
}; };
var apiUrl = LastFm.FormatApiUrl(Method, Auth.ApiKey, parameters); var apiUrl = LastFm.FormatApiUrl(Method, Auth.ApiKey, parameters);
Url = new Uri(apiUrl, UriKind.Absolute); return new Uri(apiUrl, UriKind.Absolute);
return await ExecuteInternal();
} }
public async override Task<LastResponse<Album>> HandleResponse(HttpResponseMessage response) public async override Task<LastResponse<Album>> HandleResponse(HttpResponseMessage response)

View File

@ -25,7 +25,7 @@ public GetArtistInfoCommand(IAuth auth, string artistname)
ArtistName = artistname; ArtistName = artistname;
} }
public async override Task<LastResponse<Artist>> ExecuteAsync() public override Uri BuildRequestUrl()
{ {
var parameters = new Dictionary<string, string> var parameters = new Dictionary<string, string>
{ {
@ -34,9 +34,7 @@ public async override Task<LastResponse<Artist>> ExecuteAsync()
}; };
var apiUrl = LastFm.FormatApiUrl(Method, Auth.ApiKey, parameters); var apiUrl = LastFm.FormatApiUrl(Method, Auth.ApiKey, parameters);
Url = new Uri(apiUrl, UriKind.Absolute); return new Uri(apiUrl, UriKind.Absolute);
return await ExecuteInternal();
} }
public async override Task<LastResponse<Artist>> HandleResponse(HttpResponseMessage response) public async override Task<LastResponse<Artist>> HandleResponse(HttpResponseMessage response)

View File

@ -5,7 +5,7 @@
namespace IF.Lastfm.Core.Api.Commands namespace IF.Lastfm.Core.Api.Commands
{ {
internal abstract class GetAsyncCommandBase<T> : IAsyncLastCommand<T> internal abstract class GetAsyncCommandBase<T> : IAsyncCommand<T>
{ {
public string Method { get; protected set; } public string Method { get; protected set; }
public Uri Url { get; protected set; } public Uri Url { get; protected set; }
@ -19,9 +19,9 @@ protected GetAsyncCommandBase(IAuth auth)
Auth = auth; Auth = auth;
} }
public abstract Task<T> ExecuteAsync(); public abstract Uri BuildRequestUrl();
protected async Task<T> ExecuteInternal() public async Task<T> ExecuteAsync()
{ {
var httpClient = new HttpClient(); var httpClient = new HttpClient();
var response = await httpClient.GetAsync(Url); var response = await httpClient.GetAsync(Url);

View File

@ -0,0 +1,13 @@
using System;
using System.Net.Http;
using System.Threading.Tasks;
namespace IF.Lastfm.Core.Api.Commands
{
internal interface IAsyncCommand<T>
{
Uri BuildRequestUrl();
Task<T> ExecuteAsync();
Task<T> HandleResponse(HttpResponseMessage response);
}
}

View File

@ -1,13 +0,0 @@
using System.Text;
using System.Threading.Tasks;
namespace IF.Lastfm.Core.Api.Commands
{
internal interface IAsyncLastCommand
{
}
internal interface IAsyncLastCommand<T>
{
}
}

View File

@ -5,10 +5,10 @@
namespace IF.Lastfm.Core.Api.Commands namespace IF.Lastfm.Core.Api.Commands
{ {
internal abstract class PostAsyncCommandBase<T> internal abstract class PostAsyncCommandBase<T> : IAsyncCommand<T>
{ {
public string Method { get; protected set; } 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 IAuth Auth { get; protected set; }
public int Page { get; set; } public int Page { get; set; }
@ -17,13 +17,19 @@ internal abstract class PostAsyncCommandBase<T>
protected PostAsyncCommandBase(IAuth auth) protected PostAsyncCommandBase(IAuth auth)
{ {
Auth = auth; Auth = auth;
Url = new Uri(LastFm.ApiRoot, UriKind.Absolute); }
public Uri BuildRequestUrl()
{
return new Uri(LastFm.ApiRoot, UriKind.Absolute);
} }
public abstract Task<T> ExecuteAsync(); public abstract Task<T> ExecuteAsync();
protected async Task<T> ExecuteInternal(Dictionary<string, string> parameters) protected async Task<T> ExecuteInternal(Dictionary<string, string> parameters)
{ {
Url = BuildRequestUrl();
var apisig = Auth.GenerateMethodSignature(Method, parameters); var apisig = Auth.GenerateMethodSignature(Method, parameters);
var postContent = LastFm.CreatePostBody(Method, var postContent = LastFm.CreatePostBody(Method,

View File

@ -27,7 +27,7 @@ public GetTrackInfoCommand(IAuth auth, string trackname, string artistname)
ArtistName = artistname; ArtistName = artistname;
} }
public async override Task<LastResponse<Track>> ExecuteAsync() public override Uri BuildRequestUrl()
{ {
var parameters = new Dictionary<string, string> var parameters = new Dictionary<string, string>
{ {
@ -42,9 +42,7 @@ public async override Task<LastResponse<Track>> ExecuteAsync()
} }
var apiUrl = LastFm.FormatApiUrl(Method, Auth.ApiKey, parameters); var apiUrl = LastFm.FormatApiUrl(Method, Auth.ApiKey, parameters);
Url = new Uri(apiUrl, UriKind.Absolute); return new Uri(apiUrl, UriKind.Absolute);
return await ExecuteInternal();
} }
public async override Task<LastResponse<Track>> HandleResponse(HttpResponseMessage response) public async override Task<LastResponse<Track>> HandleResponse(HttpResponseMessage response)

View File

@ -25,7 +25,7 @@ public GetTrackShoutsCommand(IAuth auth, string trackname, string artistname) :
ArtistName = artistname; ArtistName = artistname;
} }
public async override Task<PageResponse<Shout>> ExecuteAsync() public override Uri BuildRequestUrl()
{ {
var parameters = new Dictionary<string, string> var parameters = new Dictionary<string, string>
{ {
@ -37,9 +37,7 @@ public async override Task<PageResponse<Shout>> ExecuteAsync()
base.AddPagingParameters(parameters); base.AddPagingParameters(parameters);
var apiUrl = LastFm.FormatApiUrl(Method, Auth.ApiKey, parameters); var apiUrl = LastFm.FormatApiUrl(Method, Auth.ApiKey, parameters);
Url = new Uri(apiUrl, UriKind.Absolute); return new Uri(apiUrl, UriKind.Absolute);
return await ExecuteInternal();
} }
public async override Task<PageResponse<Shout>> HandleResponse(HttpResponseMessage response) public async override Task<PageResponse<Shout>> HandleResponse(HttpResponseMessage response)

View File

@ -22,7 +22,7 @@ public GetRecentScrobblesCommand(IAuth auth, string username, DateTime from) : b
From = from; From = from;
} }
public async override Task<PageResponse<Track>> ExecuteAsync() public override Uri BuildRequestUrl()
{ {
var parameters = new Dictionary<string, string> var parameters = new Dictionary<string, string>
{ {
@ -33,9 +33,7 @@ public async override Task<PageResponse<Track>> ExecuteAsync()
base.AddPagingParameters(parameters); base.AddPagingParameters(parameters);
var uristring = LastFm.FormatApiUrl(Method, Auth.ApiKey, parameters); var uristring = LastFm.FormatApiUrl(Method, Auth.ApiKey, parameters);
Url = new Uri(uristring, UriKind.Absolute); return new Uri(uristring, UriKind.Absolute);
return await ExecuteInternal();
} }
public async override Task<PageResponse<Track>> HandleResponse(HttpResponseMessage response) public async override Task<PageResponse<Track>> HandleResponse(HttpResponseMessage response)

View File

@ -23,7 +23,7 @@ public GetTopAlbumsCommand(IAuth auth, string username, LastStatsTimeSpan span)
TimeSpan = span; TimeSpan = span;
} }
public async override Task<PageResponse<Album>> ExecuteAsync() public override Uri BuildRequestUrl()
{ {
var parameters = new Dictionary<string, string> var parameters = new Dictionary<string, string>
{ {
@ -34,9 +34,7 @@ public async override Task<PageResponse<Album>> ExecuteAsync()
base.AddPagingParameters(parameters); base.AddPagingParameters(parameters);
var uristring = LastFm.FormatApiUrl(Method, Auth.ApiKey, parameters); var uristring = LastFm.FormatApiUrl(Method, Auth.ApiKey, parameters);
Url = new Uri(uristring, UriKind.Absolute); return new Uri(uristring, UriKind.Absolute);
return await ExecuteInternal();
} }
public async override Task<PageResponse<Album>> HandleResponse(HttpResponseMessage response) public async override Task<PageResponse<Album>> HandleResponse(HttpResponseMessage response)

View File

@ -53,7 +53,7 @@
<Compile Include="Api\Helpers\ApiExtensions.cs" /> <Compile Include="Api\Helpers\ApiExtensions.cs" />
<Compile Include="Api\Helpers\ApiNameAttribute.cs" /> <Compile Include="Api\Helpers\ApiNameAttribute.cs" />
<Compile Include="Api\Auth.cs" /> <Compile Include="Api\Auth.cs" />
<Compile Include="Api\Commands\IAsyncLastCommand.cs" /> <Compile Include="Api\Commands\IAsyncCommand.cs" />
<Compile Include="Api\Helpers\LastResponse.cs" /> <Compile Include="Api\Helpers\LastResponse.cs" />
<Compile Include="Api\Helpers\PageResponse.cs" /> <Compile Include="Api\Helpers\PageResponse.cs" />
<Compile Include="Api\IAlbumApi.cs" /> <Compile Include="Api\IAlbumApi.cs" />