Exemplo n.º 1
0
        public virtual async Task <IEnumerable <RpcInvoker.RpcRequestResponse <PutStockItem, object> > > PutStockItemsAsync(List <PutStockItem> stockItems, Mark mark = null)
        {
            var methodParameters = stockItems.ToJson();

            try
            {
                var stockItemsProcessed = stockItems.Select(x =>
                {
                    var catalogInventoryStockItemUpdateEntity = (x.Qty > 0) ?
                                                                new catalogInventoryStockItemUpdateEntity()
                    {
                        is_in_stock = 1, is_in_stockSpecified = true, qty = x.Qty.ToString()
                    } :
                    new catalogInventoryStockItemUpdateEntity()
                    {
                        is_in_stock = 0, is_in_stockSpecified = false, qty = x.Qty.ToString()
                    };
                    return(Tuple.Create(x, catalogInventoryStockItemUpdateEntity));
                });

                const int maxCheckCount    = 2;
                const int delayBeforeCheck = 1800000;

                var privateClient = this._clientFactory.GetClient();

                RpcInvoker.RpcResponse <catalogInventoryStockItemMultiUpdateResponse> serverResponse = null;
                await ActionPolicies.GetAsync.Do(async() =>
                {
                    var statusChecker = new StatusChecker(maxCheckCount);
                    TimerCallback tcb = statusChecker.CheckStatus;

                    privateClient = this._clientFactory.RefreshClient(privateClient);
                    var sessionId = await this.GetSessionId().ConfigureAwait(false);

                    using (var stateTimer = new Timer(tcb, privateClient, 1000, delayBeforeCheck))
                    {
                        MagentoLogger.LogTraceStarted(this.CreateMethodCallInfo(methodParameters, mark));

                        var catalogInventoryStockItemUpdateEntities = stockItemsProcessed.Select(x => x.Item2).ToArray();

                        serverResponse = await RpcInvoker.SuppressExceptions(async() => await privateClient.catalogInventoryStockItemMultiUpdateAsync(sessionId.SessionId, stockItemsProcessed.Select(x => x.Item1.ItemId).ToArray(), catalogInventoryStockItemUpdateEntities).ConfigureAwait(false)).ConfigureAwait(false);

                        var updateBriefInfo = string.Format("{{Success:{0}}}", serverResponse.Result.result);
                        MagentoLogger.LogTraceEnded(CreateMethodCallInfo(methodParameters, mark: mark, methodResult: updateBriefInfo));
                    }
                }).ConfigureAwait(false);

                var result = stockItems.Select(y => new RpcInvoker.RpcRequestResponse <PutStockItem, object>(y, new RpcInvoker.RpcResponse <object>(serverResponse?.ErrorCode ?? RpcInvoker.SoapErrorCode.Unknown, serverResponse?.Result, serverResponse?.Exception)));
                return(result);
            }
            catch (Exception exc)
            {
                throw new MagentoSoapException($"An error occured during PutStockItemsAsync({methodParameters})", exc);
            }
        }
Exemplo n.º 2
0
        public async Task <int> CreateProduct(string storeId, string name, string sku, int isInStock, string productType, Mark markForLog)
        {
            var stockItem        = new CreatteProductModel(name, sku, isInStock, productType);
            var methodParameters = stockItem.ToJson();

            try
            {
                const int maxCheckCount    = 2;
                const int delayBeforeCheck = 1800000;

                var privateClient = this._clientFactory.CreateMagentoCatalogProductRepositoryServiceClient();

                var res        = new List <RpcInvoker.RpcResponse <catalogProductRepositoryV1SaveResponse1> >();
                var stockItems = new List <CreatteProductModel> {
                    stockItem
                };

                await stockItems.DoInBatchAsync(10, async x =>
                {
                    await ActionPolicies.GetAsync.Do(async() =>
                    {
                        var statusChecker = new StatusChecker(maxCheckCount);
                        TimerCallback tcb = statusChecker.CheckStatus;

                        privateClient = this._clientFactory.RefreshMagentoCatalogProductRepositoryServiceClient(privateClient);

                        using (var stateTimer = new Timer(tcb, privateClient, 1000, delayBeforeCheck))
                        {
                            MagentoLogger.LogTraceStarted(this.CreateMethodCallInfo(methodParameters, mark: markForLog));

                            var catalogInventoryDataStockItemInterface = new CatalogDataProductInterface()
                            {
                                sku                     = x.Sku,
                                name                    = x.Name,
                                price                   = "1",
                                priceSpecified          = true,
                                status                  = 1,
                                statusSpecified         = true,
                                typeId                  = productType,
                                attributeSetId          = 4,
                                attributeSetIdSpecified = true,
                                weight                  = "1",
                                weightSpecified         = true,
                            };
                            if (productType == "bundle")
                            {
                                catalogInventoryDataStockItemInterface.customAttributes = new[]
                                {
                                    new FrameworkAttributeInterface {
                                        value = "1", attributeCode = "price_view"
                                    },
                                    new FrameworkAttributeInterface {
                                        value = "1", attributeCode = "price_type"
                                    }
                                };
                            }
                            var catalogInventoryStockRegistryV1UpdateStockItemBySkuRequest = new CatalogProductRepositoryV1SaveRequest()
                            {
                                product = catalogInventoryDataStockItemInterface
                            };

                            var temp = await privateClient.catalogProductRepositoryV1SaveAsync(catalogInventoryStockRegistryV1UpdateStockItemBySkuRequest).ConfigureAwait(false);

                            var updateResult = new RpcInvoker.RpcResponse <catalogProductRepositoryV1SaveResponse1>(RpcInvoker.SoapErrorCode.Success, temp, null);
                            res.Add(updateResult);
                        }
                    }).ConfigureAwait(false);
                }).ConfigureAwait(false);

                MagentoLogger.LogTraceEnded(this.CreateMethodCallInfo(methodParameters, mark: markForLog, methodResult: res.ToJson()));

                return(res.First().Result.catalogProductRepositoryV1SaveResponse.result.id);
            }
            catch (Exception exc)
            {
                throw new MagentoSoapException($"An error occured during PutStockItemsAsync({methodParameters})", exc);
            }
        }