From db00dfd8edd6b22fd7ca7c9033054cd13dc8c3d5 Mon Sep 17 00:00:00 2001 From: Rikki Tooley Date: Sun, 1 Sep 2013 20:52:44 +0100 Subject: [PATCH] Changed command structure to reduce repetition --- .../Api/Commands/AlbumApi/AddShoutCommand.cs | 13 ++---- .../Commands/AlbumApi/GetAlbumInfoCommand.cs | 16 +++---- .../AlbumApi/GetAlbumShoutsCommand.cs | 20 +++------ .../Api/Commands/ArtistApi/AddShoutCommand.cs | 11 ++--- .../ArtistApi/GetArtistInfoCommand.cs | 17 +++----- .../ArtistApi/GetArtistShoutsCommand.cs | 24 +++-------- .../Api/Commands/GetAsyncCommandBase.cs | 41 +++++++----------- IF.Lastfm.Core/Api/Commands/IAsyncCommand.cs | 6 +-- .../Api/Commands/LastAsyncCommandBase.cs | 43 +++++++++++++++++++ .../Api/Commands/PostAsyncCommandBase.cs | 31 ++++--------- .../Api/Commands/TrackApi/AddShoutCommand.cs | 16 +++---- .../Commands/TrackApi/GetTrackInfoCommand.cs | 21 +++------ .../TrackApi/GetTrackShoutsCommand.cs | 21 +++------ .../Api/Commands/TrackApi/LoveTrackCommand.cs | 11 ++--- .../Commands/TrackApi/UnloveTrackCommand.cs | 11 ++--- .../Api/Commands/UserApi/AddShoutCommand.cs | 16 ++----- .../UserApi/GetRecentScrobblesCommand.cs | 16 +++---- .../UserApi/GetRecentStationsCommand.cs | 11 ++--- .../Commands/UserApi/GetTopAlbumsCommand.cs | 16 +++---- .../Commands/UserApi/GetUserInfoCommand.cs | 14 ++---- .../Commands/UserApi/GetUserShoutsCommand.cs | 16 ++----- IF.Lastfm.Core/IF.Lastfm.Core.csproj | 1 + IF.Lastfm.Core/LastFm.cs | 1 + 23 files changed, 150 insertions(+), 243 deletions(-) create mode 100644 IF.Lastfm.Core/Api/Commands/LastAsyncCommandBase.cs diff --git a/IF.Lastfm.Core/Api/Commands/AlbumApi/AddShoutCommand.cs b/IF.Lastfm.Core/Api/Commands/AlbumApi/AddShoutCommand.cs index 0193d27..a958c74 100644 --- a/IF.Lastfm.Core/Api/Commands/AlbumApi/AddShoutCommand.cs +++ b/IF.Lastfm.Core/Api/Commands/AlbumApi/AddShoutCommand.cs @@ -19,16 +19,11 @@ public AddShoutCommand(IAuth auth, string album, string artist, string message) Message = message; } - public async override Task ExecuteAsync() + public override void SetParameters() { - var parameters = new Dictionary - { - {"album", Album}, - {"artist", Artist}, - {"message", Message} - }; - - return await ExecuteInternal(parameters); + Parameters.Add("album", Album); + Parameters.Add("artist", Artist); + Parameters.Add("message", Message); } public async override Task HandleResponse(HttpResponseMessage response) diff --git a/IF.Lastfm.Core/Api/Commands/AlbumApi/GetAlbumInfoCommand.cs b/IF.Lastfm.Core/Api/Commands/AlbumApi/GetAlbumInfoCommand.cs index c7f5903..1f43add 100644 --- a/IF.Lastfm.Core/Api/Commands/AlbumApi/GetAlbumInfoCommand.cs +++ b/IF.Lastfm.Core/Api/Commands/AlbumApi/GetAlbumInfoCommand.cs @@ -23,19 +23,13 @@ public GetAlbumInfoCommand(IAuth auth, string artistname, string albumname) : ba AlbumName = albumname; } - public override Uri BuildRequestUrl() + public override void SetParameters() { - var parameters = new Dictionary - { - {"artist", Uri.EscapeDataString(ArtistName)}, - {"album", Uri.EscapeDataString(AlbumName)}, - {"autocorrect", Convert.ToInt32(Autocorrect).ToString()} - }; + Parameters.Add("artist", ArtistName); + Parameters.Add("album", AlbumName); + Parameters.Add("autocorrect", Convert.ToInt32(Autocorrect).ToString()); - base.DisableCaching(parameters); - - var apiUrl = LastFm.FormatApiUrl(Method, Auth.ApiKey, parameters); - return new Uri(apiUrl, UriKind.Absolute); + base.DisableCaching(); } public async override Task> HandleResponse(HttpResponseMessage response) diff --git a/IF.Lastfm.Core/Api/Commands/AlbumApi/GetAlbumShoutsCommand.cs b/IF.Lastfm.Core/Api/Commands/AlbumApi/GetAlbumShoutsCommand.cs index d5d1cbe..de8ea84 100644 --- a/IF.Lastfm.Core/Api/Commands/AlbumApi/GetAlbumShoutsCommand.cs +++ b/IF.Lastfm.Core/Api/Commands/AlbumApi/GetAlbumShoutsCommand.cs @@ -25,22 +25,16 @@ public GetAlbumShoutsCommand(IAuth auth, string albumname, string artistname) ArtistName = artistname; } - public override Uri BuildRequestUrl() + public override void SetParameters() { - var parameters = new Dictionary - { - {"album", Uri.EscapeDataString(AlbumName)}, - {"artist", Uri.EscapeDataString(ArtistName)}, - {"autocorrect", Convert.ToInt32(Autocorrect).ToString()} - }; + Parameters.Add("album", AlbumName); + Parameters.Add("artist", ArtistName); + Parameters.Add("autocorrect", Convert.ToInt32(Autocorrect).ToString()); - base.AddPagingParameters(parameters); - base.DisableCaching(parameters); - - var apiUrl = LastFm.FormatApiUrl(Method, Auth.ApiKey, parameters); - return new Uri(apiUrl, UriKind.Absolute); + base.AddPagingParameters(); + base.DisableCaching(); } - + public async override Task> HandleResponse(HttpResponseMessage response) { string json = await response.Content.ReadAsStringAsync(); diff --git a/IF.Lastfm.Core/Api/Commands/ArtistApi/AddShoutCommand.cs b/IF.Lastfm.Core/Api/Commands/ArtistApi/AddShoutCommand.cs index 3960bc2..c89e505 100644 --- a/IF.Lastfm.Core/Api/Commands/ArtistApi/AddShoutCommand.cs +++ b/IF.Lastfm.Core/Api/Commands/ArtistApi/AddShoutCommand.cs @@ -17,15 +17,10 @@ public AddShoutCommand(IAuth auth, string artist, string message) : base(auth) Message = message; } - public async override Task ExecuteAsync() + public override void SetParameters() { - var parameters = new Dictionary - { - {"artist", Artist}, - {"message", Message} - }; - - return await ExecuteInternal(parameters); + Parameters.Add("artist", Artist); + Parameters.Add("message", Message); } 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 801a114..8b80de5 100644 --- a/IF.Lastfm.Core/Api/Commands/ArtistApi/GetArtistInfoCommand.cs +++ b/IF.Lastfm.Core/Api/Commands/ArtistApi/GetArtistInfoCommand.cs @@ -24,18 +24,15 @@ public GetArtistInfoCommand(IAuth auth, string artistname) ArtistName = artistname; } - public override Uri BuildRequestUrl() + /// + /// TODO Bio language + /// + public override void SetParameters() { - var parameters = new Dictionary - { - {"artist", Uri.EscapeDataString(ArtistName)}, - {"autocorrect", Convert.ToInt32(Autocorrect).ToString()} - }; + Parameters.Add("artist", ArtistName); + Parameters.Add("autocorrect", Convert.ToInt32(Autocorrect).ToString()); - base.DisableCaching(parameters); - - var apiUrl = LastFm.FormatApiUrl(Method, Auth.ApiKey, parameters); - return new Uri(apiUrl, UriKind.Absolute); + base.DisableCaching(); } public async override Task> HandleResponse(HttpResponseMessage response) diff --git a/IF.Lastfm.Core/Api/Commands/ArtistApi/GetArtistShoutsCommand.cs b/IF.Lastfm.Core/Api/Commands/ArtistApi/GetArtistShoutsCommand.cs index c5c8fa2..74f0eae 100644 --- a/IF.Lastfm.Core/Api/Commands/ArtistApi/GetArtistShoutsCommand.cs +++ b/IF.Lastfm.Core/Api/Commands/ArtistApi/GetArtistShoutsCommand.cs @@ -1,6 +1,4 @@ using System; -using System.Collections.Generic; -using System.Linq; using System.Net.Http; using System.Threading.Tasks; using IF.Lastfm.Core.Api.Enums; @@ -11,7 +9,7 @@ namespace IF.Lastfm.Core.Api.Commands.ArtistApi { - internal class GetArtistShoutsCommand : GetAsyncCommandBase> + public class GetArtistShoutsCommand : GetAsyncCommandBase> { public string ArtistName { get; set; } public bool Autocorrect { get; set; } @@ -23,22 +21,16 @@ public GetArtistShoutsCommand(IAuth auth, string artistname) ArtistName = artistname; } - public override Uri BuildRequestUrl() + public override void SetParameters() { - var parameters = new Dictionary - { - {"artist", Uri.EscapeDataString(ArtistName)}, - {"autocorrect", Convert.ToInt32(Autocorrect).ToString()} - }; + Parameters.Add("artist", ArtistName); + Parameters.Add("autocorrect", Convert.ToInt32(Autocorrect).ToString()); - base.AddPagingParameters(parameters); - base.DisableCaching(parameters); - - var apiUrl = LastFm.FormatApiUrl(Method, Auth.ApiKey, parameters); - return new Uri(apiUrl, UriKind.Absolute); + base.AddPagingParameters(); + base.DisableCaching(); } - public async override Task> HandleResponse(HttpResponseMessage response) + public override async Task> HandleResponse(HttpResponseMessage response) { string json = await response.Content.ReadAsStringAsync(); @@ -54,7 +46,5 @@ public async override Task> HandleResponse(HttpResponseMessa return PageResponse.CreateErrorResponse(error); } } - - } } \ No newline at end of file diff --git a/IF.Lastfm.Core/Api/Commands/GetAsyncCommandBase.cs b/IF.Lastfm.Core/Api/Commands/GetAsyncCommandBase.cs index 6d01777..7130600 100644 --- a/IF.Lastfm.Core/Api/Commands/GetAsyncCommandBase.cs +++ b/IF.Lastfm.Core/Api/Commands/GetAsyncCommandBase.cs @@ -1,29 +1,24 @@ using System; using System.Collections.Generic; +using System.Linq; using System.Net.Http; -using System.Net.Http.Headers; using System.Threading.Tasks; namespace IF.Lastfm.Core.Api.Commands { - internal abstract class GetAsyncCommandBase : IAsyncCommand - { - 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; } - + public abstract class GetAsyncCommandBase : LastAsyncCommandBase + { protected GetAsyncCommandBase(IAuth auth) { Auth = auth; } - public abstract Uri BuildRequestUrl(); - - public async Task ExecuteAsync() + public async override Task ExecuteAsync() { + SetParameters(); + + EscapeParameters(); + Url = BuildRequestUrl(); var httpClient = new HttpClient(); @@ -31,22 +26,18 @@ public async Task ExecuteAsync() return await HandleResponse(response); } - public abstract Task HandleResponse(HttpResponseMessage response); - - protected void AddPagingParameters(Dictionary parameters) + protected override Uri BuildRequestUrl() { - parameters.Add("page", Page.ToString()); - parameters.Add("limit", Count.ToString()); + var apiUrl = LastFm.FormatApiUrl(Method, Auth.ApiKey, Parameters); + return new Uri(apiUrl, UriKind.Absolute); } - /// - /// Annoying workaround for Windows Phone's caching... - /// see http://stackoverflow.com/questions/6334788/windows-phone-7-webrequest-caching - /// - /// - protected void DisableCaching(Dictionary parameters) + private void EscapeParameters() { - parameters.Add("disablecachetoken", DateTime.UtcNow.Ticks.ToString()); + foreach (var key in Parameters.Keys.ToList()) + { + Parameters[key] = Uri.EscapeDataString(Parameters[key]); + } } } } \ No newline at end of file diff --git a/IF.Lastfm.Core/Api/Commands/IAsyncCommand.cs b/IF.Lastfm.Core/Api/Commands/IAsyncCommand.cs index c78069a..888ea42 100644 --- a/IF.Lastfm.Core/Api/Commands/IAsyncCommand.cs +++ b/IF.Lastfm.Core/Api/Commands/IAsyncCommand.cs @@ -1,13 +1,9 @@ -using System; -using System.Net.Http; -using System.Threading.Tasks; +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/LastAsyncCommandBase.cs b/IF.Lastfm.Core/Api/Commands/LastAsyncCommandBase.cs new file mode 100644 index 0000000..3b3d9e2 --- /dev/null +++ b/IF.Lastfm.Core/Api/Commands/LastAsyncCommandBase.cs @@ -0,0 +1,43 @@ +using System; +using System.Collections.Generic; +using System.Net.Http; +using System.Threading.Tasks; + +namespace IF.Lastfm.Core.Api.Commands +{ + public abstract class LastAsyncCommandBase : IAsyncCommand + { + 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 Dictionary Parameters { get; set; } + + protected LastAsyncCommandBase() + { + Parameters = new Dictionary(); + } + + public abstract void SetParameters(); + protected abstract Uri BuildRequestUrl(); + public abstract Task ExecuteAsync(); + public abstract Task HandleResponse(HttpResponseMessage response); + + protected void AddPagingParameters() + { + Parameters.Add("page", Page.ToString()); + Parameters.Add("limit", Count.ToString()); + } + + /// + /// Annoying workaround for Windows Phone's caching... + /// see http://stackoverflow.com/questions/6334788/windows-phone-7-webrequest-caching + /// + protected void DisableCaching() + { + Parameters.Add("disablecachetoken", DateTime.UtcNow.Ticks.ToString()); + } + } +} \ No newline at end of file diff --git a/IF.Lastfm.Core/Api/Commands/PostAsyncCommandBase.cs b/IF.Lastfm.Core/Api/Commands/PostAsyncCommandBase.cs index 7d7142c..1572c62 100644 --- a/IF.Lastfm.Core/Api/Commands/PostAsyncCommandBase.cs +++ b/IF.Lastfm.Core/Api/Commands/PostAsyncCommandBase.cs @@ -5,51 +5,36 @@ namespace IF.Lastfm.Core.Api.Commands { - internal abstract class PostAsyncCommandBase : IAsyncCommand + internal abstract class PostAsyncCommandBase : LastAsyncCommandBase { - public string Method { get; protected set; } - public Uri Url { get; private set; } - public IAuth Auth { get; protected set; } - - public int Page { get; set; } - public int Count { get; set; } - protected PostAsyncCommandBase(IAuth auth) { Auth = auth; } - public Uri BuildRequestUrl() + protected override Uri BuildRequestUrl() { return new Uri(LastFm.ApiRoot, UriKind.Absolute); } - public abstract Task ExecuteAsync(); - - protected async Task ExecuteInternal(Dictionary parameters) + public override async Task ExecuteAsync() { + SetParameters(); + Url = BuildRequestUrl(); - parameters.Add("sk", Auth.User.Token); + Parameters.Add("sk", Auth.User.Token); - var apisig = Auth.GenerateMethodSignature(Method, parameters); + var apisig = Auth.GenerateMethodSignature(Method, Parameters); var postContent = LastFm.CreatePostBody(Method, Auth.ApiKey, apisig, - parameters); + 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/TrackApi/AddShoutCommand.cs b/IF.Lastfm.Core/Api/Commands/TrackApi/AddShoutCommand.cs index 75e1a5c..d77280f 100644 --- a/IF.Lastfm.Core/Api/Commands/TrackApi/AddShoutCommand.cs +++ b/IF.Lastfm.Core/Api/Commands/TrackApi/AddShoutCommand.cs @@ -1,5 +1,4 @@ -using System.Collections.Generic; -using System.Net.Http; +using System.Net.Http; using System.Threading.Tasks; using IF.Lastfm.Core.Api.Helpers; @@ -19,16 +18,11 @@ public AddShoutCommand(IAuth auth, string track, string artist, string message) Message = message; } - public async override Task ExecuteAsync() + public override void SetParameters() { - var parameters = new Dictionary - { - {"track", Track}, - {"artist", Artist}, - {"message", Message} - }; - - return await ExecuteInternal(parameters); + Parameters.Add("track", Track); + Parameters.Add("artist", Artist); + Parameters.Add("message", Message); } public async override Task HandleResponse(HttpResponseMessage response) diff --git a/IF.Lastfm.Core/Api/Commands/TrackApi/GetTrackInfoCommand.cs b/IF.Lastfm.Core/Api/Commands/TrackApi/GetTrackInfoCommand.cs index 7990335..e984cff 100644 --- a/IF.Lastfm.Core/Api/Commands/TrackApi/GetTrackInfoCommand.cs +++ b/IF.Lastfm.Core/Api/Commands/TrackApi/GetTrackInfoCommand.cs @@ -1,8 +1,5 @@ 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; @@ -27,24 +24,16 @@ public GetTrackInfoCommand(IAuth auth, string trackname, string artistname) ArtistName = artistname; } - public override Uri BuildRequestUrl() + public override void SetParameters() { - var parameters = new Dictionary - { - {"track", Uri.EscapeDataString(TrackName)}, - {"artist", Uri.EscapeDataString(ArtistName)}, - {"autocorrect", Convert.ToInt32(Autocorrect).ToString()} - }; + Parameters.Add("track", TrackName); + Parameters.Add("artist", ArtistName); + Parameters.Add("autocorrect", Convert.ToInt32(Autocorrect).ToString()); if (!string.IsNullOrWhiteSpace(Username)) { - parameters.Add("username", Username); + Parameters.Add("username", Username); } - - DisableCaching(parameters); - - var apiUrl = LastFm.FormatApiUrl(Method, Auth.ApiKey, parameters); - 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 7da6a5d..bceb6cd 100644 --- a/IF.Lastfm.Core/Api/Commands/TrackApi/GetTrackShoutsCommand.cs +++ b/IF.Lastfm.Core/Api/Commands/TrackApi/GetTrackShoutsCommand.cs @@ -1,8 +1,5 @@ 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; @@ -25,20 +22,14 @@ public GetTrackShoutsCommand(IAuth auth, string trackname, string artistname) : ArtistName = artistname; } - public override Uri BuildRequestUrl() + public override void SetParameters() { - var parameters = new Dictionary - { - {"track", Uri.EscapeDataString(TrackName)}, - {"artist", Uri.EscapeDataString(ArtistName)}, - {"autocorrect", Convert.ToInt32(Autocorrect).ToString()} - }; + Parameters.Add("track", TrackName); + Parameters.Add("artist", ArtistName); + Parameters.Add("autocorrect", Convert.ToInt32(Autocorrect).ToString()); - base.AddPagingParameters(parameters); - base.DisableCaching(parameters); - - var apiUrl = LastFm.FormatApiUrl(Method, Auth.ApiKey, parameters); - return new Uri(apiUrl, UriKind.Absolute); + AddPagingParameters(); + DisableCaching(); } public async override Task> HandleResponse(HttpResponseMessage response) diff --git a/IF.Lastfm.Core/Api/Commands/TrackApi/LoveTrackCommand.cs b/IF.Lastfm.Core/Api/Commands/TrackApi/LoveTrackCommand.cs index 8d7fc01..ffc8ad1 100644 --- a/IF.Lastfm.Core/Api/Commands/TrackApi/LoveTrackCommand.cs +++ b/IF.Lastfm.Core/Api/Commands/TrackApi/LoveTrackCommand.cs @@ -20,15 +20,10 @@ public LoveTrackCommand(IAuth auth, string trackname, string artistname) ArtistName = artistname; } - public async override Task ExecuteAsync() + public override void SetParameters() { - var parameters = new Dictionary - { - {"track", TrackName}, - {"artist", ArtistName} - }; - - return await ExecuteInternal(parameters); + Parameters.Add("track", TrackName); + Parameters.Add("artist", ArtistName); } public async override Task HandleResponse(HttpResponseMessage response) diff --git a/IF.Lastfm.Core/Api/Commands/TrackApi/UnloveTrackCommand.cs b/IF.Lastfm.Core/Api/Commands/TrackApi/UnloveTrackCommand.cs index bcf1b46..c5fe2ac 100644 --- a/IF.Lastfm.Core/Api/Commands/TrackApi/UnloveTrackCommand.cs +++ b/IF.Lastfm.Core/Api/Commands/TrackApi/UnloveTrackCommand.cs @@ -20,15 +20,10 @@ public UnloveTrackCommand(IAuth auth, string trackname, string artistname) ArtistName = artistname; } - public async override Task ExecuteAsync() + public override void SetParameters() { - var parameters = new Dictionary - { - {"track", TrackName}, - {"artist", ArtistName} - }; - - return await ExecuteInternal(parameters); + Parameters.Add("track", TrackName); + Parameters.Add("artist", ArtistName); } public async override Task HandleResponse(HttpResponseMessage response) diff --git a/IF.Lastfm.Core/Api/Commands/UserApi/AddShoutCommand.cs b/IF.Lastfm.Core/Api/Commands/UserApi/AddShoutCommand.cs index 8ccf610..6c7c0d6 100644 --- a/IF.Lastfm.Core/Api/Commands/UserApi/AddShoutCommand.cs +++ b/IF.Lastfm.Core/Api/Commands/UserApi/AddShoutCommand.cs @@ -1,8 +1,5 @@ -using System; -using System.Collections.Generic; -using System.Net.Http; +using System.Net.Http; using System.Threading.Tasks; -using IF.Lastfm.Core.Api.Enums; using IF.Lastfm.Core.Api.Helpers; namespace IF.Lastfm.Core.Api.Commands.UserApi @@ -19,15 +16,10 @@ public AddShoutCommand(IAuth auth, string recipient, string message) : base(auth Message = message; } - public async override Task ExecuteAsync() + public override void SetParameters() { - var parameters = new Dictionary - { - {"user", Recipient}, - {"message", Message} - }; - - return await ExecuteInternal(parameters); + Parameters.Add("user", Recipient); + Parameters.Add("message", Message); } 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 b1f5726..5b378d4 100644 --- a/IF.Lastfm.Core/Api/Commands/UserApi/GetRecentScrobblesCommand.cs +++ b/IF.Lastfm.Core/Api/Commands/UserApi/GetRecentScrobblesCommand.cs @@ -22,19 +22,13 @@ public GetRecentScrobblesCommand(IAuth auth, string username, DateTime from) : b From = from; } - public override Uri BuildRequestUrl() + public override void SetParameters() { - var parameters = new Dictionary - { - {"user", Uri.EscapeDataString(Username)}, - {"from", From.ToUnixTimestamp().ToString()} - }; + Parameters.Add("user", Username); + Parameters.Add("from", From.ToUnixTimestamp().ToString()); - base.AddPagingParameters(parameters); - base.DisableCaching(parameters); - - var uristring = LastFm.FormatApiUrl(Method, Auth.ApiKey, parameters); - return new Uri(uristring, UriKind.Absolute); + AddPagingParameters(); + DisableCaching(); } public async override Task> HandleResponse(HttpResponseMessage response) diff --git a/IF.Lastfm.Core/Api/Commands/UserApi/GetRecentStationsCommand.cs b/IF.Lastfm.Core/Api/Commands/UserApi/GetRecentStationsCommand.cs index 508e797..79bf1ff 100644 --- a/IF.Lastfm.Core/Api/Commands/UserApi/GetRecentStationsCommand.cs +++ b/IF.Lastfm.Core/Api/Commands/UserApi/GetRecentStationsCommand.cs @@ -21,16 +21,11 @@ public GetRecentStationsCommand(IAuth auth, string username) : base(auth) Username = username; } - public async override Task> ExecuteAsync() + public override void SetParameters() { - var parameters = new Dictionary - { - {"user", Username} - }; + Parameters.Add("user", Username); - AddPagingParameters(parameters); - - return await ExecuteInternal(parameters); + AddPagingParameters(); } 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 3b8016c..d5e85da 100644 --- a/IF.Lastfm.Core/Api/Commands/UserApi/GetTopAlbumsCommand.cs +++ b/IF.Lastfm.Core/Api/Commands/UserApi/GetTopAlbumsCommand.cs @@ -23,19 +23,13 @@ public GetTopAlbumsCommand(IAuth auth, string username, LastStatsTimeSpan span) TimeSpan = span; } - public override Uri BuildRequestUrl() + public override void SetParameters() { - var parameters = new Dictionary - { - {"username", Uri.EscapeDataString(Username)}, - {"period", TimeSpan.GetApiName()} - }; + Parameters.Add("username", Username); + Parameters.Add("period", TimeSpan.GetApiName()); - base.AddPagingParameters(parameters); - base.DisableCaching(parameters); - - var uristring = LastFm.FormatApiUrl(Method, Auth.ApiKey, parameters); - return new Uri(uristring, UriKind.Absolute); + AddPagingParameters(); + DisableCaching(); } public async override Task> HandleResponse(HttpResponseMessage response) diff --git a/IF.Lastfm.Core/Api/Commands/UserApi/GetUserInfoCommand.cs b/IF.Lastfm.Core/Api/Commands/UserApi/GetUserInfoCommand.cs index ce0e71c..ac6a21a 100644 --- a/IF.Lastfm.Core/Api/Commands/UserApi/GetUserInfoCommand.cs +++ b/IF.Lastfm.Core/Api/Commands/UserApi/GetUserInfoCommand.cs @@ -20,19 +20,13 @@ public GetUserInfoCommand(IAuth auth, string username) : base(auth) Username = username; } - public override Uri BuildRequestUrl() + public override void SetParameters() { - var parameters = new Dictionary - { - {"user", Uri.EscapeDataString(Username)} - }; + Parameters.Add("user", Username); - base.DisableCaching(parameters); - - var uristring = LastFm.FormatApiUrl(Method, Auth.ApiKey, parameters); - return new Uri(uristring, UriKind.Absolute); + DisableCaching(); } - + public async override Task> HandleResponse(HttpResponseMessage response) { string json = await response.Content.ReadAsStringAsync(); diff --git a/IF.Lastfm.Core/Api/Commands/UserApi/GetUserShoutsCommand.cs b/IF.Lastfm.Core/Api/Commands/UserApi/GetUserShoutsCommand.cs index 254680e..0d67a04 100644 --- a/IF.Lastfm.Core/Api/Commands/UserApi/GetUserShoutsCommand.cs +++ b/IF.Lastfm.Core/Api/Commands/UserApi/GetUserShoutsCommand.cs @@ -1,5 +1,3 @@ -using System; -using System.Collections.Generic; using System.Net.Http; using System.Threading.Tasks; using IF.Lastfm.Core.Api.Enums; @@ -20,18 +18,12 @@ public GetUserShoutsCommand(IAuth auth, string username) : base(auth) Username = username; } - public override Uri BuildRequestUrl() + public override void SetParameters() { - var parameters = new Dictionary - { - {"user", Uri.EscapeDataString(Username)}, - }; + Parameters.Add("user", Username); - base.AddPagingParameters(parameters); - base.DisableCaching(parameters); - - var uristring = LastFm.FormatApiUrl(Method, Auth.ApiKey, parameters); - return new Uri(uristring, UriKind.Absolute); + AddPagingParameters(); + DisableCaching(); } 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 8130038..10430b0 100644 --- a/IF.Lastfm.Core/IF.Lastfm.Core.csproj +++ b/IF.Lastfm.Core/IF.Lastfm.Core.csproj @@ -48,6 +48,7 @@ + diff --git a/IF.Lastfm.Core/LastFm.cs b/IF.Lastfm.Core/LastFm.cs index f49a346..6f7ba16 100644 --- a/IF.Lastfm.Core/LastFm.cs +++ b/IF.Lastfm.Core/LastFm.cs @@ -61,6 +61,7 @@ public static FormUrlEncodedContent CreatePostBody(string method, string apikey, {"format", ResponseFormat} }; + // TODO ordering var requestParameters = init.Concat(parameters); return new FormUrlEncodedContent(requestParameters);