IF.Lastfm/IF.Lastfm.Core/Api/Commands/PostAsyncCommandBase.cs

55 lines
1.5 KiB
C#
Raw Normal View History

using System;
using System.Net.Http;
using System.Threading.Tasks;
using IF.Lastfm.Core.Api.Enums;
using IF.Lastfm.Core.Api.Helpers;
namespace IF.Lastfm.Core.Api.Commands
{
internal abstract class PostAsyncCommandBase<T> : LastAsyncCommandBase<T> where T : LastResponse, new()
{
protected PostAsyncCommandBase(IAuth auth)
{
Auth = auth;
2013-07-23 22:46:29 +01:00
}
protected override Uri BuildRequestUrl()
2013-07-23 22:46:29 +01:00
{
return new Uri(LastFm.ApiRoot, UriKind.Absolute);
}
public override async Task<T> ExecuteAsync()
{
SetParameters();
2013-07-23 22:46:29 +01:00
Url = BuildRequestUrl();
Parameters.Add("sk", Auth.User.Token);
var apisig = Auth.GenerateMethodSignature(Method, Parameters);
var postContent = LastFm.CreatePostBody(Method,
Auth.ApiKey,
apisig,
Parameters);
try
{
var httpClient = new HttpClient();
var response = await httpClient.PostAsync(Url, postContent);
return await HandleResponse(response);
}
catch (HttpRequestException)
{
if (LastFm.CatchRequestExceptions)
{
return LastResponse.CreateErrorResponse<T>(LastFmApiError.RequestFailed);
}
else
{
throw;
}
}
}
}
}