コード例 #1
0
ファイル: WebHookManager.cs プロジェクト: knom/WebHooks
        /// <inheritdoc />
        public async Task <int> NotifyAsync(string user, IEnumerable <Notification> notifications, Func <WebHook, string, bool> predicate)
        {
            if (user == null)
            {
                throw new ArgumentNullException(nameof(user));
            }
            if (notifications == null)
            {
                throw new ArgumentNullException(nameof(notifications));
            }

            // Get all actions in this batch
            ICollection <Notification> nots = notifications.ToArray();
            var actions = nots.Select(n => n.Action).ToArray();

            // Find all active WebHooks that matches at least one of the actions
            var webHooks = await _webHookStore.QueryWebHooksAsync(user, actions, predicate);

            // For each WebHook set up a work item with the right set of notifications
            var workItems = GetWorkItems(webHooks, nots);

            // Start sending WebHooks
            await _webHookSender.SendWebHookWorkItemsAsync(workItems);

            return(webHooks.Count);
        }
コード例 #2
0
        public async Task <IHttpActionResult> Post(WebHook webHook)
        {
            if (webHook == null)
            {
                return(BadRequest());
            }

            webHook.WebHookUri = new Uri("https://webhook.site/e6e278c0-1958-4185-9c4d-f7cee654cd7f");
            webHook.Secret     = Guid.NewGuid().ToString();

            string userId = await GetUserId();

            await VerifyFilters(webHook);

            // await VerifyWebHook(webHook);

            try
            {
                // Ensure we have a normalized ID for the WebHook
                webHook.Id = null;

                // Add WebHook for this user.
                StoreResult result = await _store.InsertWebHookAsync(userId, webHook);

                if (result == StoreResult.Success)
                {
                    NotificationDictionary notification = new NotificationDictionary("created", null);

                    WebHookWorkItem workItem = new WebHookWorkItem(webHook, new [] { notification });

                    // WebHookWorkItem item = new WebHookWorkItem(webHook, );

                    IEnumerable <WebHookWorkItem> workItems = new[] { workItem };

                    await this.NotifyAsync("event1", new { P1 = "p1" });

                    await _sender.SendWebHookWorkItemsAsync(workItems);

                    // var a = _manager.NotifyAsync(userId, new[] {notification}, null);

                    return(CreatedAtRoute("WebhookHandler", new { id = webHook.Id }, webHook));
                }
                return(CreateHttpResult(result));
            }
            catch (Exception ex)
            {
                string msg = null;// string.Format(CultureInfo.CurrentCulture, CustomApiResources.RegistrationController_RegistrationFailure, ex.Message);
                HttpResponseMessage error = Request.CreateErrorResponse(HttpStatusCode.InternalServerError, msg, ex);
                return(ResponseMessage(error));
            }
        }
コード例 #3
0
        /// <inheritdoc />
        public async Task <int> NotifyAsync(string user, IEnumerable <NotificationDictionary> notifications, Func <WebHook, string, bool> predicate)
        {
            if (user == null)
            {
                throw new ArgumentNullException(nameof(user));
            }
            if (notifications == null)
            {
                throw new ArgumentNullException(nameof(notifications));
            }

            // Get all actions in this batch
            ICollection <NotificationDictionary> nots = notifications.ToArray();

            string[] actions = nots.Select(n => n.Action).ToArray();

            // Find all active WebHooks that matches at least one of the actions
            ICollection <WebHook> webHooks = await _webHookStore.QueryWebHooksAsync(user, actions, predicate);

            // For each WebHook set up a work item with the right set of notifications
            IEnumerable <WebHookWorkItem> workItems = GetWorkItems(webHooks, nots);

            _notificationBagge = new SendRequest();
            // Start sending WebHooks
            await _webHookSender.SendWebHookWorkItemsAsync(workItems);

            foreach (var workItem in workItems)
            {
                if (workItem.Properties.ContainsKey(BaladorConst.Messages))
                {
                    _notificationBagge = (SendRequest)workItem.Properties[BaladorConst.Messages];
                }
            }

            return(webHooks.Count);
        }