示例#1
0
        public IEnumerable <GetItemResponseItem> GetNetoProductBySKUs(string[] skus, bool isWithDesc = false)
        {
            try
            {
                var getItemObj = new GetItem();
                getItemObj.Filter                         = new GetItemFilter();
                getItemObj.Filter.SKU                     = skus;
                getItemObj.Filter.OrderBy                 = GetItemFilterOrderBy.ID;
                getItemObj.Filter.OrderBySpecified        = true;
                getItemObj.Filter.OrderDirection          = GetItemFilterOrderDirection.ASC;
                getItemObj.Filter.OrderDirectionSpecified = true;
                var outputSelector = GetOutputSelector(isWithDesc);
                getItemObj.Filter.OutputSelector = outputSelector;
                getItemObj.Filter.Page           = 0.ToString();
                getItemObj.Filter.Limit          = skus.Count().ToString();

                var rawXML         = CommonFunc.ConvertObjectToXMLString(getItemObj, Encoding.UTF8);
                var getItemRequest = new RestRequest("do/WS/NetoAPI");
                getItemRequest.Method = Method.POST;
                getItemRequest.AddHeader("NETOAPI_ACTION", "GetItem");
                getItemRequest.AddHeader("NETOAPI_USERNAME", _netoAPICredential.UserName);
                getItemRequest.AddHeader("NETOAPI_KEY", _netoAPICredential.APIKey);
                getItemRequest.AddParameter("application/xml", rawXML, ParameterType.RequestBody);

                IRestResponse response = _restClient.Execute(getItemRequest);
                if (response.IsSuccessful)
                {
                    var getItemResponse = JsonConvert.DeserializeObject <GetItemResponse>(response.Content);
                    if (getItemResponse.Ack != GetItemResponseAck.Error)
                    {
                        return(getItemResponse.Item);
                    }
                    else
                    {
                        throw new Exception(getItemResponse.Messages.Error.Select(err => err.Message).Aggregate((current, next) => (current + ";" + next)));
                    }
                }
                else
                {
                    throw new Exception(response.ErrorMessage);
                }
            }
            catch (Exception ex)
            {
                LogManager.Instance.Error(ex.Message);
                throw ex;
            }
        }
示例#2
0
        public IEnumerable <GetItemResponseItem> GetNetoProducts(bool isWithDesc = false)
        {
            try
            {
                var retProducts = new List <GetItemResponseItem>();

                int pageNumber     = 0;
                int recordsPerPage = 1000;
                var getItemObj     = new GetItem();
                getItemObj.Filter = new GetItemFilter();
                //getItemObj.Filter.DateAddedFrom = DateTime.Now;
                //getItemObj.Filter.DateAddedFromSpecified = true;
                getItemObj.Filter.IsActive                = new string[] { "True" };
                getItemObj.Filter.OrderBy                 = GetItemFilterOrderBy.ID;
                getItemObj.Filter.OrderBySpecified        = true;
                getItemObj.Filter.OrderDirection          = GetItemFilterOrderDirection.ASC;
                getItemObj.Filter.OrderDirectionSpecified = true;
                //var outputSelector = CommonFunc.GetEnumList<GetItemFilterOutputSelector>();


                var outputSelector = GetOutputSelector(isWithDesc);

                getItemObj.Filter.OutputSelector = outputSelector;
                var gotLastPage = false;
                while (!gotLastPage)
                {
                    getItemObj.Filter.Page  = pageNumber.ToString();
                    getItemObj.Filter.Limit = recordsPerPage.ToString();
                    var rawXML         = CommonFunc.ConvertObjectToXMLString(getItemObj, Encoding.UTF8);
                    var getItemRequest = new RestRequest("do/WS/NetoAPI");
                    getItemRequest.Method = Method.POST;
                    getItemRequest.AddHeader("NETOAPI_ACTION", "GetItem");
                    getItemRequest.AddHeader("NETOAPI_USERNAME", _netoAPICredential.UserName);
                    getItemRequest.AddHeader("NETOAPI_KEY", _netoAPICredential.APIKey);
                    getItemRequest.AddParameter("application/xml", rawXML, ParameterType.RequestBody);

                    IRestResponse response = _restClient.Execute(getItemRequest);
                    if (response.IsSuccessful)
                    {
                        var getItemResponse = JsonConvert.DeserializeObject <GetItemResponse>(response.Content);
                        if (getItemResponse.Ack != GetItemResponseAck.Error)
                        {
                            retProducts.AddRange(getItemResponse.Item);
                            if (getItemResponse.Item.Count() < recordsPerPage)
                            {
                                gotLastPage = true;
                            }
                        }
                        else
                        {
                            throw new Exception(getItemResponse.Messages.Error.Select(err => err.Message).Aggregate((current, next) => (current + ";" + next)));
                        }
                    }
                    else
                    {
                        throw new Exception(response.ErrorMessage);
                    }
                    pageNumber++;
                }
                return(retProducts);
            }
            catch (Exception ex)
            {
                LogManager.Instance.Error(ex.Message);
                throw ex;
            }
        }