using Newtonsoft.Json; using Sony.Filtr.ApolloAPI.Models.Apple; using System; using System.Collections.Generic; using System.Linq; using System.Net.Http; using System.Threading.Tasks; using System.Web; namespace Sony.Filtr.ApolloAPI { public class ApolloAppleWebApi : ApolloWebApiBase, IApolloAppleWebApi { public const int TrackDefaultPaginationLimit = 100; public const int TrackMaxPaginationLimit = 300; public ApolloAppleWebApi(HttpClient httpClient) : base(httpClient) { } public Task GetPlaylistWithFullTracklistAsync(string storefront, string playlistId) { return this.GetPlaylistWithFullTracklistAsync(storefront, playlistId, NoCacheTime); } public async Task GetPlaylistWithFullTracklistAsync(string storefront, string playlistId, TimeSpan cacheExpiration) { var url = $"vendor-api/apple/v1/catalog/{storefront}/playlists/{playlistId}"; var response = await this.GetFromApi(url, cacheExpiration); var playlist = response?.data?.FirstOrDefault(); if (playlist?.relationships?.tracks?.data?.Count >= TrackDefaultPaginationLimit) { playlist.relationships.tracks.data = await GetPlaylistTracksAsync(storefront, playlistId, cacheExpiration); } return playlist; } public Task> GetPlaylistsWithFullTracklistsAsync(string storefront, IEnumerable playlistIds) { return this.GetPlaylistsWithFullTracklistsAsync(storefront, playlistIds, NoCacheTime); } public async Task> GetPlaylistsWithFullTracklistsAsync(string storefront, IEnumerable playlistIds, TimeSpan cacheExpiration) { if (!playlistIds.Any()) { return new List(); } var url = $"vendor-api/apple/v1/catalog/{storefront}/playlists?ids={string.Join(",", playlistIds)}"; var playlistResponse = await base.GetFromApi(url, r => JsonConvert.DeserializeObject(r), cacheExpiration); if (playlistResponse?.data != null && playlistResponse.data.Any(x => x.relationships?.tracks?.data?.Count >= TrackDefaultPaginationLimit)) { foreach (var playlistData in playlistResponse.data.Where(x => x?.relationships?.tracks?.data.Count >= TrackDefaultPaginationLimit)) { playlistData.relationships.tracks.data = await GetPlaylistTracksAsync(storefront, playlistData.id, cacheExpiration); } } return playlistResponse?.data; } public Task> GetPlaylistTracksAsync(string storefront, string playlistId) { return this.GetPlaylistTracksAsync(storefront, playlistId, NoCacheTime); } public async Task> GetPlaylistTracksAsync(string storefront, string playlistId, TimeSpan cacheExpiration) { var tracks = new List(); var url = $"vendor-api/apple/v1/catalog/{storefront}/playlists/{playlistId}/tracks?limit={TrackMaxPaginationLimit}"; do { var response = await this.GetFromApi(url, cacheExpiration); tracks.AddRange(response.Data); url = null; if (response.Next != null) { var parameters = HttpUtility.ParseQueryString(response.Next); parameters.Add("limit", TrackMaxPaginationLimit.ToString()); url = $"vendor-api/apple/{parameters.ToString()}"; } } while (url != null); return tracks; } public Task> GetSongsAsync(string storefrontId, IEnumerable songIds) { return this.GetSongsAsync(storefrontId, songIds, NoCacheTime); } public async Task> GetSongsAsync(string storefrontId, IEnumerable songIds, TimeSpan cacheExpiration) { var url = $"vendor-api/apple/v1/catalog/{storefrontId}/songs?ids={String.Join(",", songIds)}"; var response = await this.GetFromApi(url, cacheExpiration); return response.data; } public Task> GetArtistsAsync(string storefrontId, IEnumerable artistIds) { return this.GetArtistsAsync(storefrontId, artistIds, NoCacheTime); } public async Task> GetArtistsAsync(string storefrontId, IEnumerable artistIds, TimeSpan cacheExpiration) { var url = $"vendor-api/apple/v1/catalog/{storefrontId}/artists?ids={String.Join(",", artistIds)}"; var response = await this.GetFromApi(url, cacheExpiration); return response.data; } public Task> GetAlbumsAsync(string storefrontId, IEnumerable albumIds) { return this.GetAlbumsAsync(storefrontId, albumIds, NoCacheTime); } public async Task> GetAlbumsAsync(string storefrontId, IEnumerable albumIds, TimeSpan cacheExpiration) { var url = $"vendor-api/apple/v1/catalog/{storefrontId}/albums?ids={String.Join(",", albumIds)}"; var response = await this.GetFromApi(url, cacheExpiration); return response.data; } public Task GetAllStorefrontsAsync() { return this.GetAllStorefrontsAsync(NoCacheTime); } public async Task GetAllStorefrontsAsync(TimeSpan cacheExpiration) { var url = $"vendor-api/apple/v1/storefronts"; var response = await this.GetFromApi(url, cacheExpiration); return response; } public Task GetPlaylistAsync(string storeFront, string playlistId, string jobName = null) { return this.GetPlaylistAsync(storeFront, playlistId, NoCacheTime, jobName); } public async Task GetPlaylistAsync(string storeFront, string playlistId, TimeSpan cacheExpiration, string jobName = null) { var url = $"vendor-api/apple/v1/catalog/{storeFront}/playlists/{playlistId}"; var response = await this.GetFromApi(url, cacheExpiration); return response?.data?.FirstOrDefault(); } public Task GetCuratorsAsync(string storeFrontId, IEnumerable curatorIds) { return this.GetCuratorsAsync(storeFrontId, curatorIds, NoCacheTime); } public async Task GetCuratorsAsync(string storeFrontId, IEnumerable curatorIds, TimeSpan cacheExpiration) { string url = $"vendor-api/apple/v1/catalog/{storeFrontId}/curators?ids={string.Join(",", curatorIds)}"; var response = await this.GetFromApi(url, cacheExpiration); return response?.data?.ToArray(); } public async Task GetCuratorPlaylistIdsAsync(string storeFrontId, long curatorId, TimeSpan cacheExpiration) { AppleCuratorResponseData curatorResponse = await GetCuratorsAsync(storeFrontId, new string[] { curatorId.ToString() }, cacheExpiration) .ContinueWith(t => t.Result?.First()); if (curatorResponse == null) { return Array.Empty(); } List playlists = new List(); playlists.AddRange(curatorResponse.relationships.playlists.data.Select(p => p.id)); string next = curatorResponse.relationships.playlists.next; while (!String.IsNullOrWhiteSpace(next)) { var data = await this.GetFromApi($"vendor-api/apple/{next}", cacheExpiration); playlists.AddRange(data.data.Select(p => p.id)); next = data.next; } return playlists.ToArray(); } public Task GetCuratorPlaylistIdsAsync(string storeFrontId, long curatorId) { return this.GetCuratorPlaylistIdsAsync(storeFrontId, curatorId, NoCacheTime); } protected override string GetLoggerName() { return "ApolleAppleWebApi"; } } }