artist.getTags (by username) command

This commit is contained in:
Rikki Tooley 2015-01-04 03:53:15 +00:00
parent b13bef5ea6
commit d2d4a595a8
12 changed files with 255 additions and 16 deletions

View File

@ -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
{

View File

@ -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<LastTag>
{
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<LastTag>
{
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());
}
}
}

View File

@ -80,6 +80,7 @@
<Compile Include="Api\Commands\AlbumApi\GetAlbumTopTagsCommandTest.cs" />
<Compile Include="Api\Commands\AlbumApi\SearchAlbumsCommandTests.cs" />
<Compile Include="Api\Commands\AlbumGetTagsByUserCommandTests.cs" />
<Compile Include="Api\Commands\ArtistGetTagsByUserCommandTests.cs" />
<Compile Include="Api\Commands\ArtistGetTopTagsCommandTests.cs" />
<Compile Include="Api\Commands\CommandTestsBase.cs" />
<Compile Include="Api\Commands\Library\LibraryGetTracksCommandTests.cs" />
@ -140,6 +141,10 @@
<None Include="Resources\AlbumApi\AlbumGetTopTags.json" />
<None Include="Resources\ArtistApi\ArtistGetInfoMissing.json" />
<None Include="Resources\ArtistApi\ArtistGetInfoSuccess.json" />
<None Include="Resources\ArtistApi\ArtistGetTagsEmpty.json" />
<None Include="Resources\ArtistApi\ArtistGetTagsError.json" />
<None Include="Resources\ArtistApi\ArtistGetTagsMultiple.json" />
<None Include="Resources\ArtistApi\ArtistGetTagsSingle.json" />
<None Include="Resources\ArtistApi\ArtistGetTopTagsEmpty.json" />
<None Include="Resources\ArtistApi\ArtistGetTopTagsError.json" />
<None Include="Resources\ArtistApi\ArtistGetTopTagsMultiple.json" />

View File

@ -0,0 +1,6 @@
{
"tags": {
"#text": "\n",
"artist": "Hot Chip",
}
}

View File

@ -0,0 +1,5 @@
{
"error": 6,
"message": "Invalid user supplied",
"links": []
}

View File

@ -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"
}
}
}

View File

@ -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"
}
}
}

View File

@ -80,6 +80,46 @@ internal static byte[] ArtistGetInfoSuccess {
}
}
/// <summary>
/// Looks up a localized resource of type System.Byte[].
/// </summary>
internal static byte[] ArtistGetTagsEmpty {
get {
object obj = ResourceManager.GetObject("ArtistGetTagsEmpty", resourceCulture);
return ((byte[])(obj));
}
}
/// <summary>
/// Looks up a localized resource of type System.Byte[].
/// </summary>
internal static byte[] ArtistGetTagsError {
get {
object obj = ResourceManager.GetObject("ArtistGetTagsError", resourceCulture);
return ((byte[])(obj));
}
}
/// <summary>
/// Looks up a localized resource of type System.Byte[].
/// </summary>
internal static byte[] ArtistGetTagsMultiple {
get {
object obj = ResourceManager.GetObject("ArtistGetTagsMultiple", resourceCulture);
return ((byte[])(obj));
}
}
/// <summary>
/// Looks up a localized resource of type System.Byte[].
/// </summary>
internal static byte[] ArtistGetTagsSingle {
get {
object obj = ResourceManager.GetObject("ArtistGetTagsSingle", resourceCulture);
return ((byte[])(obj));
}
}
/// <summary>
/// Looks up a localized resource of type System.Byte[].
/// </summary>

View File

@ -124,6 +124,18 @@
<data name="ArtistGetInfoSuccess" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>ArtistApi\ArtistGetInfoSuccess.json;System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</data>
<data name="ArtistGetTagsEmpty" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>ArtistApi\ArtistGetTagsEmpty.json;System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</data>
<data name="ArtistGetTagsError" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>ArtistApi\ArtistGetTagsError.json;System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</data>
<data name="ArtistGetTagsMultiple" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>ArtistApi\ArtistGetTagsMultiple.json;System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</data>
<data name="ArtistGetTagsSingle" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>ArtistApi\ArtistGetTagsSingle.json;System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</data>
<data name="ArtistGetTopTagsEmpty" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>ArtistApi\ArtistGetTopTagsEmpty.json;System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</data>

View File

@ -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<PageResponse<LastArtist>> GetSimilarArtistsAsync(string artist
return await command.ExecuteAsync();
}
public async Task<PageResponse<LastTag>> GetUserTagsForArtistAsync(string artist,
public Task<PageResponse<LastTag>> 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<PageResponse<LastTag>> GetTopTagsForArtistAsync(string artist, bool autocorrect = false)

View File

@ -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<PageResponse<LastTag>>
{
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<PageResponse<LastTag>> HandleResponse(HttpResponseMessage response)
{
var json = await response.Content.ReadAsStringAsync();
LastFmApiError error;
if (LastFm.IsResponseValid(json, out error) && response.IsSuccessStatusCode)
{
var jtoken = JsonConvert.DeserializeObject<JToken>(json);
var resultsToken = jtoken.SelectToken("tags");
var itemsToken = resultsToken.SelectToken("tag");
return PageResponse<LastTag>.CreateSuccessResponse(itemsToken, LastTag.ParseJToken);
}
else
{
return PageResponse<LastTag>.CreateErrorResponse(error);
}
}
}
}

View File

@ -42,6 +42,7 @@
<Compile Include="Api\ArtistApi.cs" />
<Compile Include="Api\Commands\AlbumApi\GetAlbumTopTagsCommand.cs" />
<Compile Include="Api\Commands\AlbumApi\AlbumGetTagsByUserCommand.cs" />
<Compile Include="Api\Commands\ArtistApi\ArtistGetTagsByUserCommand.cs" />
<Compile Include="Api\Commands\ArtistApi\ArtistGetTopTagsCommand.cs" />
<Compile Include="Api\Commands\LibraryApi\LibraryGetTracksCommand.cs" />
<Compile Include="Api\Commands\TrackApi\TrackScrobbleCommand.cs" />