コード例 #1
0
        /// <summary>
        ///	Returns Square item variation with specified sku
        /// </summary>
        /// <param name="sku">Sku</param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task <SquareItem> GetItemBySkuAsync(string sku, CancellationToken cancellationToken)
        {
            Condition.Requires(sku, "sku").IsNotNullOrWhiteSpace();

            var mark = Mark.CreateNew();

            if (cancellationToken.IsCancellationRequested)
            {
                var exceptionDetails = CreateMethodCallInfo(url: SquareEndPoint.SearchCatalogUrl, mark: mark, additionalInfo: this.AdditionalLogInfo());
                var squareException  = new SquareException(string.Format("{0}. Search item by sku request was cancelled", exceptionDetails));
                SquareLogger.LogTraceException(squareException);
                throw squareException;
            }

            var request = new SearchCatalogObjectsRequest
            {
                IncludeDeletedObjects = false,
                IncludeRelatedObjects = true,
                ObjectTypes           = new string[] { ItemsExtensions.ItemVariationCatalogObjectType }.ToList(),
                Query = new CatalogQuery()
                {
                    ExactQuery = new CatalogQueryExact("sku", sku)
                }
            };

            var response = await base.ThrottleRequest(SquareEndPoint.SearchCatalogUrl, request.ToJson(), mark, (token) =>
            {
                return(this._catalogApi.SearchCatalogObjectsAsync(request));
            }, cancellationToken).ConfigureAwait(false);

            var errors = response.Errors;

            if (errors != null && errors.Any())
            {
                var methodCallInfo  = CreateMethodCallInfo(SquareEndPoint.SearchCatalogUrl, mark, additionalInfo: this.AdditionalLogInfo(), errors: errors.ToJson());
                var squareException = new SquareException(string.Format("{0}. Search items catalog returned errors", methodCallInfo));
                SquareLogger.LogTraceException(squareException);
                throw squareException;
            }

            var catalogItem = response.Objects?.FirstOrDefault(o => o.ItemVariationData != null);

            if (catalogItem != null)
            {
                var item = catalogItem.ToSvItems().First();
                var itemsWithQuantity = await this.FillItemsQuantities(new SquareItem[] { item }.ToList(), cancellationToken).ConfigureAwait(false);

                return(itemsWithQuantity.FirstOrDefault());
            }

            return(null);
        }
コード例 #2
0
        /// <summary>
        ///	Returns Square items which were updated or created after specified date
        /// </summary>
        /// <param name="date"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task <IEnumerable <SquareItem> > GetChangedItemsAfterAsync(DateTime date, CancellationToken cancellationToken)
        {
            var items = new List <SquareItem>();
            var mark  = Mark.CreateNew();

            if (cancellationToken.IsCancellationRequested)
            {
                var exceptionDetails = CreateMethodCallInfo(url: SquareEndPoint.SearchCatalogUrl, mark: mark, additionalInfo: this.AdditionalLogInfo());
                var squareException  = new SquareException(string.Format("{0}. Search changed items request was cancelled", exceptionDetails));
                SquareLogger.LogTraceException(squareException);
                throw squareException;
            }

            string paginationCursor = null;

            do
            {
                var request = new SearchCatalogObjectsRequest
                {
                    IncludeDeletedObjects = false,
                    IncludeRelatedObjects = true,
                    Cursor      = paginationCursor,
                    ObjectTypes = new string[] { ItemsExtensions.ItemCatalogObjectType }.ToList(),
                    BeginTime = date.ToUniversalTime().FromUtcToRFC3339()
                };

                var response = await base.ThrottleRequest(SquareEndPoint.SearchCatalogUrl, request.ToJson(), mark, (token) =>
                {
                    return(this._catalogApi.SearchCatalogObjectsAsync(request));
                }, cancellationToken).ConfigureAwait(false);

                var errors = response.Errors;
                if (errors != null && errors.Any())
                {
                    var methodCallInfo  = CreateMethodCallInfo(SquareEndPoint.SearchCatalogUrl, mark, additionalInfo: this.AdditionalLogInfo(), errors: errors.ToJson());
                    var squareException = new SquareException(string.Format("{0}. Search items catalog returned errors", methodCallInfo));
                    SquareLogger.LogTraceException(squareException);
                    throw squareException;
                }

                if (response.Objects != null)
                {
                    foreach (var obj in response.Objects)
                    {
                        items.AddRange(obj.ToSvItems());
                    }
                }

                paginationCursor = response.Cursor;
            }while(!string.IsNullOrWhiteSpace(paginationCursor));

            return(items);
        }