/// <summary> /// Creates an IResultList for the paging object located in the response to a Spotify URI. /// </summary> /// <returns>The IResultList.</returns> /// <param name="uri">The entire Spotify request URI (including the BASE_ADDRESS).</param> /// <param name="isWrapped"> /// If set to <c>true</c>, the paging object is wrapped in a SearchResult, which actually contains multiple /// paging objects. We deduce which one to use based on type parameter Q. /// </param> /// <typeparam name="T">The internal data type; an instance of IMetadata.</typeparam> /// <typeparam name="Q">The json data type; an instance of IMetadataResult.</typeparam> public IResultList <T> PagingUri <T, Q>(string uri, bool isWrapped) where T : IMetadata where Q : IMetadataResult { var task = Task.Run(async() => { return(await GetJsonFullUri(uri)); }); var json = task.Result; PagingObjectResult <Q> theList; if (isWrapped) { var jsonResult = JsonConvert.DeserializeObject <SearchResult <Q> >(json); theList = PagingObjectResult <Q> .CastTypeParam <Q>(jsonResult.Items); } else { theList = JsonConvert.DeserializeObject <PagingObjectResult <Q> >(json); } // Store metadata in the cache so we can use it, if possible foreach (var item in theList.items) { metadata.CacheSubmit(item); } var search = new PagingWrapper <T, Q>(theList, this, isWrapped); return(search); }
internal PagingWrapper(PagingObjectResult <Q> result, WebApi webApi, bool isWrapped) { this.webApi = webApi; this.isWrapped = isWrapped; this.delayedLoad = null; this.firstResult = result.offset; this.nextPage = result.next; this.prevPage = result.previous; this.items = new List <T>(); var uids = new List <string>(); foreach (var item in result.items) { uids.Add(item.uri); } if (typeof(T) == typeof(ISong)) { var songs = webApi.metadata.GetSongs(uids); foreach (var song in songs) { this.items.Add((T)song); } } else if (typeof(T) == typeof(IAlbum)) { // TODO throw new NotImplementedException(); } else if (typeof(T) == typeof(IArtist)) { // TODO throw new NotImplementedException(); } else if (typeof(T) == typeof(IPlaylist)) { var playlists = webApi.metadata.GetPlaylists(uids); foreach (var pl in playlists) { this.items.Add((T)pl); } } else { throw new Exception("Invalid type T: " + typeof(T).Name); } }
internal SpotifyPlaylist(MetadataFactory factory, PlaylistResult result, WebApi webApi) { this.factory = factory; this.uri = result.uri; this.name = result.name; PagingObjectResult <PlaylistTrackResult> tracks = result.tracks; var wrap = new PagingWrapper <ISong, PlaylistTrackResult>(tracks, webApi, false); this.page1 = wrap; // Submit items for caching foreach (var playlistTrack in tracks.items) { this.factory.CacheSubmit(playlistTrack); } }
public static PagingObjectResult <T> CastTypeParam <T>(PagingObjectResult <Q> original) where T : IMetadataResult { Debug.Assert(typeof(T) == typeof(Q)); var fresh = new PagingObjectResult <T>(); fresh.href = original.href; fresh.limit = original.limit; fresh.next = original.next; fresh.offset = original.offset; fresh.previous = original.previous; fresh.total = original.total; fresh.items = new List <T>(); foreach (var item in original.items) { // This casting is very kludgy but it works fresh.items.Add((T)(IMetadataResult)item); } return(fresh); }