2013-06-11 17:40:22 +01:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Net.Http;
|
|
|
|
|
using System.Threading.Tasks;
|
2013-06-13 21:30:18 +01:00
|
|
|
|
using IF.Lastfm.Core.Api.Enums;
|
2013-06-11 17:40:22 +01:00
|
|
|
|
using IF.Lastfm.Core.Objects;
|
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
|
|
|
|
|
|
namespace IF.Lastfm.Core.Api
|
|
|
|
|
{
|
|
|
|
|
public class AlbumApi : IAlbumApi
|
|
|
|
|
{
|
|
|
|
|
public IAuth Auth { get; private set; }
|
|
|
|
|
|
|
|
|
|
public AlbumApi(IAuth auth)
|
|
|
|
|
{
|
|
|
|
|
Auth = auth;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<Album> GetAlbumInfoAsync(string artistname, string albumname, bool autocorrect = false)
|
|
|
|
|
{
|
|
|
|
|
const string apiMethod = "album.getInfo";
|
|
|
|
|
|
|
|
|
|
var parameters = new Dictionary<string, string>
|
|
|
|
|
{
|
|
|
|
|
{"artist", artistname},
|
|
|
|
|
{"album", albumname},
|
|
|
|
|
{"autocorrect", Convert.ToInt32(autocorrect).ToString()}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var httpClient = new HttpClient();
|
2013-06-13 21:30:18 +01:00
|
|
|
|
var apiUrl = LastFm.FormatApiUrl(apiMethod, Auth.ApiKey, parameters);
|
2013-06-11 17:40:22 +01:00
|
|
|
|
var lastResponse = await httpClient.GetAsync(apiUrl);
|
2013-06-13 21:30:18 +01:00
|
|
|
|
var json = await lastResponse.Content.ReadAsStringAsync();
|
2013-06-11 17:40:22 +01:00
|
|
|
|
|
2013-06-13 21:30:18 +01:00
|
|
|
|
LastFmApiError error;
|
|
|
|
|
if (LastFm.IsResponseValid(json, out error) && lastResponse.IsSuccessStatusCode)
|
2013-06-11 17:40:22 +01:00
|
|
|
|
{
|
|
|
|
|
var jtoken = JsonConvert.DeserializeObject<JToken>(json);
|
|
|
|
|
|
|
|
|
|
var album = Album.ParseJToken(jtoken.SelectToken("album"));
|
|
|
|
|
|
|
|
|
|
return album;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2013-06-13 21:30:18 +01:00
|
|
|
|
LastFm.HandleError(error);
|
|
|
|
|
return null; // an exception is gon' be thrown but the compiler needs this
|
2013-06-11 17:40:22 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<Album> GetAlbumInfoWithMbidAsync(string mbid, bool autocorrect = false)
|
|
|
|
|
{
|
|
|
|
|
throw new System.NotImplementedException();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|