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

53 lines
1.5 KiB
C#
Raw Normal View History

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>
{
public string Method { get; protected set; }
2013-07-23 22:46:29 +01:00
public Uri Url { get; private set; }
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);
}
public abstract Task<T> ExecuteAsync();
protected async Task<T> ExecuteInternal(Dictionary<string, string> parameters)
{
2013-07-23 22:46:29 +01:00
Url = BuildRequestUrl();
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());
}
}
}