public virtual async Task <WebhookFeedEntry> LogAsync(WebhookFeedEntry feedEntry)
        {
            if (feedEntry == null)
            {
                throw new ArgumentNullException(nameof(feedEntry));
            }

            WebhookFeedEntry result = null;

            using (await AsyncLock.GetLockByKey(CacheKey.With(typeof(WebhookFeedEntry), feedEntry.WebHookId)).LockAsync())
            {
                switch (feedEntry.RecordType)
                {
                case (int)WebhookFeedEntryType.Success:
                    result = await LogSuccessAsync(feedEntry);

                    break;

                case (int)WebhookFeedEntryType.Error:
                    result = await LogErrorAsync(feedEntry);

                    break;

                default:
                    throw new ArgumentException($"Unsupported {nameof(WebhookFeedEntry.RecordType)}: {feedEntry.RecordType}.");
                }
            }

            return(result);
        }
        public virtual WebHookFeedEntryEntity FromModel(WebhookFeedEntry webHookFeedEntry, PrimaryKeyResolvingMap pkMap)
        {
            if (webHookFeedEntry == null)
            {
                throw new ArgumentNullException(nameof(webHookFeedEntry));
            }

            Id              = webHookFeedEntry.Id;
            CreatedBy       = webHookFeedEntry.CreatedBy;
            CreatedDate     = webHookFeedEntry.CreatedDate;
            ModifiedBy      = webHookFeedEntry.ModifiedBy;
            ModifiedDate    = webHookFeedEntry.ModifiedDate;
            WebHookId       = webHookFeedEntry.WebHookId;
            EventId         = webHookFeedEntry.EventId;
            AttemptCount    = webHookFeedEntry.AttemptCount;
            RecordType      = webHookFeedEntry.RecordType;
            Status          = webHookFeedEntry.Status;
            Error           = webHookFeedEntry.Error;
            RequestHeaders  = webHookFeedEntry.RequestHeaders;
            RequestBody     = webHookFeedEntry.RequestBody;
            ResponseHeaders = webHookFeedEntry.ResponseHeaders;
            ResponseBody    = webHookFeedEntry.ResponseBody;

            pkMap.AddPair(webHookFeedEntry, this);

            return(this);
        }
        public virtual WebhookFeedEntry ToModel(WebhookFeedEntry webHookFeedEntry)
        {
            if (webHookFeedEntry == null)
            {
                throw new ArgumentNullException(nameof(webHookFeedEntry));
            }

            webHookFeedEntry.Id              = Id;
            webHookFeedEntry.CreatedBy       = CreatedBy;
            webHookFeedEntry.CreatedDate     = CreatedDate;
            webHookFeedEntry.ModifiedBy      = ModifiedBy;
            webHookFeedEntry.ModifiedDate    = ModifiedDate;
            webHookFeedEntry.WebHookId       = WebHookId;
            webHookFeedEntry.EventId         = EventId;
            webHookFeedEntry.AttemptCount    = AttemptCount;
            webHookFeedEntry.RecordType      = RecordType;
            webHookFeedEntry.Status          = Status;
            webHookFeedEntry.Error           = Error;
            webHookFeedEntry.RequestHeaders  = RequestHeaders;
            webHookFeedEntry.RequestBody     = RequestBody;
            webHookFeedEntry.ResponseHeaders = ResponseHeaders;
            webHookFeedEntry.ResponseBody    = ResponseBody;

            return(webHookFeedEntry);
        }
        /// <summary>
        /// Logs Error in webHook notifications by saving log record.
        /// </summary>
        /// <param name="feedEntry"></param>
        /// <returns>Saved feed entry.</returns>
        protected virtual async Task <WebhookFeedEntry> LogErrorAsync(WebhookFeedEntry feedEntry)
        {
            if (feedEntry.IsTransient())
            {
                await _webHookFeedService.SaveChangesAsync(new[] { feedEntry });
            }
            else
            {
                await _webHookFeedService.UpdateCountAttemps(new[] { feedEntry });
            }

            return(feedEntry);
        }
Beispiel #5
0
        public static WebhookFeedEntry CreateFeedEntry(WebhookFeedEntryType entryType, string eventId, WebhookSendResponse response, Webhook webHook)
        {
            var result = new WebhookFeedEntry()
            {
                RecordType      = (int)entryType,
                WebHookId       = webHook.Id,
                EventId         = eventId,
                AttemptCount    = 0,
                Error           = new string(response.Error.Take(1024).ToArray()),
                Status          = response?.StatusCode ?? 0,
                RequestHeaders  = GetJsonString(webHook.RequestParams.Headers),
                RequestBody     = webHook.RequestParams.Body,
                ResponseHeaders = GetJsonString(response?.ResponseParams?.Headers),
                ResponseBody    = response?.ResponseParams?.Body,
            };

            return(result);
        }
        /// <summary>
        /// Logs Success webHook notifications by incrementing AttemptCount.
        /// </summary>
        /// <param name="feedEntry">Entry with the event data.</param>
        /// <returns>Saved feed entry.</returns>
        protected async Task <WebhookFeedEntry> LogSuccessAsync(WebhookFeedEntry feedEntry)
        {
            var criteria = new WebhookFeedSearchCriteria()
            {
                RecordTypes = new[] { (int)WebhookFeedEntryType.Success },
                WebHookIds  = new[] { feedEntry.WebHookId },
                Skip        = 0,
                Take        = 1,
            };
            var feedEntrySearchResult = await _webHookFeedSearchService.SearchAsync(criteria);

            var feedEntryToSave = feedEntrySearchResult.Results.FirstOrDefault();

            if (feedEntryToSave == null)
            {
                feedEntryToSave = feedEntry;
            }

            feedEntryToSave.AttemptCount++;

            await _webHookFeedService.SaveChangesAsync(new[] { feedEntryToSave });

            return(feedEntryToSave);
        }