示例#1
0
 public AkeneoCategory CreateNewCategory(AkeneoCategory category, AkeneoProduct product)
 {
     try
     {
         _logger.info($"Creating category {category.CategoryName} : {product.productName} -> start");
         IBaseRequestHandler <NameValueCollection, WebClientHeader> httpManager = new BaseWebClientWriter();
         var _c = new AkeneoBaseCategoriesIndexerConverter();
         category.CategoryId = category.CategoryName.Trim().ToLower().Replace(" ", "_").Replace("-", "_").Replace("/", "_").Replace("?", "_").Replace(".", "_").Replace(",", "_").Replace("&", "_");
         AkeneoIndexedCategoriesDtoEmbedCollectionItem dtoCat = _c.ConvertToDtoEntity(category);
         if (!ReferenceEquals(dtoCat, null) &&
             !ReferenceEquals(dtoCat.CategoryCode, null) &&
             !ReferenceEquals(dtoCat.CategoriesLocales, null) &&
             !ReferenceEquals(dtoCat.CategoriesLocales.Count, 0))
         {
             var EncodedContent = JsonConvert.SerializeObject(dtoCat);
             httpManager.AddHeader(new WebClientHeader("Authorization", $"Bearer {akeneoAuthInfo.access_token}"));
             httpManager.AddBodyParameter(EncodedContent);
             httpManager.PostDataAsStringContext($"{Akeneo.BaseAkeneoUrl}{Akeneo.AkeneoCategoryListUrl}");
             return(category);
         }
         return(null);
         //AkeneoIndexedCategoriesDtoEmbedCollectionItem
     }
     catch (Exception e)
     {
         _logger.error($"{category.CategoryName} -> {product.productName} Error in creating akeneo category: {e.Message} -> {e.StackTrace}");
         return(null);
     }
 }
示例#2
0
        private void Connect(object obj)
        {
            _logger.info("Akeneo -> connecting");
            IBaseRequestHandler <NameValueCollection, WebClientHeader> httpManager = new BaseWebClientWriter();

            akeneoStatus = BaseServicesStatuses.ServiceLaunching;
            var bytes = System.Text.Encoding.UTF8.GetBytes($"{Akeneo.BaseAkeneoClientId}:{Akeneo.BaseAkeneoSecretKey}");
            var auth  = System.Convert.ToBase64String(bytes).ToString();

            var auth_data = new NameValueCollection();

            auth_data.Add("grant_type", Akeneo.AkeneoPasswordGrantType);
            auth_data.Add("username", Akeneo.BaseAkeneoUserName);
            auth_data.Add("password", Akeneo.BaseAkeneoPassword);

            httpManager.AddBodyParameter(auth_data);
            httpManager.AddHeader(new WebClientHeader("Authorization", $"Basic {auth}"));

            var akeneoBaseResponse = String.Empty;

            try
            {
                akeneoBaseResponse = httpManager.PostData($"{Akeneo.BaseAkeneoUrl}{Akeneo.AkeneoAuthUrl}");
            }
            catch (Exception e)
            {
                akeneoStatus = BaseServicesStatuses.ServiceError;
                _logger.error(e.Message);
                return;
            }

            try
            {
                var akeneoJson =
                    JsonConvert.DeserializeObject <AkeneoAuthEntity>(akeneoBaseResponse);
                if (akeneoJson.access_token.Equals(String.Empty) || akeneoJson.access_token == null)
                {
                    akeneoStatus = BaseServicesStatuses.ServiceError;
                    _logger.error("Cannot obtain new token from Akeneo's API!");
                }
                else
                {
                    akeneoStatus = BaseServicesStatuses.ServiceLaunched;
                    var timer = new TimerCallback(Connect);
                    AkeneoUpdaterTimer = new Timer(timer, null, akeneoJson.expires_in * 1000 - 1, akeneoJson.expires_in * 1000);
                    akeneoAuthInfo     = akeneoJson;
                }
            }
            catch (Exception e)
            {
                akeneoStatus = BaseServicesStatuses.ServiceError;
                _logger.error(e.Message);
            }
        }
示例#3
0
        public bool BaseSend(AkeneoProduct product, string requestUrl, string protocol)
        {
            IBaseRequestHandler <NameValueCollection, WebClientHeader> httpManager = new BaseWebClientWriter();
            var EntitiesConverter = new AkeneoBaseProductEntityConverter();
            var AkeneoProductDto  = EntitiesConverter.ConvertToDtoEntity(product);

            try
            {
                var EncodedContent = JsonConvert.SerializeObject(AkeneoProductDto);
                httpManager.AddHeader(new WebClientHeader("Authorization", $"Bearer {akeneoAuthInfo.access_token}"));
                httpManager.AddBodyParameter(EncodedContent);
                var akeneoBaseResponse = String.Empty;
                try
                {
                    switch (protocol)
                    {
                    case "POST":
                        akeneoBaseResponse = httpManager.PostDataAsStringContext($"{Akeneo.BaseAkeneoUrl}{Akeneo.AkeneoProductCreateUrl}");
                        break;

                    case "PATCH":
                        akeneoBaseResponse = httpManager.PatchData(requestUrl);
                        break;
                    }

                    _logger.info($"(Creating) Product {product.productName} successfully created! Akeneo response is: {akeneoBaseResponse}");
                }
                catch (Exception e)
                {
                    _logger.error($"(Creating) Some error detected during creating product ({product.productName}), Akeneo response is: {e.Message}. ({product.productName})");
                    return(false);
                }
                return(true);
            }
            catch (Exception e)
            {
                _logger.error($"(Creating): (Exception): error detected, during preparing information for product {product.productName} -> {e.Message}.");
                return(false);
            }
        }