Пример #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;
                }
            }));
        }
Пример #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;
                }
            }));
        }
Пример #3
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));
        }
Пример #4
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());
                }
            }));
        }