Пример #1
0
 /// <summary>
 /// Constructor for a new page to an existing paged query.
 /// </summary>
 /// <param name="newItems"></param>
 public PCOApiQueryResult(PCOApiQueryResult existingResults, List <DataItem> includedItems)
 {
     Items         = existingResults.Items;
     IncludedItems = existingResults.IncludedItems;
     MergeIncludedItems(includedItems);
 }
Пример #2
0
        /// <summary>
        /// Gets the results of an API query for the specified API end point.
        /// </summary>
        /// <param name="apiEndPoint">The API end point.</param>
        /// <param name="apiRequestOptions">An optional collection of request options.</param>
        /// <param name="modifiedSince">The modified since.</param>
        /// <returns></returns>
        private static PCOApiQueryResult GetAPIQuery(string apiEndPoint, Dictionary <string, string> apiRequestOptions = null, DateTime?modifiedSince = null, PCOApiQueryResult existingResults = null)
        {
            if (modifiedSince.HasValue && apiRequestOptions != null)
            {
                // Add a parameter to sort records by last update, descending.
                apiRequestOptions.Add("order", "-updated_at");
            }

            string result = ApiGet(apiEndPoint, apiRequestOptions);

            if (result.IsNullOrWhiteSpace())
            {
                return(null);
            }

            var itemsResult = JsonConvert.DeserializeObject <QueryItems>(result);

            if (itemsResult == null)
            {
                PCOApi.ErrorMessage = $"Error:  Unable to deserialize result retrieved from { apiEndPoint }.";
                throw new Exception(PCOApi.ErrorMessage);
            }


            PCOApiQueryResult queryResult;

            if (existingResults != null)
            {
                queryResult = new PCOApiQueryResult(existingResults, itemsResult.IncludedItems);
            }
            else
            {
                queryResult = new PCOApiQueryResult(itemsResult.IncludedItems);
            }

            // Loop through each item in the results
            var continuePaging = true;

            foreach (var itemResult in itemsResult.Data)
            {
                // If we're only looking for records updated after last update, and this record is older, stop processing
                DateTime?recordUpdatedAt = (itemResult.Item.updated_at ?? itemResult.Item.created_at);
                if (modifiedSince.HasValue && recordUpdatedAt.HasValue && recordUpdatedAt.Value <= modifiedSince.Value)
                {
                    continuePaging = false;
                    break;
                }

                queryResult.Items.Add(itemResult);
            }

            // If there are more page, and we should be paging
            string nextEndPoint = itemsResult.Links != null && itemsResult.Links.Next != null ? itemsResult.Links.Next : string.Empty;

            if (nextEndPoint.IsNotNullOrWhitespace() && continuePaging)
            {
                nextEndPoint = nextEndPoint.Substring(ApiBaseUrl.Length);
                // Get the next page of results by doing a recursive call to this same method.
                // Note that nextEndPoint is supplied without the options dictionary, as those should already be specified in the result from PCO.
                return(GetAPIQuery(nextEndPoint, null, modifiedSince, queryResult));
            }

            return(queryResult);
        }