Merge pull request #74 from rms81/tag_api_getInfo

tag.getInfo
This commit is contained in:
Rikki Tooley 2015-07-06 11:33:03 +01:00
commit 1af3d9b7d7
11 changed files with 179 additions and 3 deletions

View File

@ -0,0 +1,58 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using IF.Lastfm.Core.Api.Commands.Tag;
using IF.Lastfm.Core.Api.Enums;
using IF.Lastfm.Core.Objects;
using IF.Lastfm.Core.Tests.Resources;
using NUnit.Framework;
namespace IF.Lastfm.Core.Tests.Api.Commands.Tag
{
public class GetInfoCommandTests: CommandTestsBase
{
[Test]
public async Task HandleSuccessResponse()
{
//Arrange
const string tagName = "disco";
const string tagUri = "http://www.last.fm/tag/disco";
var command = new GetInfoCommand(MAuth.Object, tagName);
var expectedTag=new LastTag(tagName,tagUri)
{
Reach = 34671,
Count = 172224,
Streamable = true
};
//Act
var response = CreateResponseMessage(Encoding.UTF8.GetString(TagApiResponses.GetInfoSuccess));
var lastResponse = await command.HandleResponse(response);
var tag = lastResponse.Content;
//Assert
Assert.IsTrue(lastResponse.Success);
Assert.AreEqual(expectedTag.Reach,tag.Reach);
Assert.AreEqual(expectedTag.Name, tag.Name);
Assert.AreEqual(expectedTag.Count, tag.Count);
Assert.AreEqual(expectedTag.Streamable, tag.Streamable);
}
[Test]
public async Task HandleErrorResponse()
{
var command = new GetInfoCommand(MAuth.Object, "errorTag");
var response = CreateResponseMessage(Encoding.UTF8.GetString(TagApiResponses.GetInfoError));
var parsed = await command.HandleResponse(response);
Assert.IsFalse(parsed.Success);
Assert.IsTrue(parsed.Status == LastResponseStatus.MissingParameters);
}
}
}

View File

@ -92,6 +92,7 @@
<Compile Include="Api\Commands\Library\LibraryGetTracksCommandTests.cs" />
<Compile Include="Api\Commands\Library\RemoveScrobbleCommandTests.cs" />
<Compile Include="Api\Commands\Library\RemoveTrackCommandTests.cs" />
<Compile Include="Api\Commands\Tag\GetInfoCommandTests.cs" />
<Compile Include="Api\Commands\Tag\GetSimilarCommandTests.cs" />
<Compile Include="Api\Commands\TrackApi\GetTrackShoutsCommandTests.cs" />
<Compile Include="Api\Commands\ArtistGetInfoCommandTests.cs" />
@ -174,6 +175,8 @@
<None Include="Resources\ArtistApi\ArtistGetTopTagsSingle.json" />
<None Include="Resources\LibraryApi\LibraryGetTracksMultiple.json" />
<None Include="Resources\LibraryApi\LibraryGetTracksSingle.json" />
<None Include="Resources\Tag\GetInfoError.json" />
<None Include="Resources\Tag\GetInfoSuccess.json" />
<None Include="Resources\Tag\GetSimilarError.json" />
<None Include="Resources\Tag\GetSimilarSuccess.json" />
<None Include="Resources\TrackApi\TrackGetShouts.json" />

View File

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

File diff suppressed because one or more lines are too long

View File

@ -1,7 +1,7 @@
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:4.0.30319.0
// Runtime Version:4.0.30319.42000
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
@ -60,6 +60,26 @@ internal TagApiResponses() {
}
}
/// <summary>
/// Looks up a localized resource of type System.Byte[].
/// </summary>
internal static byte[] GetInfoError {
get {
object obj = ResourceManager.GetObject("GetInfoError", resourceCulture);
return ((byte[])(obj));
}
}
/// <summary>
/// Looks up a localized resource of type System.Byte[].
/// </summary>
internal static byte[] GetInfoSuccess {
get {
object obj = ResourceManager.GetObject("GetInfoSuccess", resourceCulture);
return ((byte[])(obj));
}
}
/// <summary>
/// Looks up a localized resource of type System.Byte[].
/// </summary>

View File

@ -118,6 +118,12 @@
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<assembly alias="System.Windows.Forms" name="System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<data name="GetInfoError" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>Tag\GetInfoError.json;System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</data>
<data name="GetInfoSuccess" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>Tag\GetInfoSuccess.json;System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</data>
<data name="GetSimilarError" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>tag\getsimilarerror.json;System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</data>

View File

@ -0,0 +1,48 @@
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.Tag
{
[ApiMethodName("tag.getInfo")]
public class GetInfoCommand:GetAsyncCommandBase<LastResponse<LastTag>>
{
public string TagName { get; set; }
public GetInfoCommand(ILastAuth auth, string tagName)
: base(auth)
{
TagName = tagName;
}
public override void SetParameters()
{
Parameters.Add("tag", TagName);
DisableCaching();
}
public async override Task<LastResponse<LastTag>> HandleResponse(HttpResponseMessage response)
{
var json = await response.Content.ReadAsStringAsync();
LastResponseStatus status;
if (LastFm.IsResponseValid(json, out status) && response.IsSuccessStatusCode)
{
var jtoken = JsonConvert.DeserializeObject<JToken>(json);
var tag = LastTag.ParseJToken(jtoken.SelectToken("tag"));
return LastResponse<LastTag>.CreateSuccessResponse(tag);
}
else
{
return LastResponse.CreateErrorResponse<LastResponse<LastTag>>(status);
}
}
}
}

View File

@ -7,5 +7,6 @@ namespace IF.Lastfm.Core.Api
public interface ITagApi
{
Task<PageResponse<LastTag>> GetSimilarAsync(string tagName);
Task<LastResponse<LastTag>> GetInfoAsync(string tagName);
}
}

View File

@ -26,5 +26,12 @@ public Task<PageResponse<LastTag>> GetSimilarAsync(string tagName)
return command.ExecuteAsync();
}
public async Task<LastResponse<LastTag>> GetInfoAsync(string tagName)
{
var command=new GetInfoCommand(Auth,tagName);
return await command.ExecuteAsync();
}
}
}

View File

@ -63,6 +63,7 @@
<Compile Include="Api\Commands\Library\GetTracksCommand.cs" />
<Compile Include="Api\Commands\Library\RemoveScrobbleCommand.cs" />
<Compile Include="Api\Commands\Library\RemoveTrackCommand.cs" />
<Compile Include="Api\Commands\Tag\GetInfoCommand.cs" />
<Compile Include="Api\Commands\Tag\GetSimilarCommand.cs" />
<Compile Include="Api\Commands\Track\ScrobbleCommand.cs" />
<Compile Include="Api\Commands\Track\UpdateNowPlayingCommand.cs" />

View File

@ -17,6 +17,11 @@ public class LastTag : ILastfmObject
public bool? Streamable { get; set; }
/// <summary>
/// The number of users that have used this tag
/// </summary>
public int? Reach { get; set; }
#endregion
public LastTag()
@ -36,7 +41,7 @@ internal static LastTag ParseJToken(JToken token, string relatedTag = null)
var url = token.Value<string>("url");
int? count = null;
var countToken = token.SelectToken("count");
var countToken = token.SelectToken("count") ?? token.SelectToken("taggings");
if (countToken != null)
{
count = countToken.ToObject<int?>();
@ -49,10 +54,18 @@ internal static LastTag ParseJToken(JToken token, string relatedTag = null)
streamable = Convert.ToBoolean(streamableToken.Value<int>());
}
int? reach = null;
var reachToken = token.SelectToken("reach");
if (reachToken != null)
{
reach = reachToken.ToObject<int?>();
}
return new LastTag(name, url, count)
{
Streamable = streamable,
RelatedTo = relatedTag
RelatedTo = relatedTag,
Reach = reach
};
}
}