Ejemplo n.º 1
0
        public async Task <Item> GetProductAsync(string sku)
        {
            var webRequest = ( WebRequest )WebRequest.Create()
                             .Method(MagentoWebRequestMethod.Get)
                             .Path(MagentoServicePath.CreateProductsServicePath().AddCatalog(Uri.EscapeDataString(sku)))
                             .AuthToken(this.Token)
                             .Url(this.Url);

            return(await ActionPolicies.RepeatOnChannelProblemAsync.Get(async() =>
            {
                try
                {
                    using (var v = await webRequest.RunAsync(Mark.CreateNew()).ConfigureAwait(false))
                    {
                        return JsonConvert.DeserializeObject <Item>(new StreamReader(v, Encoding.UTF8).ReadToEnd());
                    }
                }
                catch (MagentoException exception)
                {
                    var webException = (exception as MagentoWebException)?.InnerException as WebException;
                    var response = webException?.Response as HttpWebResponse;
                    if (webException?.Status == WebExceptionStatus.ProtocolError && response?.StatusCode == HttpStatusCode.NotFound)
                    {
                        return null;
                    }

                    throw;
                }
            }));
        }
Ejemplo n.º 2
0
        public async Task <Item> GetProductAsync(string sku)
        {
            var webRequest = ( WebRequest )WebRequest.Create()
                             .Method(MagentoWebRequestMethod.Get)
                             .Path(MagentoServicePath.CreateProductsServicePath().AddCatalog(Uri.EscapeDataString(sku)))
                             .AuthToken(this.Token)
                             .Url(this.Url);

            return(await ActionPolicies.RepeatOnChannelProblemAsync.Get(async() =>
            {
                try
                {
                    using (var v = await webRequest.RunAsync(Mark.CreateNew()).ConfigureAwait(false))
                    {
                        return JsonConvert.DeserializeObject <Item>(new StreamReader(v, Encoding.UTF8).ReadToEnd());
                    }
                }
                catch (MagentoWebException exception)
                {
                    if (exception.IsNotFoundException())
                    {
                        return null;
                    }
                    throw;
                }
            }));
        }
        public async Task <bool> PutStockItemAsync(string productSku, string itemId, RootObject stockItem, Mark mark = null)
        {
            var webRequest = ( WebRequest )WebRequest.Create()
                             .Method(MagentoWebRequestMethod.Put)
                             .Path(MagentoServicePath.Create(
                                       MagentoServicePath.ProductsPath +
                                       $"/{Uri.EscapeDataString( productSku )}/" +
                                       MagentoServicePath.StockItemsPath +
                                       $"/{itemId}"))
                             .Body(JsonConvert.SerializeObject(stockItem, new JsonSerializerSettings()
            {
                NullValueHandling = NullValueHandling.Ignore
            }))
                             .AuthToken(this.Token)
                             .Url(this.Url);

            return(await ActionPolicies.RepeatOnChannelProblemAsync.Get(async() =>
            {
                try
                {
                    using (var v = await webRequest.RunAsync(mark).ConfigureAwait(false))
                    {
                        return JsonConvert.DeserializeObject <int>(new StreamReader(v, Encoding.UTF8).ReadToEnd()) == 1;
                    }
                }
                catch (MagentoWebException exception)
                {
                    if (exception.IsNotFoundException())
                    {
                        return false;
                    }
                    throw;
                }
            }));
        }
Ejemplo n.º 4
0
        public async Task <RootObject> GetProductsAsync(DateTime updatedAt, string type, bool excludeType, PagingModel page, Mark mark = null)
        {
            var filterGroups = new List <FilterGroup>()
            {
            };

            if (!string.IsNullOrWhiteSpace(type))
            {
                filterGroups.Add(new FilterGroup()
                {
                    filters = new List <Filter> {
                        new Filter(@"type_id", type, excludeType ? Filter.ConditionType.NotEqual : Filter.ConditionType.Equals)
                    }
                });
            }
            if (updatedAt > DateTime.MinValue)
            {
                filterGroups.Add(new FilterGroup()
                {
                    filters = new List <Filter> {
                        new Filter(@"updated_at", updatedAt.ToRestParameterString(), Filter.ConditionType.GreaterThan)
                    }
                });
            }
            if (!filterGroups.Any())
            {
                filterGroups.Add(new FilterGroup()
                {
                    filters = new List <Filter>()
                });
            }

            var parameters = new SearchCriteria()
            {
                filter_groups = filterGroups,
                current_page  = page.CurrentPage,
                page_size     = page.ItemsPerPage
            };
            var webRequest = ( WebRequest )WebRequest.Create()
                             .Method(MagentoWebRequestMethod.Get)
                             .Path(MagentoServicePath.CreateProductsServicePath())
                             .Parameters(parameters)
                             .AuthToken(this.Token)
                             .Url(this.Url);

            return(await ActionPolicies.RepeatOnChannelProblemAsync.Get(async() =>
            {
                using (var v = await webRequest.RunAsync(mark).ConfigureAwait(false))
                {
                    var response = JsonConvert.DeserializeObject <RootObject>(new StreamReader(v, Encoding.UTF8).ReadToEnd());

                    if (response.items != null)
                    {
                        response.items = response.items.Where(i => !string.IsNullOrWhiteSpace(i.sku)).ToList();
                    }

                    return response;
                }
            }).ConfigureAwait(false));
        }
        public void GetOrders_StoreContainsOrders_ReceiveOrders2()
        {
            //------------ Arrange

            //------------ Act
            var sc = new SearchCriteria()
            {
                filter_groups = new List <FilterGroup>()
                {
                    new FilterGroup()
                    {
                        filters = new List <Filter>
                        {
                            new Filter(@"updated_at", @"2016-07-01 00:00:00", Filter.ConditionType.GreaterThan),
                        }
                    }
                },
                page_size = 100, current_page = 1
            };

            //------------ Assert
            var qwe = ( WebRequest )WebRequest.Create()
                      .Method(MagentoWebRequestMethod.Get)
                      .Path(MagentoServicePath.CreateProductsServicePath())
                      .Parameters(sc)
                      .Url(MagentoUrl.Create("http://xxx"))
            ;
            var res = qwe.RunAsync();

            res.Wait();
        }
Ejemplo n.º 6
0
        public async Task <CategoryNode> GetCategoriesTreeAsync()
        {
            var webRequest = ( WebRequest )WebRequest.Create()
                             .Method(MagentoWebRequestMethod.Get)
                             .Path(MagentoServicePath.CreateCategoriesPath())
                             .AuthToken(this.Token)
                             .Url(this.Url);

            return(await ActionPolicies.RepeatOnChannelProblemAsync.Get(async() =>
            {
                using (var v = await webRequest.RunAsync(Mark.CreateNew()).ConfigureAwait(false))
                {
                    return JsonConvert.DeserializeObject <CategoryNode>(new StreamReader(v, Encoding.UTF8).ReadToEnd());
                }
            }));
        }
Ejemplo n.º 7
0
        public async Task <StockItem> GetStockItemAsync(string productSku, Mark mark = null)
        {
            var webRequest = ( WebRequest )WebRequest.Create()
                             .Method(MagentoWebRequestMethod.Get)
                             .Path(MagentoServicePath.Create(
                                       MagentoServicePath.StockItemsPath +
                                       $"/{productSku}"))
                             .AuthToken(this.Token)
                             .Url(this.Url);

            return(await ActionPolicies.RepeatOnChannelProblemAsync.Get(async() =>
            {
                using (var v = await webRequest.RunAsync(mark).ConfigureAwait(false))
                {
                    return JsonConvert.DeserializeObject <StockItem>(new StreamReader(v, Encoding.UTF8).ReadToEnd());
                }
            }));
        }
 public async Task <AuthorizationToken> GetTokenAsync(MagentoLogin token, MagentoPass url)
 {
     return(await ActionPolicies.RepeatOnChannelProblemAsync.Get(async() =>
     {
         using (var v = await(( WebRequest )
                              WebRequest.Create()
                              .Method(MagentoWebRequestMethod.Post)
                              .Url(this.Url)
                              .Path(MagentoServicePath.CreateIntegrationServicePath())
                              .Body(JsonConvert.SerializeObject(new CredentialsModel()
         {
             username = token.Login, password = url.Password
         })))
                        .RunAsync(Mark.CreateNew()).ConfigureAwait(false))
         {
             return AuthorizationToken.Create(new StreamReader(v, Encoding.UTF8).ReadToEnd());
         }
     }).ConfigureAwait(false));
 }
Ejemplo n.º 9
0
        public async Task <RootObject> GetOrdersAsync(DateTime updatedFrom, DateTime updatedTo, PagingModel page, Mark mark = null)
        {
            var parameters = new SearchCriteria()
            {
                filter_groups = new List <FilterGroup>()
                {
                    new FilterGroup()
                    {
                        filters = new List <Filter>()
                        {
                            new Filter("updated_at", updatedFrom.ToRestParameterString(), Filter.ConditionType.From)
                        }
                    },
                    new FilterGroup()
                    {
                        filters = new List <Filter>()
                        {
                            new Filter("updated_at", updatedTo.ToRestParameterString(), Filter.ConditionType.To)
                        }
                    }
                },
                current_page = page.CurrentPage,
                page_size    = page.ItemsPerPage,
            };

            var webRequest = ( WebRequest )WebRequest.Create()
                             .Method(MagentoWebRequestMethod.Get)
                             .Path(MagentoServicePath.CreateOrdersServicePath())
                             .Parameters(parameters)
                             .AuthToken(this.Token)
                             .Url(this.Url);

            return(await ActionPolicies.RepeatOnChannelProblemAsync.Get(async() =>
            {
                using (var v = await webRequest.RunAsync(mark).ConfigureAwait(false))
                {
                    return JsonConvert.DeserializeObject <RootObject>(new StreamReader(v, Encoding.UTF8).ReadToEnd());
                }
            }).ConfigureAwait(false));
        }
Ejemplo n.º 10
0
        public async Task <RootObject> GetOrdersAsync(IEnumerable <string> ids, PagingModel page)
        {
            var idsList = ids as IList <string> ?? ids.ToList();

            if (ids == null || !idsList.Any())
            {
                return(default(RootObject));
            }

            var parameters = new SearchCriteria()
            {
                filter_groups = new List <FilterGroup>()
                {
                    new FilterGroup()
                    {
                        filters = new List <Filter>()
                        {
                            new Filter("increment_id", string.Join(",", idsList), Filter.ConditionType.In)
                        }
                    }
                },
                current_page = page.CurrentPage,
                page_size    = page.ItemsPerPage,
            };

            var webRequest = ( WebRequest )WebRequest.Create()
                             .Method(MagentoWebRequestMethod.Get)
                             .Path(MagentoServicePath.CreateOrdersServicePath())
                             .Parameters(parameters)
                             .AuthToken(this.Token)
                             .Url(this.Url);

            return(await ActionPolicies.RepeatOnChannelProblemAsync.Get(async() =>
            {
                using (var v = await webRequest.RunAsync(Mark.CreateNew()).ConfigureAwait(false))
                {
                    return JsonConvert.DeserializeObject <RootObject>(new StreamReader(v, Encoding.UTF8).ReadToEnd());
                }
            }));
        }