2013-06-08 18:49:21 +01:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Net.Http;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using IF.Lastfm.Core.Api;
|
2013-06-13 21:30:18 +01:00
|
|
|
|
using IF.Lastfm.Core.Api.Enums;
|
|
|
|
|
using IF.Lastfm.Core.Api.Helpers;
|
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
using Newtonsoft.Json.Linq;
|
2013-06-08 18:49:21 +01:00
|
|
|
|
|
|
|
|
|
namespace IF.Lastfm.Core
|
|
|
|
|
{
|
|
|
|
|
public class LastFm : ILastFm
|
|
|
|
|
{
|
|
|
|
|
#region Constants
|
|
|
|
|
|
|
|
|
|
public const string ApiRoot = "http://ws.audioscrobbler.com/2.0/";
|
|
|
|
|
private const string ApiRootFormat = "{0}://ws.audioscrobbler.com/2.0/?method={1}&api_key={2}{3}";
|
|
|
|
|
|
|
|
|
|
private const string ResponseFormat = "json";
|
2013-06-13 17:27:51 +01:00
|
|
|
|
|
2013-06-14 13:25:02 +01:00
|
|
|
|
public const string DefaultLanguageCode = "en";
|
2013-06-13 17:27:51 +01:00
|
|
|
|
public const int DefaultPageLength = 20;
|
2013-06-08 18:49:21 +01:00
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Api objects
|
|
|
|
|
|
|
|
|
|
public IAuth Auth { get; set; }
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Api helper methods
|
|
|
|
|
|
2013-06-11 17:40:22 +01:00
|
|
|
|
public static string FormatApiUrl(string method, string apikey, Dictionary<string, string> parameters = null, bool secure = false)
|
2013-06-08 18:49:21 +01:00
|
|
|
|
{
|
|
|
|
|
if (parameters == null)
|
|
|
|
|
{
|
|
|
|
|
parameters = new Dictionary<string, string>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
parameters.Add("format", ResponseFormat);
|
|
|
|
|
|
2013-06-11 17:40:22 +01:00
|
|
|
|
var querystring = LastFm.FormatQueryParameters(parameters.OrderBy(kv => kv.Key));
|
2013-06-08 18:49:21 +01:00
|
|
|
|
|
|
|
|
|
var protocol = secure
|
|
|
|
|
? "https"
|
|
|
|
|
: "http";
|
|
|
|
|
|
|
|
|
|
return string.Format(ApiRootFormat, protocol, method, apikey, querystring);
|
|
|
|
|
}
|
|
|
|
|
|
2013-06-11 17:40:22 +01:00
|
|
|
|
public static FormUrlEncodedContent CreatePostBody(string method, string apikey, string apisig,
|
2013-06-08 18:49:21 +01:00
|
|
|
|
IEnumerable<KeyValuePair<string, string>> parameters)
|
|
|
|
|
{
|
|
|
|
|
var init = new Dictionary<string, string>
|
|
|
|
|
{
|
|
|
|
|
{"method", method},
|
|
|
|
|
{"api_key", apikey},
|
|
|
|
|
{"api_sig", apisig},
|
|
|
|
|
{"format", ResponseFormat}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var requestParameters = init.Concat(parameters);
|
|
|
|
|
|
|
|
|
|
return new FormUrlEncodedContent(requestParameters);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2013-06-11 17:40:22 +01:00
|
|
|
|
public static string FormatQueryParameters(IEnumerable<KeyValuePair<string, string>> parameters)
|
2013-06-08 18:49:21 +01:00
|
|
|
|
{
|
|
|
|
|
const string parameterFormat = "&{0}={1}";
|
|
|
|
|
|
|
|
|
|
var builder = new StringBuilder();
|
|
|
|
|
|
|
|
|
|
foreach (var pair in parameters)
|
|
|
|
|
{
|
|
|
|
|
builder.Append(string.Format(parameterFormat, pair.Key, pair.Value));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return builder.ToString();
|
|
|
|
|
}
|
|
|
|
|
|
2013-06-13 21:30:18 +01:00
|
|
|
|
public static bool IsResponseValid(string json, out LastFmApiError error)
|
|
|
|
|
{
|
2013-06-14 16:19:26 +01:00
|
|
|
|
// hmmm
|
2013-06-13 21:30:18 +01:00
|
|
|
|
if (!json.Contains("error"))
|
|
|
|
|
{
|
|
|
|
|
error = LastFmApiError.None;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
error = LastFmApiError.Unknown;
|
|
|
|
|
JObject jo;
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
jo = JsonConvert.DeserializeObject<JObject>(json);
|
|
|
|
|
}
|
|
|
|
|
catch (JsonException)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var code = jo.Value<int>("error");
|
|
|
|
|
|
|
|
|
|
switch (code)
|
|
|
|
|
{
|
|
|
|
|
case 2:
|
|
|
|
|
error = LastFmApiError.ServiceServiceWhereArtThou;
|
|
|
|
|
break;
|
|
|
|
|
case 3:
|
|
|
|
|
error = LastFmApiError.BadMethod;
|
|
|
|
|
break;
|
|
|
|
|
case 4:
|
|
|
|
|
error = LastFmApiError.BadAuth;
|
|
|
|
|
break;
|
|
|
|
|
case 5:
|
|
|
|
|
error = LastFmApiError.BadFormat;
|
|
|
|
|
break;
|
|
|
|
|
case 6:
|
|
|
|
|
error = LastFmApiError.MissingParameters;
|
|
|
|
|
break;
|
|
|
|
|
case 7:
|
|
|
|
|
error = LastFmApiError.BadResource;
|
|
|
|
|
break;
|
|
|
|
|
case 8:
|
|
|
|
|
error = LastFmApiError.Failure;
|
|
|
|
|
break;
|
|
|
|
|
case 9:
|
|
|
|
|
error = LastFmApiError.SessionExpired;
|
|
|
|
|
break;
|
|
|
|
|
case 10:
|
|
|
|
|
error = LastFmApiError.BadApiKey;
|
|
|
|
|
break;
|
|
|
|
|
case 11:
|
|
|
|
|
error = LastFmApiError.ServiceDown;
|
|
|
|
|
break;
|
|
|
|
|
case 13:
|
|
|
|
|
error = LastFmApiError.BadMethodSignature;
|
|
|
|
|
break;
|
|
|
|
|
case 16:
|
|
|
|
|
error = LastFmApiError.TemporaryFailure;
|
|
|
|
|
break;
|
|
|
|
|
case 26:
|
|
|
|
|
error = LastFmApiError.KeySuspended;
|
|
|
|
|
break;
|
|
|
|
|
case 29:
|
|
|
|
|
error = LastFmApiError.RateLimited;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2013-06-08 18:49:21 +01:00
|
|
|
|
#endregion
|
|
|
|
|
}
|
|
|
|
|
}
|