Album.getInfo, starting to get a nice layout to the api.

This commit is contained in:
Rikki Tooley 2013-06-11 17:40:22 +01:00
parent 720e055d82
commit 3184fc617d
22 changed files with 503 additions and 40 deletions

View File

@ -1,7 +1,9 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Net;
using System.Text; using System.Text;
using System.Threading.Tasks;
using IF.Lastfm.Core; using IF.Lastfm.Core;
using IF.Lastfm.Core.Api; using IF.Lastfm.Core.Api;
@ -9,14 +11,27 @@ namespace IF.Lastfm.Console
{ {
class Program class Program
{ {
private const string ApiKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxx"; private const string ApiKey = "xxx";
private const string ApiSecret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxx"; private const string ApiSecret = "xxx";
static void Main(string[] args) static void Main(string[] args)
{ {
var lastfm = new Auth(ApiKey, ApiSecret); Run().Wait();
}
lastfm.GetSessionTokenAsync("xxxxxxxxxxxx", "xxxxxxxxxxxx").Wait(); public static async Task Run()
{
var auth = new Auth(ApiKey, ApiSecret);
await auth.GetSessionTokenAsync("xxx", "xxx");
var albumApi = new AlbumApi(auth);
var album = await albumApi.GetAlbumInfoAsync("Grimes", "Visions", false);
var req = WebRequest.Create("");
var w = await req.GetRequestStreamAsync();
System.Console.ReadLine();
} }
} }
} }

View File

@ -0,0 +1,19 @@
using IF.Lastfm.Core.Api;
using Moq;
namespace IF.Lastfm.Core.Tests.Api
{
public class MockAlbumApi
{
public AlbumApi Object { get; private set; }
public Mock<IAuth> Auth { get; private set; }
public MockAlbumApi(Mock<IAuth> auth)
{
Auth = auth;
Object = new AlbumApi(Auth.Object);
}
}
}

View File

@ -40,19 +40,22 @@
<Prefer32Bit>false</Prefer32Bit> <Prefer32Bit>false</Prefer32Bit>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Reference Include="Microsoft.Threading.Tasks"> <Reference Include="Microsoft.Threading.Tasks, Version=1.0.12.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.Bcl.Async.1.0.16\lib\net40\Microsoft.Threading.Tasks.dll</HintPath> <SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\Microsoft.Bcl.Async.1.0.16\lib\net45\Microsoft.Threading.Tasks.dll</HintPath>
</Reference> </Reference>
<Reference Include="Microsoft.Threading.Tasks.Extensions"> <Reference Include="Microsoft.Threading.Tasks.Extensions, Version=1.0.12.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.Bcl.Async.1.0.16\lib\net40\Microsoft.Threading.Tasks.Extensions.dll</HintPath> <SpecificVersion>False</SpecificVersion>
</Reference> <HintPath>..\packages\Microsoft.Bcl.Async.1.0.16\lib\net45\Microsoft.Threading.Tasks.Extensions.dll</HintPath>
<Reference Include="Microsoft.Threading.Tasks.Extensions.Desktop">
<HintPath>..\packages\Microsoft.Bcl.Async.1.0.16\lib\net40\Microsoft.Threading.Tasks.Extensions.Desktop.dll</HintPath>
</Reference> </Reference>
<Reference Include="Moq"> <Reference Include="Moq">
<HintPath>..\packages\Moq.4.0.10827\lib\NET40\Moq.dll</HintPath> <HintPath>..\packages\Moq.4.0.10827\lib\NET40\Moq.dll</HintPath>
<Private>True</Private> <Private>True</Private>
</Reference> </Reference>
<Reference Include="Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\Newtonsoft.Json.5.0.6\lib\portable-net40+sl4+wp7+win8\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="System" /> <Reference Include="System" />
<Reference Include="System.Core"> <Reference Include="System.Core">
<RequiredTargetFramework>3.5</RequiredTargetFramework> <RequiredTargetFramework>3.5</RequiredTargetFramework>
@ -92,13 +95,22 @@
</Otherwise> </Otherwise>
</Choose> </Choose>
<ItemGroup> <ItemGroup>
<Compile Include="Api\AlbumTests.cs" />
<Compile Include="Api\MockAlbumApi.cs" />
<Compile Include="MockLastFm.cs" /> <Compile Include="MockLastFm.cs" />
<Compile Include="LastFmTests.cs" /> <Compile Include="LastFmTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ResourceManager.cs" />
<Compile Include="TestData.Designer.cs">
<AutoGen>True</AutoGen>
<DesignTime>True</DesignTime>
<DependentUpon>TestData.resx</DependentUpon>
</Compile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="app.config" /> <None Include="app.config" />
<None Include="packages.config" /> <None Include="packages.config" />
<None Include="Resources\AlbumGetInfo.json" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\IF.Lastfm.Core\IF.Lastfm.Core.csproj"> <ProjectReference Include="..\IF.Lastfm.Core\IF.Lastfm.Core.csproj">
@ -106,6 +118,12 @@
<Name>IF.Lastfm.Core</Name> <Name>IF.Lastfm.Core</Name>
</ProjectReference> </ProjectReference>
</ItemGroup> </ItemGroup>
<ItemGroup>
<EmbeddedResource Include="TestData.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>TestData.Designer.cs</LastGenOutput>
</EmbeddedResource>
</ItemGroup>
<Choose> <Choose>
<When Condition="'$(VisualStudioVersion)' == '10.0' And '$(IsCodedUITest)' == 'True'"> <When Condition="'$(VisualStudioVersion)' == '10.0' And '$(IsCodedUITest)' == 'True'">
<ItemGroup> <ItemGroup>

View File

@ -11,11 +11,9 @@ public class LastFmTests
[TestMethod] [TestMethod]
public void ApiUrlFormatReturnsCorrectly() public void ApiUrlFormatReturnsCorrectly()
{ {
var lastfm = new MockLastFm();
const string expected = "https://ws.audioscrobbler.com/2.0/?method=tobias.funke&api_key=suddenvalley&blue=performance&format=json&uncle=t-bag"; const string expected = "https://ws.audioscrobbler.com/2.0/?method=tobias.funke&api_key=suddenvalley&blue=performance&format=json&uncle=t-bag";
var actual = lastfm.Object.FormatApiUrl("tobias.funke", "suddenvalley", new Dictionary<string, string> var actual = LastFm.FormatApiUrl("tobias.funke", "suddenvalley", new Dictionary<string, string>
{ {
{"uncle", "t-bag"}, {"uncle", "t-bag"},
{"blue", "performance"} {"blue", "performance"}

View File

@ -0,0 +1,45 @@
using System;
using System.Collections.Generic;
using System.Text;
using IF.Lastfm.Core.Api;
using IF.Lastfm.Core.Objects;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace IF.Lastfm.Core.Tests.Objects
{
[TestClass]
public class AlbumTests
{
[TestMethod]
public void AlbumParsesValidJson()
{
var jo = ResourceManager.LoadResource(Encoding.UTF8.GetString(TestData.AlbumGetInfo));
var parsed = Album.ParseJToken(jo.SelectToken("album"));
var expected = new Album()
{
ArtistId = "283786832",
ArtistName = "Grimes",
ListenerCount = 293542,
TotalPlayCount = 10540575,
Mbid = "2fd00edb-391a-41ec-8f2f-01e2c202d9eb",
Name = "Visions",
ReleaseDateUtc = new DateTime(2012, 02, 21, 0, 0, 0),
Url = new Uri("http://www.last.fm/music/Grimes/Visions", UriKind.Absolute),
TopTags = new List<Tag>
{
// TODO
},
Tracks = new List<Track>
{
}
};
Assert.AreEqual(parsed, expected);
}
}
}

View File

@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace IF.Lastfm.Core.Tests
{
public static class ResourceManager
{
public static JObject LoadResource(string json)
{
JsonReader reader = new JsonTextReader(new StringReader(json));
reader.DateParseHandling = DateParseHandling.None;
return JObject.Load(reader);
}
}
}

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,73 @@
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:4.0.30319.18033
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
namespace IF.Lastfm.Core.Tests {
using System;
/// <summary>
/// A strongly-typed resource class, for looking up localized strings, etc.
/// </summary>
// This class was auto-generated by the StronglyTypedResourceBuilder
// class via a tool like ResGen or Visual Studio.
// To add or remove a member, edit your .ResX file then rerun ResGen
// with the /str option, or rebuild your VS project.
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
internal class TestData {
private static global::System.Resources.ResourceManager resourceMan;
private static global::System.Globalization.CultureInfo resourceCulture;
[global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
internal TestData() {
}
/// <summary>
/// Returns the cached ResourceManager instance used by this class.
/// </summary>
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
internal static global::System.Resources.ResourceManager ResourceManager {
get {
if (object.ReferenceEquals(resourceMan, null)) {
global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("IF.Lastfm.Core.Tests.TestData", typeof(TestData).Assembly);
resourceMan = temp;
}
return resourceMan;
}
}
/// <summary>
/// Overrides the current thread's CurrentUICulture property for all
/// resource lookups using this strongly typed resource class.
/// </summary>
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
internal static global::System.Globalization.CultureInfo Culture {
get {
return resourceCulture;
}
set {
resourceCulture = value;
}
}
/// <summary>
/// Looks up a localized resource of type System.Byte[].
/// </summary>
internal static byte[] AlbumGetInfo {
get {
object obj = ResourceManager.GetObject("AlbumGetInfo", resourceCulture);
return ((byte[])(obj));
}
}
}
}

View File

@ -0,0 +1,124 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<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="AlbumGetInfo" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>resources\albumgetinfo.json;System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</data>
</root>

View File

@ -1,19 +1,19 @@
<?xml version="1.0"?> <?xml version="1.0" encoding="utf-8"?>
<configuration> <configuration>
<runtime> <runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly> <dependentAssembly>
<assemblyIdentity name="System.Net.Http" publicKeyToken="b03f5f7f11d50a3a" culture="neutral"/> <assemblyIdentity name="System.Net.Http" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-2.1.10.0" newVersion="2.1.10.0"/> <bindingRedirect oldVersion="0.0.0.0-2.1.10.0" newVersion="2.1.10.0" />
</dependentAssembly> </dependentAssembly>
<dependentAssembly> <dependentAssembly>
<assemblyIdentity name="System.Runtime" publicKeyToken="b03f5f7f11d50a3a" culture="neutral"/> <assemblyIdentity name="System.Runtime" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-2.5.19.0" newVersion="2.5.19.0"/> <bindingRedirect oldVersion="0.0.0.0-2.5.19.0" newVersion="2.5.19.0" />
</dependentAssembly> </dependentAssembly>
<dependentAssembly> <dependentAssembly>
<assemblyIdentity name="System.Threading.Tasks" publicKeyToken="b03f5f7f11d50a3a" culture="neutral"/> <assemblyIdentity name="System.Threading.Tasks" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-2.5.19.0" newVersion="2.5.19.0"/> <bindingRedirect oldVersion="0.0.0.0-2.5.19.0" newVersion="2.5.19.0" />
</dependentAssembly> </dependentAssembly>
</assemblyBinding> </assemblyBinding>
</runtime> </runtime>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/></startup></configuration> <startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /></startup></configuration>

View File

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<packages> <packages>
<package id="Microsoft.Bcl" version="1.0.19" targetFramework="net40" /> <package id="Microsoft.Bcl" version="1.0.19" targetFramework="net40" />
<package id="Microsoft.Bcl.Async" version="1.0.16" targetFramework="net40" /> <package id="Microsoft.Bcl.Async" version="1.0.16" targetFramework="net45" />
<package id="Microsoft.Bcl.Build" version="1.0.7" targetFramework="net40" /> <package id="Microsoft.Bcl.Build" version="1.0.7" targetFramework="net40" />
<package id="Microsoft.Net.Http" version="2.1.10" targetFramework="net40" /> <package id="Microsoft.Net.Http" version="2.1.10" targetFramework="net40" />
<package id="Moq" version="4.0.10827" targetFramework="net40" /> <package id="Moq" version="4.0.10827" targetFramework="net40" />

View File

@ -0,0 +1,60 @@
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
using IF.Lastfm.Core.Objects;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace IF.Lastfm.Core.Api
{
public class AlbumApi : IAlbumApi
{
public IAuth Auth { get; private set; }
public AlbumApi(IAuth auth)
{
Auth = auth;
}
public async Task<Album> GetAlbumInfoAsync(string artistname, string albumname, bool autocorrect = false)
{
const string apiMethod = "album.getInfo";
var parameters = new Dictionary<string, string>
{
{"artist", artistname},
{"album", albumname},
{"autocorrect", Convert.ToInt32(autocorrect).ToString()}
};
var apiUrl = LastFm.FormatApiUrl(apiMethod, Auth.ApiKey, parameters);
var httpClient = new HttpClient();
var lastResponse = await httpClient.GetAsync(apiUrl);
if (lastResponse.IsSuccessStatusCode)
{
var json = await lastResponse.Content.ReadAsStringAsync();
var jtoken = JsonConvert.DeserializeObject<JToken>(json);
var album = Album.ParseJToken(jtoken.SelectToken("album"));
return album;
}
else
{
// ???
throw new NotImplementedException();
}
}
public async Task<Album> GetAlbumInfoWithMbidAsync(string mbid, bool autocorrect = false)
{
throw new System.NotImplementedException();
}
}
}

View File

@ -1,6 +1,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Net.Http; using System.Net.Http;
using System.Threading.Tasks; using System.Threading.Tasks;
using IF.Lastfm.Core.Objects;
using Newtonsoft.Json; using Newtonsoft.Json;
using Newtonsoft.Json.Linq; using Newtonsoft.Json.Linq;
using xBrainLab.Security.Cryptography; using xBrainLab.Security.Cryptography;
@ -12,25 +13,27 @@ public class Auth : IAuth
private const string ApiSignatureSeedFormat = "api_key{0}method{1}password{2}username{3}{4}"; private const string ApiSignatureSeedFormat = "api_key{0}method{1}password{2}username{3}{4}";
private const string ApiAuthMethod = "auth.getMobileSession"; private const string ApiAuthMethod = "auth.getMobileSession";
protected string ApiSecret { get; set; } private readonly string _apiSecret;
public string ApiKey { get; set; }
public bool HasAuthenticated { get { return User != null; } }
public string ApiKey { get; private set; }
public UserSession User { get; private set; }
public Auth(string apikey, string secret) public Auth(string apikey, string secret)
{ {
ApiKey = apikey; ApiKey = apikey;
ApiSecret = secret; _apiSecret = secret;
} }
public async Task<UserSession> GetSessionTokenAsync(string username, string password) public async Task GetSessionTokenAsync(string username, string password)
{ {
const string apiMethod = "auth.getMobileSession"; const string apiMethod = "auth.getMobileSession";
var apisigseed = string.Format(ApiSignatureSeedFormat, ApiKey, ApiAuthMethod, password, username, ApiSecret); var apisigseed = string.Format(ApiSignatureSeedFormat, ApiKey, ApiAuthMethod, password, username, _apiSecret);
var apisig = MD5.GetHashString(apisigseed); var apisig = MD5.GetHashString(apisigseed);
var lastfm = new LastFm(); var postContent = LastFm.CreatePostBody(apiMethod, ApiKey, apisig, new Dictionary<string, string>
var postContent = lastfm.CreatePostBody(apiMethod, ApiKey, apisig, new Dictionary<string, string>
{ {
{"password", password}, {"password", password},
{"username", username} {"username", username}
@ -41,9 +44,7 @@ public async Task<UserSession> GetSessionTokenAsync(string username, string pass
var json = await response.Content.ReadAsStringAsync(); var json = await response.Content.ReadAsStringAsync();
var sessionObject = JsonConvert.DeserializeObject<JObject>(json).GetValue("session"); var sessionObject = JsonConvert.DeserializeObject<JObject>(json).GetValue("session");
var session = JsonConvert.DeserializeObject<UserSession>(sessionObject.ToString()); User = JsonConvert.DeserializeObject<UserSession>(sessionObject.ToString());
return session;
} }
} }
} }

View File

@ -0,0 +1,13 @@
using System.Threading.Tasks;
using IF.Lastfm.Core.Objects;
namespace IF.Lastfm.Core.Api
{
public interface IAlbumApi
{
IAuth Auth { get; }
Task<Album> GetAlbumInfoAsync(string artist, string album, bool autocorrect = false);
Task<Album> GetAlbumInfoWithMbidAsync(string mbid, bool autocorrect = false);
}
}

View File

@ -1,10 +1,13 @@
using System.Threading.Tasks; using System.Threading.Tasks;
using IF.Lastfm.Core.Objects;
namespace IF.Lastfm.Core.Api namespace IF.Lastfm.Core.Api
{ {
public interface IAuth public interface IAuth
{ {
bool HasAuthenticated { get; }
string ApiKey { get; } string ApiKey { get; }
UserSession User { get; }
/// <summary> /// <summary>
/// Gets the session token which is used as authentication for any service calls. /// Gets the session token which is used as authentication for any service calls.
@ -13,6 +16,7 @@ public interface IAuth
/// <param name="username">Username</param> /// <param name="username">Username</param>
/// <param name="password">User's password</param> /// <param name="password">User's password</param>
/// <returns>Session token used to authenticate calls to last.fm</returns> /// <returns>Session token used to authenticate calls to last.fm</returns>
Task<UserSession> GetSessionTokenAsync(string username, string password); /// <remarks>API: Auth.getMobileSession</remarks>
Task GetSessionTokenAsync(string username, string password);
} }
} }

View File

@ -39,11 +39,16 @@
<None Include="packages.config" /> <None Include="packages.config" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="Api\AlbumApi.cs" />
<Compile Include="Api\Auth.cs" /> <Compile Include="Api\Auth.cs" />
<Compile Include="Api\IAlbumApi.cs" />
<Compile Include="Json\LastFmBooleanConverter.cs" /> <Compile Include="Json\LastFmBooleanConverter.cs" />
<Compile Include="Api\IAuth.cs" /> <Compile Include="Api\IAuth.cs" />
<Compile Include="ILastFm.cs" /> <Compile Include="ILastFm.cs" />
<Compile Include="LastFm.cs" /> <Compile Include="LastFm.cs" />
<Compile Include="Objects\Album.cs" />
<Compile Include="Objects\Tag.cs" />
<Compile Include="Objects\Track.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Objects\UserSession.cs" /> <Compile Include="Objects\UserSession.cs" />
</ItemGroup> </ItemGroup>

View File

@ -25,7 +25,7 @@ public class LastFm : ILastFm
#region Api helper methods #region Api helper methods
public string FormatApiUrl(string method, string apikey, Dictionary<string, string> parameters = null, bool secure = false) public static string FormatApiUrl(string method, string apikey, Dictionary<string, string> parameters = null, bool secure = false)
{ {
if (parameters == null) if (parameters == null)
{ {
@ -34,7 +34,7 @@ public string FormatApiUrl(string method, string apikey, Dictionary<string, stri
parameters.Add("format", ResponseFormat); parameters.Add("format", ResponseFormat);
var querystring = FormatQueryParameters(parameters.OrderBy(kv => kv.Key)); var querystring = LastFm.FormatQueryParameters(parameters.OrderBy(kv => kv.Key));
var protocol = secure var protocol = secure
? "https" ? "https"
@ -43,7 +43,7 @@ public string FormatApiUrl(string method, string apikey, Dictionary<string, stri
return string.Format(ApiRootFormat, protocol, method, apikey, querystring); return string.Format(ApiRootFormat, protocol, method, apikey, querystring);
} }
public FormUrlEncodedContent CreatePostBody(string method, string apikey, string apisig, public static FormUrlEncodedContent CreatePostBody(string method, string apikey, string apisig,
IEnumerable<KeyValuePair<string, string>> parameters) IEnumerable<KeyValuePair<string, string>> parameters)
{ {
var init = new Dictionary<string, string> var init = new Dictionary<string, string>
@ -60,7 +60,7 @@ public FormUrlEncodedContent CreatePostBody(string method, string apikey, string
} }
public string FormatQueryParameters(IEnumerable<KeyValuePair<string, string>> parameters) public static string FormatQueryParameters(IEnumerable<KeyValuePair<string, string>> parameters)
{ {
const string parameterFormat = "&{0}={1}"; const string parameterFormat = "&{0}={1}";

View File

@ -0,0 +1,55 @@
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace IF.Lastfm.Core.Objects
{
public class Album
{
#region Properties
public string Name { get; set; }
public IEnumerable<Track> Tracks { get; set; }
public string ArtistName { get; set; }
public string ArtistId { get; set; }
public DateTime ReleaseDateUtc { get; set; }
public int ListenerCount { get; set; }
public int TotalPlayCount { get; set; }
public string Mbid { get; set; }
public IEnumerable<Tag> TopTags { get; set; }
public Uri Url { get; set; }
#endregion
/// <summary>
/// TODO datetime parsing
/// TODO images
/// TODO tags
/// TODO tracks
/// </summary>
/// <param name="token"></param>
/// <returns></returns>
public static Album ParseJToken(JToken token)
{
var a = new Album();
a.ArtistName = token.Value<string>("artist");
a.ArtistId = token.Value<string>("id");
a.ListenerCount = token.Value<int>("listeners");
a.Mbid = token.Value<string>("mbid");
a.Name = token.Value<string>("name");
a.TotalPlayCount = token.Value<int>("playcount");
a.Url = new Uri(token.Value<string>("url"), UriKind.Absolute);
return a;
}
}
}

View File

@ -0,0 +1,6 @@
namespace IF.Lastfm.Core.Objects
{
public class Tag
{
}
}

View File

@ -0,0 +1,6 @@
namespace IF.Lastfm.Core.Objects
{
public class Track
{
}
}

View File

@ -1,7 +1,7 @@
using IF.Lastfm.Core.Json; using IF.Lastfm.Core.Json;
using Newtonsoft.Json; using Newtonsoft.Json;
namespace IF.Lastfm.Core namespace IF.Lastfm.Core.Objects
{ {
public class UserSession public class UserSession
{ {

View File

@ -11,7 +11,6 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".nuget", ".nuget", "{81B53C
ProjectSection(SolutionItems) = preProject ProjectSection(SolutionItems) = preProject
.nuget\NuGet.Config = .nuget\NuGet.Config .nuget\NuGet.Config = .nuget\NuGet.Config
.nuget\NuGet.exe = .nuget\NuGet.exe .nuget\NuGet.exe = .nuget\NuGet.exe
.nuget\NuGet.targets = .nuget\NuGet.targets
EndProjectSection EndProjectSection
EndProject EndProject
Global Global