Merge pull request #37 from prashantkhandelwal/master

Closes #37
This commit is contained in:
Rikki Tooley 2014-12-05 14:45:10 +00:00
commit 7b8f11ea75
10 changed files with 231 additions and 5 deletions

View File

@ -0,0 +1,80 @@
using IF.Lastfm.Core.Api.Commands.AlbumApi;
using IF.Lastfm.Core.Api.Enums;
using IF.Lastfm.Core.Tests.Resources;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace IF.Lastfm.Core.Tests.Api.Commands.AlbumApi
{
[TestClass]
public class GetAlbumTopTagsCommandTest : CommandTestsBase
{
private GetAlbumTopTagsCommand _command;
public GetAlbumTopTagsCommandTest()
{
_command = new GetAlbumTopTagsCommand(MAuth.Object)
{
AlbumName = "Believe",
ArtistName = "Cher"
};
_command.SetParameters();
}
[TestMethod]
public void Constructor()
{
Assert.AreEqual(_command.Method, "album.gettoptags");
Assert.AreEqual(_command.Parameters["album"], "Believe");
Assert.AreEqual(_command.Parameters["artist"], "Cher");
}
[TestMethod]
public async Task HandleSuccessResponse()
{
var response = CreateResponseMessage(Encoding.UTF8.GetString(AlbumApiResponses.AlbumGetTopTags));
var parsed = await _command.HandleResponse(response);
Assert.IsTrue(parsed.Success);
Assert.IsNotNull(parsed.Content);
}
[TestMethod]
public async Task HandleEmptyResponse()
{
var response = CreateResponseMessage(Encoding.UTF8.GetString(AlbumApiResponses.AlbumGetTopTagsEmpty));
var parsed = await _command.HandleResponse(response);
Assert.IsTrue(parsed.Success);
Assert.IsNotNull(parsed.Content);
Assert.IsTrue(!parsed.Content.Any());
}
[TestMethod]
public async Task HandleErrorResponse()
{
var response = CreateResponseMessage(Encoding.UTF8.GetString(AlbumApiResponses.AlbumGetTopTagsError));
var parsed = await _command.HandleResponse(response);
Assert.IsFalse(parsed.Success);
Assert.IsTrue(parsed.Error == LastFmApiError.MissingParameters);
Assert.IsNotNull(parsed.Content);
Assert.IsTrue(!parsed.Content.Any());
}
}
}

View File

@ -57,6 +57,12 @@
</Reference>
<Reference Include="System.Net" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Net.Http.Extensions">
<HintPath>..\..\packages\Microsoft.Net.Http.2.2.28\lib\net45\System.Net.Http.Extensions.dll</HintPath>
</Reference>
<Reference Include="System.Net.Http.Primitives">
<HintPath>..\..\packages\Microsoft.Net.Http.2.2.28\lib\net45\System.Net.Http.Primitives.dll</HintPath>
</Reference>
<Reference Include="System.Net.Http.WebRequest" />
</ItemGroup>
<Choose>
@ -70,6 +76,7 @@
<ItemGroup>
<Compile Include="Api\Commands\AlbumApi\GetAlbumInfoCommandTests.cs" />
<Compile Include="Api\Commands\AlbumApi\GetAlbumShoutsCommandTests.cs" />
<Compile Include="Api\Commands\AlbumApi\GetAlbumTopTagsCommandTest.cs" />
<Compile Include="Api\Commands\AlbumApi\SearchAlbumsCommandTests.cs" />
<Compile Include="Api\Commands\CommandTestsBase.cs" />
<Compile Include="Api\Commands\TrackApi\GetTrackShoutsCommandTests.cs" />
@ -80,9 +87,9 @@
<Compile Include="LastFmTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Resources\AlbumApiResponses.Designer.cs">
<DependentUpon>AlbumApiResponses.resx</DependentUpon>
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
<DesignTime>True</DesignTime>
<DependentUpon>AlbumApiResponses.resx</DependentUpon>
</Compile>
<Compile Include="Resources\TrackApiResponses.Designer.cs">
<DependentUpon>TrackApiResponses.resx</DependentUpon>
@ -102,10 +109,13 @@
<None Include="Resources\AlbumApi\AlbumGetShoutsEmpty.json" />
<None Include="Resources\AlbumApi\AlbumGetShoutsMultiple.json" />
<None Include="Resources\AlbumApi\AlbumGetShoutsSingle.json" />
<None Include="Resources\AlbumApi\AlbumGetTopTagsEmpty.json" />
<None Include="Resources\AlbumApi\AlbumGetTopTagsError.json" />
<None Include="Resources\AlbumApi\AlbumSearch.json" />
<None Include="Resources\AlbumApi\AlbumSearchEmpty.json" />
<None Include="Resources\AlbumApi\AlbumSearchError.json" />
<None Include="Resources\AlbumApi\AlbumSearchSingle.json" />
<None Include="Resources\AlbumApi\AlbumGetTopTags.json" />
<None Include="Resources\TrackApi\TrackGetShouts.json" />
<None Include="Resources\TrackApi\TrackGetShoutsEmpty.json" />
<None Include="Resources\TrackApi\TrackGetShoutsError.json" />
@ -149,6 +159,11 @@
<Import Project="$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets" Condition="Exists('$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets')" />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />
<Import Project="..\..\packages\Microsoft.Bcl.Build.1.0.14\tools\Microsoft.Bcl.Build.targets" Condition="Exists('..\..\packages\Microsoft.Bcl.Build.1.0.14\tools\Microsoft.Bcl.Build.targets')" />
<Target Name="EnsureBclBuildImported" BeforeTargets="BeforeBuild" Condition="'$(BclBuildImported)' == ''">
<Error Condition="!Exists('..\..\packages\Microsoft.Bcl.Build.1.0.14\tools\Microsoft.Bcl.Build.targets')" Text="This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=317567." HelpKeyword="BCLBUILD2001" />
<Error Condition="Exists('..\..\packages\Microsoft.Bcl.Build.1.0.14\tools\Microsoft.Bcl.Build.targets')" Text="The build restored NuGet packages. Build the project again to include these packages in the build. For more information, see http://go.microsoft.com/fwlink/?LinkID=317568." HelpKeyword="BCLBUILD2002" />
</Target>
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,8 @@
{
"toptags":
{
"#text":"\n",
"artist":"Amit Sana",
"album":"Chal Diye"
}
}

View File

@ -0,0 +1 @@
{"error":6,"message":"You must supply either an album & artist name or an album mbid.","links":[]}

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.34014
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
@ -120,6 +120,36 @@ internal static byte[] AlbumGetShoutsSingle {
}
}
/// <summary>
/// Looks up a localized resource of type System.Byte[].
/// </summary>
internal static byte[] AlbumGetTopTags {
get {
object obj = ResourceManager.GetObject("AlbumGetTopTags", resourceCulture);
return ((byte[])(obj));
}
}
/// <summary>
/// Looks up a localized resource of type System.Byte[].
/// </summary>
internal static byte[] AlbumGetTopTagsEmpty {
get {
object obj = ResourceManager.GetObject("AlbumGetTopTagsEmpty", resourceCulture);
return ((byte[])(obj));
}
}
/// <summary>
/// Looks up a localized resource of type System.Byte[].
/// </summary>
internal static byte[] AlbumGetTopTagsError {
get {
object obj = ResourceManager.GetObject("AlbumGetTopTagsError", resourceCulture);
return ((byte[])(obj));
}
}
/// <summary>
/// Looks up a localized resource of type System.Byte[].
/// </summary>

View File

@ -136,6 +136,15 @@
<data name="AlbumGetShoutsSingle" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>albumapi\albumgetshoutssingle.json;System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</data>
<data name="AlbumGetTopTags" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>AlbumApi\AlbumGetTopTags.json;System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</data>
<data name="AlbumGetTopTagsEmpty" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>AlbumApi\AlbumGetTopTagsEmpty.json;System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</data>
<data name="AlbumGetTopTagsError" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>AlbumApi\AlbumGetTopTagsError.json;System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</data>
<data name="AlbumSearch" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>AlbumApi\AlbumSearch.json;System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</data>

View File

@ -46,9 +46,15 @@ public Task<PageResponse<LastTag>> GetUserTagsForAlbumAsync(string artist, strin
throw new NotImplementedException();
}
public Task<PageResponse<LastTag>> GetTopTagsForAlbumAsync(string artist, string album, bool autocorrect = false)
public async Task<PageResponse<LastTag>> GetTopTagsForAlbumAsync(string artist, string album, bool autocorrect = false)
{
throw new NotImplementedException();
var command = new GetAlbumTopTagsCommand(Auth)
{
ArtistName = artist,
AlbumName = album
};
return await command.ExecuteAsync();
}
public async Task<PageResponse<LastAlbum>> SearchForAlbumAsync(string albumname, int page = 1, int itemsPerPage = LastFm.DefaultPageLength)

View File

@ -0,0 +1,75 @@
using IF.Lastfm.Core.Api.Enums;
using IF.Lastfm.Core.Api.Helpers;
using IF.Lastfm.Core.Objects;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
namespace IF.Lastfm.Core.Api.Commands.AlbumApi
{
internal class GetAlbumTopTagsCommand : GetAsyncCommandBase<PageResponse<LastTag>>
{
public string AlbumMbid { get; set; }
public string ArtistName { get; set; }
public string AlbumName { get; set; }
public bool Autocorrect { get; set; }
public GetAlbumTopTagsCommand(ILastAuth auth)
: base(auth)
{
Method = "album.gettoptags";
}
public GetAlbumTopTagsCommand(ILastAuth auth, string album, string artist)
: this(auth)
{
AlbumName = album;
ArtistName = artist;
}
public override void SetParameters()
{
if (AlbumMbid != null)
{
Parameters.Add("mbid", AlbumMbid);
}
else
{
Parameters.Add("artist", ArtistName);
Parameters.Add("album", AlbumName);
}
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("toptags");
var itemsToken = resultsToken.SelectToken("tag");
return PageResponse<LastTag>.CreateSuccessResponse(itemsToken, resultsToken, LastTag.ParseJToken, false);
}
else
{
return LastResponse.CreateErrorResponse<PageResponse<LastTag>>(error);
}
}
}
}

View File

@ -40,6 +40,7 @@
<ItemGroup>
<Compile Include="Api\AlbumApi.cs" />
<Compile Include="Api\ArtistApi.cs" />
<Compile Include="Api\Commands\AlbumApi\GetAlbumTopTagsCommand.cs" />
<Compile Include="Api\Commands\UserApi\GetRecommendedArtistsCommand.cs" />
<Compile Include="Api\LastAuth.cs" />
<Compile Include="Api\ChartApi.cs" />