From d2d4a595a82d039e7291361bc7b916e110199e28 Mon Sep 17 00:00:00 2001 From: Rikki Tooley Date: Sun, 4 Jan 2015 03:53:15 +0000 Subject: [PATCH] artist.getTags (by username) command --- .../AlbumGetTagsByUserCommandTests.cs | 13 ++- .../ArtistGetTagsByUserCommandTests.cs | 83 +++++++++++++++++++ .../IF.Lastfm.Core.Tests.csproj | 5 ++ .../ArtistApi/ArtistGetTagsEmpty.json | 6 ++ .../ArtistApi/ArtistGetTagsError.json | 5 ++ .../ArtistApi/ArtistGetTagsMultiple.json | 17 ++++ .../ArtistApi/ArtistGetTagsSingle.json | 11 +++ .../Resources/ArtistApiResponses.Designer.cs | 40 +++++++++ .../Resources/ArtistApiResponses.resx | 12 +++ src/IF.Lastfm.Core/Api/ArtistApi.cs | 20 +++-- .../ArtistApi/ArtistGetTagsByUserCommand.cs | 58 +++++++++++++ src/IF.Lastfm.Core/IF.Lastfm.Core.csproj | 1 + 12 files changed, 255 insertions(+), 16 deletions(-) create mode 100644 src/IF.Lastfm.Core.Tests/Api/Commands/ArtistGetTagsByUserCommandTests.cs create mode 100644 src/IF.Lastfm.Core.Tests/Resources/ArtistApi/ArtistGetTagsEmpty.json create mode 100644 src/IF.Lastfm.Core.Tests/Resources/ArtistApi/ArtistGetTagsError.json create mode 100644 src/IF.Lastfm.Core.Tests/Resources/ArtistApi/ArtistGetTagsMultiple.json create mode 100644 src/IF.Lastfm.Core.Tests/Resources/ArtistApi/ArtistGetTagsSingle.json create mode 100644 src/IF.Lastfm.Core/Api/Commands/ArtistApi/ArtistGetTagsByUserCommand.cs diff --git a/src/IF.Lastfm.Core.Tests/Api/Commands/AlbumGetTagsByUserCommandTests.cs b/src/IF.Lastfm.Core.Tests/Api/Commands/AlbumGetTagsByUserCommandTests.cs index e4e3350..bcc632b 100644 --- a/src/IF.Lastfm.Core.Tests/Api/Commands/AlbumGetTagsByUserCommandTests.cs +++ b/src/IF.Lastfm.Core.Tests/Api/Commands/AlbumGetTagsByUserCommandTests.cs @@ -1,13 +1,12 @@ -using System.Collections.Generic; +using IF.Lastfm.Core.Api.Commands.AlbumApi; +using IF.Lastfm.Core.Api.Enums; +using IF.Lastfm.Core.Objects; +using IF.Lastfm.Core.Tests.Resources; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; -using IF.Lastfm.Core.Api.Commands.AlbumApi; -using IF.Lastfm.Core.Api.Enums; -using IF.Lastfm.Core.Objects; -using IF.Lastfm.Core.Tests.Api.Commands; -using IF.Lastfm.Core.Tests.Resources; -using Microsoft.VisualStudio.TestTools.UnitTesting; namespace IF.Lastfm.Core.Tests.Api.Commands { diff --git a/src/IF.Lastfm.Core.Tests/Api/Commands/ArtistGetTagsByUserCommandTests.cs b/src/IF.Lastfm.Core.Tests/Api/Commands/ArtistGetTagsByUserCommandTests.cs new file mode 100644 index 0000000..ef1edea --- /dev/null +++ b/src/IF.Lastfm.Core.Tests/Api/Commands/ArtistGetTagsByUserCommandTests.cs @@ -0,0 +1,83 @@ +using IF.Lastfm.Core.Api.Commands.ArtistApi; +using IF.Lastfm.Core.Api.Enums; +using IF.Lastfm.Core.Objects; +using IF.Lastfm.Core.Tests.Resources; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace IF.Lastfm.Core.Tests.Api.Commands +{ + [TestClass] + public class ArtistGetTagsByUserCommandTests : CommandTestsBase + { + private ArtistGetTagsByUserCommand _command; + + [TestInitialize] + public void Initialise() + { + _command = new ArtistGetTagsByUserCommand(MAuth.Object, "", ""); + } + + [TestMethod] + public async Task HandleResponseSingle() + { + var expectedTags = new List + { + new LastTag("the fate of the world is safe in crystal castles", "http://www.last.fm/tag/the%20fate%20of%20the%20world%20is%20safe%20in%20crystal%20castles") + }; + + var response = CreateResponseMessage(Encoding.UTF8.GetString(ArtistApiResponses.ArtistGetTagsSingle)); + var parsed = await _command.HandleResponse(response); + + var expectedJson = expectedTags.TestSerialise(); + var actualJson = parsed.Content.TestSerialise(); + + parsed.AssertValues(true, 1, 1, 1, 1); + Assert.AreEqual(expectedJson, actualJson, expectedJson.DifferencesTo(actualJson)); + } + + [TestMethod] + public async Task HandleResponseMultiple() + { + var expectedTags = new List + { + new LastTag("the fate of the world is safe in crystal castles", "http://www.last.fm/tag/the%20fate%20of%20the%20world%20is%20safe%20in%20crystal%20castles"), + new LastTag("if this were a pokemon i would catch it", "http://www.last.fm/tag/if%20this%20were%20a%20pokemon%20i%20would%20catch%20it") + }; + + var response = CreateResponseMessage(Encoding.UTF8.GetString(ArtistApiResponses.ArtistGetTagsMultiple)); + var parsed = await _command.HandleResponse(response); + + var expectedJson = expectedTags.TestSerialise(); + var actualJson = parsed.Content.TestSerialise(); + + parsed.AssertValues(true, 2, 2, 1, 1); + Assert.AreEqual(expectedJson, actualJson, expectedJson.DifferencesTo(actualJson)); + } + + [TestMethod] + public async Task HandleResponseEmpty() + { + var response = CreateResponseMessage(Encoding.UTF8.GetString(ArtistApiResponses.ArtistGetTagsEmpty)); + var parsed = await _command.HandleResponse(response); + + parsed.AssertValues(true, 0, 0, 1, 1); + Assert.IsTrue(!parsed.Content.Any()); + } + + [TestMethod] + public async Task HandleResponseError() + { + var response = CreateResponseMessage(Encoding.UTF8.GetString(ArtistApiResponses.ArtistGetTagsError)); + var parsed = await _command.HandleResponse(response); + + parsed.AssertValues(false, 0, 0, 1, 1); + Assert.IsFalse(parsed.Success); + Assert.IsTrue(parsed.Error == LastFmApiError.MissingParameters); + Assert.IsTrue(!parsed.Content.Any()); + } + } +} \ No newline at end of file diff --git a/src/IF.Lastfm.Core.Tests/IF.Lastfm.Core.Tests.csproj b/src/IF.Lastfm.Core.Tests/IF.Lastfm.Core.Tests.csproj index a645862..5e0cdaa 100644 --- a/src/IF.Lastfm.Core.Tests/IF.Lastfm.Core.Tests.csproj +++ b/src/IF.Lastfm.Core.Tests/IF.Lastfm.Core.Tests.csproj @@ -80,6 +80,7 @@ + @@ -140,6 +141,10 @@ + + + + diff --git a/src/IF.Lastfm.Core.Tests/Resources/ArtistApi/ArtistGetTagsEmpty.json b/src/IF.Lastfm.Core.Tests/Resources/ArtistApi/ArtistGetTagsEmpty.json new file mode 100644 index 0000000..71df144 --- /dev/null +++ b/src/IF.Lastfm.Core.Tests/Resources/ArtistApi/ArtistGetTagsEmpty.json @@ -0,0 +1,6 @@ +{ + "tags": { + "#text": "\n", + "artist": "Hot Chip", + } +} \ No newline at end of file diff --git a/src/IF.Lastfm.Core.Tests/Resources/ArtistApi/ArtistGetTagsError.json b/src/IF.Lastfm.Core.Tests/Resources/ArtistApi/ArtistGetTagsError.json new file mode 100644 index 0000000..51805a6 --- /dev/null +++ b/src/IF.Lastfm.Core.Tests/Resources/ArtistApi/ArtistGetTagsError.json @@ -0,0 +1,5 @@ +{ + "error": 6, + "message": "Invalid user supplied", + "links": [] +} \ No newline at end of file diff --git a/src/IF.Lastfm.Core.Tests/Resources/ArtistApi/ArtistGetTagsMultiple.json b/src/IF.Lastfm.Core.Tests/Resources/ArtistApi/ArtistGetTagsMultiple.json new file mode 100644 index 0000000..aae05a8 --- /dev/null +++ b/src/IF.Lastfm.Core.Tests/Resources/ArtistApi/ArtistGetTagsMultiple.json @@ -0,0 +1,17 @@ +{ + "tags": { + "tag": [ + { + "name": "the fate of the world is safe in crystal castles", + "url": "http://www.last.fm/tag/the%20fate%20of%20the%20world%20is%20safe%20in%20crystal%20castles" + }, + { + "name": "if this were a pokemon i would catch it", + "url": "http://www.last.fm/tag/if%20this%20were%20a%20pokemon%20i%20would%20catch%20it" + } + ], + "@attr": { + "artist": "Crim3s" + } + } +} \ No newline at end of file diff --git a/src/IF.Lastfm.Core.Tests/Resources/ArtistApi/ArtistGetTagsSingle.json b/src/IF.Lastfm.Core.Tests/Resources/ArtistApi/ArtistGetTagsSingle.json new file mode 100644 index 0000000..35b92ea --- /dev/null +++ b/src/IF.Lastfm.Core.Tests/Resources/ArtistApi/ArtistGetTagsSingle.json @@ -0,0 +1,11 @@ +{ + "tags": { + "tag": { + "name": "the fate of the world is safe in crystal castles", + "url": "http://www.last.fm/tag/the%20fate%20of%20the%20world%20is%20safe%20in%20crystal%20castles" + }, + "@attr": { + "artist": "Crim3s" + } + } +} \ No newline at end of file diff --git a/src/IF.Lastfm.Core.Tests/Resources/ArtistApiResponses.Designer.cs b/src/IF.Lastfm.Core.Tests/Resources/ArtistApiResponses.Designer.cs index b481dc7..73f1ccf 100644 --- a/src/IF.Lastfm.Core.Tests/Resources/ArtistApiResponses.Designer.cs +++ b/src/IF.Lastfm.Core.Tests/Resources/ArtistApiResponses.Designer.cs @@ -80,6 +80,46 @@ internal static byte[] ArtistGetInfoSuccess { } } + /// + /// Looks up a localized resource of type System.Byte[]. + /// + internal static byte[] ArtistGetTagsEmpty { + get { + object obj = ResourceManager.GetObject("ArtistGetTagsEmpty", resourceCulture); + return ((byte[])(obj)); + } + } + + /// + /// Looks up a localized resource of type System.Byte[]. + /// + internal static byte[] ArtistGetTagsError { + get { + object obj = ResourceManager.GetObject("ArtistGetTagsError", resourceCulture); + return ((byte[])(obj)); + } + } + + /// + /// Looks up a localized resource of type System.Byte[]. + /// + internal static byte[] ArtistGetTagsMultiple { + get { + object obj = ResourceManager.GetObject("ArtistGetTagsMultiple", resourceCulture); + return ((byte[])(obj)); + } + } + + /// + /// Looks up a localized resource of type System.Byte[]. + /// + internal static byte[] ArtistGetTagsSingle { + get { + object obj = ResourceManager.GetObject("ArtistGetTagsSingle", resourceCulture); + return ((byte[])(obj)); + } + } + /// /// Looks up a localized resource of type System.Byte[]. /// diff --git a/src/IF.Lastfm.Core.Tests/Resources/ArtistApiResponses.resx b/src/IF.Lastfm.Core.Tests/Resources/ArtistApiResponses.resx index a91538b..504f546 100644 --- a/src/IF.Lastfm.Core.Tests/Resources/ArtistApiResponses.resx +++ b/src/IF.Lastfm.Core.Tests/Resources/ArtistApiResponses.resx @@ -124,6 +124,18 @@ ArtistApi\ArtistGetInfoSuccess.json;System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + ArtistApi\ArtistGetTagsEmpty.json;System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + ArtistApi\ArtistGetTagsError.json;System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + ArtistApi\ArtistGetTagsMultiple.json;System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + ArtistApi\ArtistGetTagsSingle.json;System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + ArtistApi\ArtistGetTopTagsEmpty.json;System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 diff --git a/src/IF.Lastfm.Core/Api/ArtistApi.cs b/src/IF.Lastfm.Core/Api/ArtistApi.cs index c7a291e..4e2f812 100644 --- a/src/IF.Lastfm.Core/Api/ArtistApi.cs +++ b/src/IF.Lastfm.Core/Api/ArtistApi.cs @@ -1,12 +1,7 @@ -#region - -using System; -using System.Threading.Tasks; -using IF.Lastfm.Core.Api.Commands.ArtistApi; +using IF.Lastfm.Core.Api.Commands.ArtistApi; using IF.Lastfm.Core.Api.Helpers; using IF.Lastfm.Core.Objects; - -#endregion +using System.Threading.Tasks; namespace IF.Lastfm.Core.Api { @@ -84,13 +79,20 @@ public async Task> GetSimilarArtistsAsync(string artist return await command.ExecuteAsync(); } - public async Task> GetUserTagsForArtistAsync(string artist, + public Task> GetUserTagsForArtistAsync(string artist, string username, bool autocorrect = false, int page = 1, int itemsPerPage = LastFm.DefaultPageLength) { - throw new NotImplementedException(); + var command = new ArtistGetTagsByUserCommand(Auth, artist, username) + { + Autocorrect = autocorrect, + Page = page, + Count = itemsPerPage + }; + + return command.ExecuteAsync(); } public Task> GetTopTagsForArtistAsync(string artist, bool autocorrect = false) diff --git a/src/IF.Lastfm.Core/Api/Commands/ArtistApi/ArtistGetTagsByUserCommand.cs b/src/IF.Lastfm.Core/Api/Commands/ArtistApi/ArtistGetTagsByUserCommand.cs new file mode 100644 index 0000000..34cd23a --- /dev/null +++ b/src/IF.Lastfm.Core/Api/Commands/ArtistApi/ArtistGetTagsByUserCommand.cs @@ -0,0 +1,58 @@ +using System; +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.ArtistApi +{ + internal class ArtistGetTagsByUserCommand : GetAsyncCommandBase> + { + public string ArtistName { get; set; } + + public string Username { get; set; } + + public bool Autocorrect { get; set; } + + public ArtistGetTagsByUserCommand(ILastAuth auth, string artist, string username) + : base(auth) + { + Method = "album.getTags"; + + ArtistName = artist; + Username = username; + } + + public override void SetParameters() + { + Parameters.Add("artist", ArtistName); + Parameters.Add("user", Username); + Parameters.Add("autocorrect", Convert.ToInt32(Autocorrect).ToString()); + + AddPagingParameters(); + DisableCaching(); + } + + public async override Task> HandleResponse(HttpResponseMessage response) + { + var json = await response.Content.ReadAsStringAsync(); + + LastFmApiError error; + if (LastFm.IsResponseValid(json, out error) && response.IsSuccessStatusCode) + { + var jtoken = JsonConvert.DeserializeObject(json); + var resultsToken = jtoken.SelectToken("tags"); + var itemsToken = resultsToken.SelectToken("tag"); + + return PageResponse.CreateSuccessResponse(itemsToken, LastTag.ParseJToken); + } + else + { + return PageResponse.CreateErrorResponse(error); + } + } + } +} diff --git a/src/IF.Lastfm.Core/IF.Lastfm.Core.csproj b/src/IF.Lastfm.Core/IF.Lastfm.Core.csproj index b981cce..9cfdb9c 100644 --- a/src/IF.Lastfm.Core/IF.Lastfm.Core.csproj +++ b/src/IF.Lastfm.Core/IF.Lastfm.Core.csproj @@ -42,6 +42,7 @@ +