public async Task <List <T> > PerformIncrementalPostAsync <T>(string url, int itemsToGet = 0)
        {
            HttpClient client = GetHttpClient();

            List <T> results       = new List <T>();
            int      totalTracks   = 0;
            string   nextPageToken = null;
            string   prevPageToken = null;

            do
            {
                JObject requestData = new JObject()
                {
                    { "max-results", "20000" },
                    { "start-token", nextPageToken }
                };

                IncrementalResponse <T> response = await PerformPostAsync <IncrementalResponse <T> >(url, requestData);

                // sometimes the previous request will include a non null next page token, so we fetch and get no data
                // which results in null data and null nextpagetoken. In this case, we've fetched all we can, so return
                // what we've fetched.
                if (response.Data == null)
                {
                    if (response.NextPageToken == null)
                    {
                        return(results);
                    }
                    else
                    {
                        // TODO: error?
                        return(results);
                    }
                }

                results.AddRange(response.Data.Items);
                totalTracks += response.Data.Items.Count;

                if (itemsToGet != 0)
                {
                    if (totalTracks < itemsToGet)
                    {
                        prevPageToken = nextPageToken;
                        nextPageToken = response.NextPageToken;
                    }
                    else
                    {
                        return(results.Take(itemsToGet).ToList());
                    }
                }
                else
                {
                    prevPageToken = nextPageToken;
                    nextPageToken = response.NextPageToken;
                }
            }while (nextPageToken != null);

            return(results);
        }
Esempio n. 2
0
        private async void incrementalTracksGetButton_Click(object sender, EventArgs e)
        {
            try
            {
                IncrementalResponse <Track> resp = await gpmClient.GetLibraryIncrementalAsync(10, null);

                Debug.WriteLine(resp.NextPageToken);
                Debug.WriteLine(resp.Data.Items[0].ToString());

                resp = await gpmClient.GetLibraryIncrementalAsync(10, resp.NextPageToken);

                Debug.WriteLine(resp.NextPageToken);
                Debug.WriteLine(resp.Data.Items[0].ToString());
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
        /// <summary>
        /// Get a list of devices associated with the account
        /// </summary>
        /// <returns></returns>
        public async Task <List <Device> > GetDevicesAsync()
        {
            IncrementalResponse <Device> response = await GetAsync <IncrementalResponse <Device> >(SJ_URL_DEVICE_MANAGEMENT);

            return(response.Data.Items);
        }