forked from inflatablefriends/lastfm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement command auth.getToken (inflatablefriends#154)
- Loading branch information
Showing
3 changed files
with
61 additions
and
1 deletion.
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
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