public JsonResult Test(Guid id)
        {
            var errorResponse = new WebhookExecutionResponse {
                Success = false, Response = "Unable to execute webhook. This is probably caused by suitable content being unavailable below the selected parent."
            };
            var webhook = _repo.GetWebhook(id);

            if (webhook.ParentId > 0)
            {
                var parentRef = new ContentReference(webhook.ParentId);
                if (ContentReference.IsNullOrEmpty(parentRef))
                {
                    return(Json(errorResponse));
                }
                var parentContent = _contentLoader.Get <IContent>(parentRef);
                if (parentContent == null)
                {
                    return(Json(errorResponse));
                }
                var testEventType = webhook.EventTypes?.Select(x => _repo.GetEventTypes().FirstOrDefault(y => y.Key.Equals(x)))?.FirstOrDefault(x => x != null) ?? Constants.PublishEvent;
                if (webhook.ContentTypes == null || !webhook.ContentTypes.Any() || webhook.ContentTypes.Contains(parentContent.ContentTypeID))
                {
                    return(Json(webhook.Execute(parentContent, testEventType, _repo), JsonRequestBehavior.AllowGet));
                }
                else
                {
                    var childRef = _contentLoader.GetDescendents(parentRef).FirstOrDefault(x => webhook.ContentTypes.Contains(_contentLoader.Get <IContent>(x).ContentTypeID));
                    if (childRef != null)
                    {
                        return(Json(webhook.Execute(_contentLoader.Get <IContent>(childRef), testEventType, _repo), JsonRequestBehavior.AllowGet));
                    }
                }
            }
            return(Json(errorResponse, JsonRequestBehavior.AllowGet));
        }
Пример #2
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);
        }