-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from tolbxela/master
Auth methods
- Loading branch information
Showing
13 changed files
with
212 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
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.Auth | ||
{ | ||
[ApiMethodName("auth.getSession")] | ||
internal class GetSessionCommand : UnauthenticatedPostAsyncCommandBase<LastResponse<LastUserSession>> | ||
{ | ||
private string Token { get; } | ||
|
||
public GetSessionCommand(ILastAuth auth, string authToken) : base(auth) | ||
{ | ||
Token = authToken; | ||
} | ||
|
||
protected override Uri BuildRequestUrl() | ||
{ | ||
return new Uri(LastFm.ApiRootSsl, UriKind.Absolute); | ||
} | ||
|
||
public override void SetParameters() | ||
{ | ||
Parameters.Add("token", Token); | ||
} | ||
|
||
public override async Task<LastResponse<LastUserSession>> HandleResponse(HttpResponseMessage response) | ||
{ | ||
var json = await response.Content.ReadAsStringAsync(); | ||
|
||
if (LastFm.IsResponseValid(json, out LastResponseStatus status) && response.IsSuccessStatusCode) | ||
{ | ||
var sessionObject = JsonConvert.DeserializeObject<JObject>(json).GetValue("session"); | ||
var session = JsonConvert.DeserializeObject<LastUserSession>(sessionObject.ToString()); | ||
|
||
return LastResponse<LastUserSession>.CreateSuccessResponse(session); | ||
} | ||
else | ||
{ | ||
return LastResponse.CreateErrorResponse<LastResponse<LastUserSession>>(status); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
using System; | ||
using System.Net.Http; | ||
using System.Threading.Tasks; | ||
using IF.Lastfm.Core.Api.Enums; | ||
using IF.Lastfm.Core.Api.Helpers; | ||
using Newtonsoft.Json; | ||
using Newtonsoft.Json.Linq; | ||
|
||
namespace IF.Lastfm.Core.Api.Commands.Auth | ||
{ | ||
[ApiMethodName("auth.getToken")] | ||
internal class GetTokenCommand : UnauthenticatedPostAsyncCommandBase<LastResponse<string>> | ||
{ | ||
public GetTokenCommand(ILastAuth auth) : base(auth) | ||
{ | ||
} | ||
|
||
protected override Uri BuildRequestUrl() | ||
{ | ||
return new Uri(LastFm.ApiRootSsl, UriKind.Absolute); | ||
} | ||
|
||
public override void SetParameters() | ||
{ | ||
} | ||
|
||
public override async Task<LastResponse<string>> HandleResponse(HttpResponseMessage response) | ||
{ | ||
var json = await response.Content.ReadAsStringAsync(); | ||
|
||
if (LastFm.IsResponseValid(json, out LastResponseStatus status) && response.IsSuccessStatusCode) | ||
{ | ||
var token = JsonConvert.DeserializeObject<JObject>(json).GetValue("token"); | ||
return LastResponse<string>.CreateSuccessResponse(token.Value<string>()); | ||
} | ||
else | ||
{ | ||
return LastResponse.CreateErrorResponse<LastResponse<string>>(status); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.