diff --git a/IF.Lastfm.Core.Tests/MockLastFm.cs b/IF.Lastfm.Core.Tests/MockLastFm.cs index 0fb99ac..a4b0b56 100644 --- a/IF.Lastfm.Core.Tests/MockLastFm.cs +++ b/IF.Lastfm.Core.Tests/MockLastFm.cs @@ -5,18 +5,11 @@ namespace IF.Lastfm.Core.Tests { public class MockLastFm { - public LastFm Object { get; set; } - public Mock Auth { get; set; } public MockLastFm() { Auth = new Mock(); - - Object = new LastFm - { - Auth = Auth.Object - }; } } } \ No newline at end of file diff --git a/IF.Lastfm.Core/Api/Auth.cs b/IF.Lastfm.Core/Api/Auth.cs index 35315d9..c3c89c4 100644 --- a/IF.Lastfm.Core/Api/Auth.cs +++ b/IF.Lastfm.Core/Api/Auth.cs @@ -1,24 +1,17 @@ using System.Collections.Generic; using System.Linq; -using System.Net.Http; using System.Text; using System.Threading.Tasks; -using IF.Lastfm.Core.Api.Enums; +using IF.Lastfm.Core.Api.Commands.AuthApi; using IF.Lastfm.Core.Api.Helpers; using IF.Lastfm.Core.Objects; -using Newtonsoft.Json; -using Newtonsoft.Json.Linq; using xBrainLab.Security.Cryptography; namespace IF.Lastfm.Core.Api { public class Auth : IAuth { - private const string ApiAuthMethod = "auth.getMobileSession"; - private readonly string _apiSecret; - private string _password; - private string _username; public bool HasAuthenticated { get { return User != null; } } public string ApiKey { get; private set; } @@ -43,35 +36,17 @@ public bool LoadSession(UserSession session) public async Task GetSessionTokenAsync(string username, string password) { - const string apiMethod = "auth.getMobileSession"; + var command = new GetMobileSessionCommand(this, username, password); + var response = await command.ExecuteAsync(); - _password = password; - _username = username; - - var apisig = GenerateMethodSignature(apiMethod); - - var postContent = LastFm.CreatePostBody(apiMethod, ApiKey, apisig, new Dictionary - { - {"password", password}, - {"username", username} - }); - - var httpClient = new HttpClient(); - HttpResponseMessage response = await httpClient.PostAsync("https://ws.audioscrobbler.com/2.0/", postContent); - string json = await response.Content.ReadAsStringAsync(); - - LastFmApiError error; - if (LastFm.IsResponseValid(json, out error) && response.IsSuccessStatusCode) + if (response.Success) { - var sessionObject = JsonConvert.DeserializeObject(json).GetValue("session"); - - User = JsonConvert.DeserializeObject(sessionObject.ToString()); - + User = response.Content; return LastResponse.CreateSuccessResponse(); } else { - return LastResponse.CreateErrorResponse(error); + return LastResponse.CreateErrorResponse(response.Error); } } @@ -84,8 +59,6 @@ public string GenerateMethodSignature(string method, Dictionary parameters.Add("api_key", ApiKey); parameters.Add("method", method); - parameters.Add("password", _password); - parameters.Add("username", _username); var builder = new StringBuilder(); diff --git a/IF.Lastfm.Core/Api/Commands/AuthApi/GetMobileSessionCommand.cs b/IF.Lastfm.Core/Api/Commands/AuthApi/GetMobileSessionCommand.cs new file mode 100644 index 0000000..34bf981 --- /dev/null +++ b/IF.Lastfm.Core/Api/Commands/AuthApi/GetMobileSessionCommand.cs @@ -0,0 +1,54 @@ +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.AuthApi +{ + public class GetMobileSessionCommand : PostAsyncCommandBase> + { + public string Username { get; set; } + public string Password { get; set; } + + public GetMobileSessionCommand(IAuth auth, string username, string password) : base(auth) + { + Method = "auth.getMobileSession"; + Username = username; + Password = password; + } + + protected override Uri BuildRequestUrl() + { + return new Uri(LastFm.ApiRootSsl, UriKind.Absolute); + } + + public override void SetParameters() + { + Parameters.Add("username", Username); + Parameters.Add("password", Password); + } + + public async override Task> HandleResponse(HttpResponseMessage response) + { + string json = await response.Content.ReadAsStringAsync(); + + LastFmApiError error; + if (LastFm.IsResponseValid(json, out error) && response.IsSuccessStatusCode) + { + var sessionObject = JsonConvert.DeserializeObject(json).GetValue("session"); + + var session = JsonConvert.DeserializeObject(sessionObject.ToString()); + + return LastResponse.CreateSuccessResponse(session); + } + else + { + return LastResponse.CreateErrorResponse>(error); + } + } + } +} diff --git a/IF.Lastfm.Core/Api/Commands/PostAsyncCommandBase.cs b/IF.Lastfm.Core/Api/Commands/PostAsyncCommandBase.cs index d9955cc..0e41a82 100644 --- a/IF.Lastfm.Core/Api/Commands/PostAsyncCommandBase.cs +++ b/IF.Lastfm.Core/Api/Commands/PostAsyncCommandBase.cs @@ -6,7 +6,7 @@ namespace IF.Lastfm.Core.Api.Commands { - internal abstract class PostAsyncCommandBase : LastAsyncCommandBase where T : LastResponse, new() + public abstract class PostAsyncCommandBase : LastAsyncCommandBase where T : LastResponse, new() { protected PostAsyncCommandBase(IAuth auth) { @@ -24,7 +24,10 @@ public override async Task ExecuteAsync() Url = BuildRequestUrl(); - Parameters.Add("sk", Auth.User.Token); + if (Auth.HasAuthenticated) + { + Parameters.Add("sk", Auth.User.Token); + } var apisig = Auth.GenerateMethodSignature(Method, Parameters); diff --git a/IF.Lastfm.Core/IF.Lastfm.Core.csproj b/IF.Lastfm.Core/IF.Lastfm.Core.csproj index 9c823be..2ad5e39 100644 --- a/IF.Lastfm.Core/IF.Lastfm.Core.csproj +++ b/IF.Lastfm.Core/IF.Lastfm.Core.csproj @@ -47,6 +47,7 @@ + diff --git a/IF.Lastfm.Core/LastFm.cs b/IF.Lastfm.Core/LastFm.cs index c883cd6..d95505b 100644 --- a/IF.Lastfm.Core/LastFm.cs +++ b/IF.Lastfm.Core/LastFm.cs @@ -14,6 +14,7 @@ public class LastFm #region Constants public const string ApiRoot = "http://ws.audioscrobbler.com/2.0/"; + public const string ApiRootSsl = "https://ws.audioscrobbler.com/2.0/"; private const string ApiRootFormat = "{0}://ws.audioscrobbler.com/2.0/?method={1}&api_key={2}{3}"; private const string ResponseFormat = "json";