diff --git a/IF.Lastfm.Core/Api/ArtistApi.cs b/IF.Lastfm.Core/Api/ArtistApi.cs new file mode 100644 index 0000000..202c4f4 --- /dev/null +++ b/IF.Lastfm.Core/Api/ArtistApi.cs @@ -0,0 +1,98 @@ +using System; +using System.Threading.Tasks; +using IF.Lastfm.Core.Api.Commands.ArtistApi; +using IF.Lastfm.Core.Api.Helpers; +using IF.Lastfm.Core.Objects; + +namespace IF.Lastfm.Core.Api +{ + public class ArtistApi : IArtistApi + { + public IAuth Auth { get; private set; } + + public ArtistApi(Auth auth) + { + Auth = auth; + } + + #region artist.getInfo + + public async Task> GetArtistInfoAsync(string artist, + string bioLang = LastFm.DefaultLanguageCode, + bool autocorrect = false) + { + var command = new GetArtistInfoCommand(Auth, artist) + { + BioLanguage = bioLang, + Autocorrect = autocorrect + }; + + return await command.ExecuteAsync(); + } + + public async Task> GetArtistInfoWithMbidAsync(string mbid, + string bioLang = LastFm.DefaultLanguageCode, + bool autocorrect = false) + { + throw new NotImplementedException(); + } + + #endregion + + #region artist.getTopAlbums + + public async Task> GetTopAlbumsForArtistAsync(string artist, + bool autocorrect = false, + int page = 1, + int itemsPerPage = LastFm.DefaultPageLength) + { + throw new NotImplementedException(); + } + + public async Task> GetTopAlbumsForArtistWithMbidAsync(string mbid, + bool autocorrect = false, + int page = 1, + int itemsPerPage = LastFm.DefaultPageLength) + { + throw new NotImplementedException(); + } + + #endregion + + #region artist.getTags + + public async Task> GetUserTagsForArtistAsync(string artist, + string username, + bool autocorrect = false, + int page = 1, + int itemsPerPage = LastFm.DefaultPageLength) + { + throw new NotImplementedException(); + } + + public async Task> GetUserTagsForArtistWithMbidAsync(string mbid, + string username, + bool autocorrect = false, + int page = 1, + int itemsPerPage = LastFm.DefaultPageLength) + { + throw new NotImplementedException(); + } + + #endregion + + #region artist.getTopTags + + public async Task> GetTopTagsForArtistAsync(string artist, bool autocorrect = false) + { + throw new NotImplementedException(); + } + + public async Task> GetTopTagsForArtistWithMbidAsync(string mbid, bool autocorrect = false) + { + throw new NotImplementedException(); + } + + #endregion + } +} \ No newline at end of file diff --git a/IF.Lastfm.Core/Api/Commands/ArtistApi/GetArtistInfoCommand.cs b/IF.Lastfm.Core/Api/Commands/ArtistApi/GetArtistInfoCommand.cs new file mode 100644 index 0000000..9f126c5 --- /dev/null +++ b/IF.Lastfm.Core/Api/Commands/ArtistApi/GetArtistInfoCommand.cs @@ -0,0 +1,61 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net.Http; +using System.Text; +using System.Threading.Tasks; +using IF.Lastfm.Core.Api.Enums; +using IF.Lastfm.Core.Api.Helpers; +using IF.Lastfm.Core.Objects; +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; + +namespace IF.Lastfm.Core.Api.Commands.ArtistApi +{ + internal class GetArtistInfoCommand : GetAsyncCommandBase> + { + public string ArtistName { get; set; } + public string BioLanguage { get; set; } + public bool Autocorrect { get; set; } + + public GetArtistInfoCommand(IAuth auth, string artistname) + : base(auth) + { + Method = "artist.getInfo"; + ArtistName = artistname; + } + + public async override Task> ExecuteAsync() + { + var parameters = new Dictionary + { + {"artist", ArtistName}, + {"autocorrect", Convert.ToInt32(Autocorrect).ToString()} + }; + + var apiUrl = LastFm.FormatApiUrl(Method, Auth.ApiKey, parameters); + Url = new Uri(apiUrl, UriKind.Absolute); + + return await ExecuteInternal(); + } + + public async override Task> HandleResponse(HttpResponseMessage response) + { + string json = await response.Content.ReadAsStringAsync(); + + LastFmApiError error; + if (LastFm.IsResponseValid(json, out error) && response.IsSuccessStatusCode) + { + var jtoken = JsonConvert.DeserializeObject(json); + + var artist = Artist.ParseJToken(jtoken.SelectToken("artist")); + + return LastResponse.CreateSuccessResponse(artist); + } + else + { + return LastResponse.CreateErrorResponse(error); + } + } + } +} diff --git a/IF.Lastfm.Core/IF.Lastfm.Core.csproj b/IF.Lastfm.Core/IF.Lastfm.Core.csproj index 24af77a..04d5865 100644 --- a/IF.Lastfm.Core/IF.Lastfm.Core.csproj +++ b/IF.Lastfm.Core/IF.Lastfm.Core.csproj @@ -40,7 +40,9 @@ + + @@ -107,7 +109,6 @@ - diff --git a/IF.Lastfm.Core/Objects/Artist.cs b/IF.Lastfm.Core/Objects/Artist.cs index 222885d..c384c1e 100644 --- a/IF.Lastfm.Core/Objects/Artist.cs +++ b/IF.Lastfm.Core/Objects/Artist.cs @@ -1,9 +1,54 @@ -using Newtonsoft.Json.Linq; +using System; +using System.Collections; +using System.Collections.Generic; +using System.Linq; +using IF.Lastfm.Core.Api.Helpers; +using Newtonsoft.Json.Linq; namespace IF.Lastfm.Core.Objects { + /// + /// Todo bio, tour, similar, stats, streamable + /// public class Artist { + #region Properties + + public string Name { get; set; } + public string Mbid { get; set; } + public Uri Url { get; set; } + public bool OnTour { get; set; } + public IEnumerable Tags { get; set; } + public LastImageCollection Images { get; set; } + + #endregion + + internal static Artist ParseJToken(JToken token) + { + var a = new Artist(); + + a.Name = token.Value("name"); + a.Mbid = token.Value("mbid"); + a.Url = new Uri(token.Value("url"), UriKind.Absolute); + + a.OnTour = Convert.ToBoolean(token.Value("ontour")); + + var tagsToken = token.SelectToken("tags"); + if (tagsToken != null) + { + a.Tags = tagsToken.SelectToken("tag").Children().Select(Tag.ParseJToken); + } + + var images = token.SelectToken("image"); + if (images != null) + { + var imageCollection = LastImageCollection.ParseJToken(images); + a.Images = imageCollection; + } + + return a; + } + internal static string GetNameFromJToken(JToken artistToken) { var name = artistToken.Value("name"); diff --git a/IF.Lastfm.Demo.Apollo/IF.Lastfm.Demo.Apollo.csproj b/IF.Lastfm.Demo.Apollo/IF.Lastfm.Demo.Apollo.csproj index 8124ffc..7dece94 100644 --- a/IF.Lastfm.Demo.Apollo/IF.Lastfm.Demo.Apollo.csproj +++ b/IF.Lastfm.Demo.Apollo/IF.Lastfm.Demo.Apollo.csproj @@ -102,6 +102,9 @@ MainPage.xaml + + GetArtistInfo.xaml + RecentStations.xaml @@ -112,6 +115,7 @@ True AppResources.resx + History.xaml @@ -137,6 +141,10 @@ Designer MSBuild:Compile + + MSBuild:Compile + Designer + MSBuild:Compile Designer diff --git a/IF.Lastfm.Demo.Apollo/Pages/ApiTest.xaml b/IF.Lastfm.Demo.Apollo/Pages/ApiTest.xaml index 7517771..dd47675 100644 --- a/IF.Lastfm.Demo.Apollo/Pages/ApiTest.xaml +++ b/IF.Lastfm.Demo.Apollo/Pages/ApiTest.xaml @@ -39,6 +39,7 @@