2013-06-23 20:10:57 +01:00
|
|
|
using System;
|
2013-09-01 20:52:44 +01:00
|
|
|
using System.Linq;
|
2013-06-23 20:10:57 +01:00
|
|
|
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-06-23 20:10:57 +01:00
|
|
|
|
|
|
|
namespace IF.Lastfm.Core.Api.Commands
|
|
|
|
{
|
2013-09-01 23:57:54 +01:00
|
|
|
public abstract class GetAsyncCommandBase<T> : LastAsyncCommandBase<T> where T : LastResponse, new()
|
2013-09-01 20:52:44 +01:00
|
|
|
{
|
2013-06-23 20:10:57 +01:00
|
|
|
protected GetAsyncCommandBase(IAuth auth)
|
|
|
|
{
|
|
|
|
Auth = auth;
|
|
|
|
}
|
|
|
|
|
2013-09-01 20:52:44 +01:00
|
|
|
public async override Task<T> ExecuteAsync()
|
2013-06-23 20:10:57 +01:00
|
|
|
{
|
2013-09-01 20:52:44 +01:00
|
|
|
SetParameters();
|
|
|
|
|
|
|
|
EscapeParameters();
|
|
|
|
|
2013-07-24 01:09:33 +01:00
|
|
|
Url = BuildRequestUrl();
|
|
|
|
|
2013-09-01 23:57:54 +01:00
|
|
|
try
|
|
|
|
{
|
|
|
|
var httpClient = new HttpClient();
|
|
|
|
var response = await httpClient.GetAsync(Url);
|
|
|
|
return await HandleResponse(response);
|
|
|
|
}
|
|
|
|
catch (HttpRequestException)
|
|
|
|
{
|
|
|
|
if (LastFm.CatchRequestExceptions)
|
|
|
|
{
|
|
|
|
return LastResponse.CreateErrorResponse<T>(LastFmApiError.RequestFailed);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
}
|
2013-06-23 20:10:57 +01:00
|
|
|
}
|
|
|
|
|
2013-09-01 20:52:44 +01:00
|
|
|
protected override Uri BuildRequestUrl()
|
2013-06-23 20:10:57 +01:00
|
|
|
{
|
2013-09-01 20:52:44 +01:00
|
|
|
var apiUrl = LastFm.FormatApiUrl(Method, Auth.ApiKey, Parameters);
|
|
|
|
return new Uri(apiUrl, UriKind.Absolute);
|
2013-06-23 20:10:57 +01:00
|
|
|
}
|
2013-07-24 01:48:55 +01:00
|
|
|
|
2013-09-01 20:52:44 +01:00
|
|
|
private void EscapeParameters()
|
2013-07-24 01:48:55 +01:00
|
|
|
{
|
2013-09-01 20:52:44 +01:00
|
|
|
foreach (var key in Parameters.Keys.ToList())
|
|
|
|
{
|
|
|
|
Parameters[key] = Uri.EscapeDataString(Parameters[key]);
|
|
|
|
}
|
2013-07-24 01:48:55 +01:00
|
|
|
}
|
2013-06-23 20:10:57 +01:00
|
|
|
}
|
|
|
|
}
|