Ejemplo n.º 1
0
        public static WebhookExecutionResponse Execute(this Webhook webhook, IContent content, EventType eventType, IWebhookRepository repo, Dictionary <string, object> extraData = null)
        {
            var res = new WebhookExecutionResponse {
                Response = string.Empty, Success = false
            };

            try
            {
                IContentModelMapper mapper = _contentModelMapperFactory.Value.GetMapper(content);
                var refs         = _contentRepository.Value.GetReferencesToContent(content.ContentLink, eventType.ImpactsDescendants);
                var payload      = new WebhookDetailedPayload(content, eventType.Key, DateTime.UtcNow);
                var referencedBy = new List <ContentInfo>();
                referencedBy.AddRange(refs.Select(x => new ContentInfo(_contentRepository.Value.Get <IContent>(x.OwnerID, x.OwnerLanguage))));
                if (eventType.ImpactsDescendants)
                {
                    var descendents = _contentRepository.Value.GetDescendents(content.ContentLink);
                    foreach (var descendent in descendents.Take(_maxDescendents))
                    {
                        if (!referencedBy.Any(x => x.ContentId.Equals(descendent.ID)))
                        {
                            referencedBy.Add(new ContentInfo(_contentRepository.Value.Get <IContent>(descendent)));
                        }
                    }
                }
                payload.ReferencedBy = referencedBy.GroupBy(x => x.ContentId).Select(x => x.FirstOrDefault());
                payload.Content      = mapper.TransformContent(content, false, "*");
                payload.ExtraData    = extraData;

                var url          = repo.ReplacePlaceholders(webhook.Url);
                var uri          = new Uri(url);
                var servicePoint = ServicePointManager.FindServicePoint(uri);
                servicePoint.Expect100Continue = false;
                using (var wc = new WebClient())
                {
                    wc.Encoding = Encoding.UTF8;
                    foreach (var header in webhook.Headers ?? new Dictionary <string, string>())
                    {
                        wc.Headers.Add(repo.ReplacePlaceholders(header.Key), repo.ReplacePlaceholders(header.Value));
                    }
                    wc.Headers.Add(HttpRequestHeader.ContentType, "application/json");
                    var response = wc.UploadString(url, "POST", JsonConvert.SerializeObject(payload));
                    res.Response = response;
                    res.Success  = true;
                }
                webhook.LastResult = $"OK\n{res.Response}";
            }
            catch (WebException ex)
            {
                res.Response       = $"{ex?.Message ?? string.Empty}\n\n{ex?.StackTrace ?? string.Empty}";
                webhook.LastResult = $"ERROR ({ex.Status})\n\n{ex?.Message ?? string.Empty}\n\n{ex?.StackTrace ?? string.Empty}";
            }
            catch (Exception ex)
            {
                res.Response       = $"{ex?.Message ?? string.Empty}\n\n{ex?.StackTrace ?? string.Empty}";
                webhook.LastResult = $"ERROR\n\n{ex?.Message ?? string.Empty}\n\n{ex?.StackTrace ?? string.Empty}";
            }
            webhook.LastExecuted = DateTime.UtcNow;
            repo.SaveWebhook(webhook);
            return(res);
        }
        /// <summary>
        /// Maps an instance of IContent to ContentApiModel and additionally add info about existing languages
        /// </summary>
        public ContentApiModel TransformContent(IContent content, bool excludePersonalizedContent, string expand)
        {
            var contentModel = _defaultContentModelMapper.TransformContent(content, excludePersonalizedContent, expand);

            contentModel.Url = ResolveUrl(content.ContentLink, content.LanguageBranch());

            contentModel.Properties = contentModel.Properties.Select(FlattenProperty).ToDictionary(x => x.Key, x => x.Value);

            if (contentModel.ParentLink != null)
            {
                contentModel.ParentLink.Url = ResolveUrl(content.ParentLink, content.LanguageBranch());
            }

            return(contentModel);
        }
Ejemplo n.º 3
0
        public ActionResult Get(int contentTypeId, string language, string properties, string keyword = "")
        {
            var contentType       = _contentTypeRepository.Load(contentTypeId);
            var catalogContent    = Type.GetType("EPiServer.Commerce.Catalog.ContentTypes.CatalogContentBase, EPiServer.Business.Commerce", false)?.IsAssignableFrom(contentType.ModelType) ?? false;
            var contentReferences = _contentLoader.GetDescendents(!catalogContent ? ContentReference.RootPage : GetContentLink(1, 1, 0));
            var contents          = GetItemsWithFallback(contentReferences, language);

            contents = contents.Where(o => o.ContentTypeID == contentTypeId).ToList();
            if (!string.IsNullOrWhiteSpace(keyword))
            {
                contents = contents.Where(o => o.Name.IndexOf(keyword, StringComparison.CurrentCultureIgnoreCase) >= 0);
            }
            var models = contents.Select(o => _contentModelMapper.TransformContent(o, false, properties));

            return(new ContentResult
            {
                Content = JsonConvert.SerializeObject(models),
                ContentType = "application/json",
            });
        }