Example #1
0
        private async Task <PlaylistSummaryViewModel> GetPlaylistDetailsViewModel(string id, bool doAsync = true)
        {
            ServicePointManager.DefaultConnectionLimit = 5;
            var access_token = Session["AccessToken"].ToString();

            var    url = String.Format("https://api.spotify.com/v1/users/{0}/playlists?limit=50", id);
            string json;

            using (mp.Step("Get Users Playlists first 50")) {
                json = sh.CallSpotifyAPIPassingToken(access_token, url);
            }
            var vm = JsonConvert.DeserializeObject <PlaylistSummaryViewModel>(json);

            var recordsPerPage = 50;

            if (vm.total > recordsPerPage)
            {
                var totalRecords        = vm.total;
                int numberOfTimesToLoop = (totalRecords + recordsPerPage - 1) / recordsPerPage;

                // we've already done the first query to get the total, do skip that
                numberOfTimesToLoop -= 1;
                if (doAsync)
                {
                    using (mp.Step("Async " + numberOfTimesToLoop + " queries"))
                        using (mp.CustomTiming("http", "overall")) {
                            var tasks  = new Task <string> [numberOfTimesToLoop];
                            int offset = 0;
                            for (int i = 0; i < numberOfTimesToLoop; i++)
                            {
                                // start offset at 50
                                offset += recordsPerPage;
                                url     = String.Format("https://api.spotify.com/v1/users/{0}/playlists?limit={2}&offset={1}", id,
                                                        offset, recordsPerPage);
                                tasks[i] = sh.CallSpotifyAPIPassingTokenAsync(access_token, url);
                            }
                            // at this point all tasks will be running at the same time
                            await Task.WhenAll(tasks);
                        }

                    for (int i = 0; i < numberOfTimesToLoop - 1; i++)
                    {
                        var vm2 = JsonConvert.DeserializeObject <PlaylistSummaryViewModel>(json);
                        // merge with vm
                        vm.items = vm.items.Union(vm2.items).ToList();
                    }
                }
                else
                {
                    for (int i = 1; i < numberOfTimesToLoop; i++)
                    {
                        int offset = i * recordsPerPage;
                        url = String.Format("https://api.spotify.com/v1/users/{0}/playlists?limit={2}&offset={1}", id, offset, recordsPerPage);
                        using (mp.Step("Get Users Playlists - " + i)) {
                            json = sh.CallSpotifyAPIPassingToken(access_token, url);
                        }

                        var vm2 = JsonConvert.DeserializeObject <PlaylistSummaryViewModel>(json);
                        // merge with vm
                        vm.items = vm.items.Union(vm2.items).ToList();
                    }
                }
            }
            vm.access_token = access_token;
            return(vm);
        }