2013-07-23 22:34:51 +01:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Net.Http;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
namespace IF.Lastfm.Core.Api.Commands
|
|
|
|
{
|
2013-07-23 22:46:29 +01:00
|
|
|
internal abstract class PostAsyncCommandBase<T> : IAsyncCommand<T>
|
2013-07-23 22:34:51 +01:00
|
|
|
{
|
|
|
|
public string Method { get; protected set; }
|
2013-07-23 22:46:29 +01:00
|
|
|
public Uri Url { get; private set; }
|
2013-07-23 22:34:51 +01:00
|
|
|
public IAuth Auth { get; protected set; }
|
|
|
|
|
|
|
|
public int Page { get; set; }
|
|
|
|
public int Count { get; set; }
|
|
|
|
|
|
|
|
protected PostAsyncCommandBase(IAuth auth)
|
|
|
|
{
|
|
|
|
Auth = auth;
|
2013-07-23 22:46:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public Uri BuildRequestUrl()
|
|
|
|
{
|
|
|
|
return new Uri(LastFm.ApiRoot, UriKind.Absolute);
|
2013-07-23 22:34:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public abstract Task<T> ExecuteAsync();
|
|
|
|
|
|
|
|
protected async Task<T> ExecuteInternal(Dictionary<string, string> parameters)
|
|
|
|
{
|
2013-07-23 22:46:29 +01:00
|
|
|
Url = BuildRequestUrl();
|
|
|
|
|
2013-07-23 22:34:51 +01:00
|
|
|
var apisig = Auth.GenerateMethodSignature(Method, parameters);
|
|
|
|
|
|
|
|
var postContent = LastFm.CreatePostBody(Method,
|
|
|
|
Auth.ApiKey,
|
|
|
|
apisig,
|
|
|
|
parameters);
|
|
|
|
|
|
|
|
var httpClient = new HttpClient();
|
|
|
|
var response = await httpClient.PostAsync(Url, postContent);
|
|
|
|
return await HandleResponse(response);
|
|
|
|
}
|
|
|
|
|
|
|
|
public abstract Task<T> HandleResponse(HttpResponseMessage response);
|
|
|
|
|
|
|
|
protected void AddPagingParameters(Dictionary<string, string> parameters)
|
|
|
|
{
|
|
|
|
parameters.Add("page", Page.ToString());
|
|
|
|
parameters.Add("limit", Count.ToString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|