/// <summary>
        /// Takes an IntegrationEventSubscription and returns it for addition
        /// </summary>
        /// <param name="schedule"></param>
        /// <returns>The IntegrationEventSubscription to be added</returns>
        public IntegrationEventSubscription AddIntegrationEventSubscription(IntegrationEventSubscription eventSubscription)
        {
            var existingIntegrationEventSubscription = _repo.Find(null, d => d.Name.ToLower() == eventSubscription.Name.ToLower())?.Items?.FirstOrDefault();

            if (existingIntegrationEventSubscription != null)
            {
                throw new EntityAlreadyExistsException("IntegrationEventSubscription name already exists");
            }

            return(eventSubscription);
        }
Exemple #2
0
        public async Task SendWebhook(IntegrationEventSubscription eventSubscription, WebhookPayload payload,
                                      IntegrationEventSubscriptionAttempt subscriptionAttempt, bool isSystemEvent, string payloadJSON)
        {
            var attemptCount = _attemptManager.SaveAndGetAttemptCount(subscriptionAttempt, eventSubscription.Max_RetryCount);

            payload.AttemptCount = attemptCount;

            if (isSystemEvent == true)//event is system event use system generated payload
            {
                payloadJSON = JsonConvert.SerializeObject(payload);
            }

            bool isSuccessful;

            try
            {
                isSuccessful = await SendWebhookAsync(payloadJSON, eventSubscription.HTTP_URL, eventSubscription);
            }
            catch (Exception exception) //an internal error occurred
            {
                throw exception;
            }

            if (!isSuccessful)
            {
                if (attemptCount > (eventSubscription.Max_RetryCount ?? 1))
                {
                    var previousAttempt = _attemptManager.GetLastAttempt(subscriptionAttempt);
                    previousAttempt.Status = "FailedFatally";
                    _attemptRepository.Update(previousAttempt);
                }
                else
                {
                    throw new Exception($"Webhook sending attempt failed.");
                }
            }
            else
            {
                var previousAttempt = _attemptManager.GetLastAttempt(subscriptionAttempt);
                previousAttempt.Status = "Completed";
                _attemptRepository.Update(previousAttempt);
            }

            return;
        }
Exemple #3
0
        public async Task <bool> SendWebhookAsync(string payloadJSON, string url, IntegrationEventSubscription eventSubscription)
        {
            var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);

            httpWebRequest.ContentType = "application/json";
            httpWebRequest.Method      = "POST";

            if (!String.IsNullOrEmpty(eventSubscription.HTTP_AddHeader_Key))
            {
                httpWebRequest.Headers[eventSubscription.HTTP_AddHeader_Key] = eventSubscription?.HTTP_AddHeader_Value ?? "";
            }

            using (var client = new HttpClient())
            {
                var response = await client.PostAsync(
                    url,
                    new StringContent(payloadJSON, Encoding.UTF8, "application/json")).ConfigureAwait(false);

                return(response.IsSuccessStatusCode);
            }
        }
        public async Task SendWebhook(IntegrationEventSubscription eventSubscription, WebhookPayload payload,
                                      IntegrationEventSubscriptionAttempt subscriptionAttempt)
        {
            var attemptCount = _attemptManager.SaveAndGetAttemptCount(subscriptionAttempt, eventSubscription.Max_RetryCount);

            payload.AttemptCount = attemptCount;

            bool isSuccessful;

            try
            {
                isSuccessful = await SendWebhookAsync(payload, eventSubscription.HTTP_URL, eventSubscription);
            }
            catch (Exception exception) //an internal error occurred
            {
                throw exception;
            }

            if (!isSuccessful)
            {
                if (attemptCount > eventSubscription.Max_RetryCount)
                {
                    var previousAttempt = _attemptManager.GetLastAttempt(subscriptionAttempt);
                    previousAttempt.Status = "FailedFatally";
                    _attemptRepository.Update(previousAttempt);
                }
                else
                {
                    throw new Exception($"Webhook sending attempt failed.");
                }
            }
            else
            {
                var previousAttempt = _attemptManager.GetLastAttempt(subscriptionAttempt);
                previousAttempt.Status = "Completed";
                _attemptRepository.Update(previousAttempt);
            }

            return;
        }
        /// <summary>
        /// Updates an IntegrationEventSubscription entity
        /// </summary>
        /// <param name="id"></param>
        /// <param name="request"></param>
        /// <returns></returns>
        public IntegrationEventSubscription UpdateIntegrationEventSubscription(string id, IntegrationEventSubscription request)
        {
            Guid entityId = new Guid(id);

            var existingEventSubscription = _repo.GetOne(entityId);

            if (existingEventSubscription == null)
            {
                throw new EntityDoesNotExistException("No IntegrationEventSubscription exists for the specified id");
            }

            var namedIntegrationEventSubscription = _repo.Find(null, d => d.Name.ToLower() == request.Name.ToLower() && d.Id != entityId)?.Items?.FirstOrDefault();

            if (namedIntegrationEventSubscription != null && namedIntegrationEventSubscription.Id != entityId)
            {
                throw new EntityAlreadyExistsException("IntegrationEventSubscription already exists");
            }

            existingEventSubscription.Name                 = request.Name;
            existingEventSubscription.EntityType           = request.EntityType;
            existingEventSubscription.IntegrationEventName = request.IntegrationEventName;
            existingEventSubscription.EntityID             = request.EntityID;
            existingEventSubscription.EntityName           = request.EntityName;
            existingEventSubscription.TransportType        = request.TransportType;
            existingEventSubscription.HTTP_URL             = request.HTTP_URL;
            existingEventSubscription.HTTP_AddHeader_Key   = request.HTTP_AddHeader_Key;
            existingEventSubscription.HTTP_AddHeader_Value = request.HTTP_AddHeader_Value;
            existingEventSubscription.Max_RetryCount       = request.Max_RetryCount;
            existingEventSubscription.QUEUE_QueueID        = request.QUEUE_QueueID;

            return(existingEventSubscription);
        }