2013-07-23 22:34:51 +01:00
|
|
|
using System;
|
|
|
|
using System.Net.Http;
|
|
|
|
using System.Threading.Tasks;
|
2013-09-01 23:57:54 +01:00
|
|
|
using IF.Lastfm.Core.Api.Enums;
|
|
|
|
using IF.Lastfm.Core.Api.Helpers;
|
2013-07-23 22:34:51 +01:00
|
|
|
|
|
|
|
namespace IF.Lastfm.Core.Api.Commands
|
|
|
|
{
|
2013-09-01 23:57:54 +01:00
|
|
|
internal abstract class PostAsyncCommandBase<T> : LastAsyncCommandBase<T> where T : LastResponse, new()
|
2013-07-23 22:34:51 +01:00
|
|
|
{
|
|
|
|
protected PostAsyncCommandBase(IAuth auth)
|
|
|
|
{
|
|
|
|
Auth = auth;
|
2013-07-23 22:46:29 +01:00
|
|
|
}
|
|
|
|
|
2013-09-01 20:52:44 +01:00
|
|
|
protected override Uri BuildRequestUrl()
|
2013-07-23 22:46:29 +01:00
|
|
|
{
|
|
|
|
return new Uri(LastFm.ApiRoot, UriKind.Absolute);
|
2013-07-23 22:34:51 +01:00
|
|
|
}
|
|
|
|
|
2013-09-01 20:52:44 +01:00
|
|
|
public override async Task<T> ExecuteAsync()
|
2013-07-23 22:34:51 +01:00
|
|
|
{
|
2013-09-01 20:52:44 +01:00
|
|
|
SetParameters();
|
|
|
|
|
2013-07-23 22:46:29 +01:00
|
|
|
Url = BuildRequestUrl();
|
|
|
|
|
2013-09-01 20:52:44 +01:00
|
|
|
Parameters.Add("sk", Auth.User.Token);
|
2013-07-24 01:14:22 +01:00
|
|
|
|
2013-09-01 20:52:44 +01:00
|
|
|
var apisig = Auth.GenerateMethodSignature(Method, Parameters);
|
2013-07-23 22:34:51 +01:00
|
|
|
|
|
|
|
var postContent = LastFm.CreatePostBody(Method,
|
|
|
|
Auth.ApiKey,
|
|
|
|
apisig,
|
2013-09-01 20:52:44 +01:00
|
|
|
Parameters);
|
2013-07-23 22:34:51 +01:00
|
|
|
|
2013-09-01 23:57:54 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2013-07-23 22:34:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|