Ejemplo n.º 1
0
 private static async Task SendWebhookAsync()
 {
     var notifications = new List <NotificationDictionary> {
         new NotificationDictionary("event1", new { DynamicParamA = 100, DynamicParamB = 200 })
     };
     var x = await _whManager.NotifyAsync("user1", notifications);
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Submits a notification to all matching registered WebHooks. To match, the <see cref="WebHook"/> must be registered by the
        /// current <see cref="ControllerBase.User"/> and have a filter that matches one or more of the actions provided for the notification.
        /// </summary>
        /// <param name="controller">The <see cref="ControllerBase"/> instance.</param>
        /// <param name="notifications">The set of notifications to include in the WebHook.</param>
        /// <param name="predicate">A function to test each <see cref="WebHook"/> to see whether it fulfills the condition. The
        /// predicate is passed the <see cref="WebHook"/> and the user who registered it. If the predicate returns <c>true</c> then
        /// the <see cref="WebHook"/> is included; otherwise it is not.</param>
        /// <returns>The number of <see cref="WebHook"/> instances that were selected and subsequently notified about the actions.</returns>
        public static async Task <int> NotifyAsync(this ControllerBase controller, IEnumerable <NotificationDictionary> notifications, Func <WebHook, string, bool> predicate)
        {
            if (controller == null)
            {
                throw new ArgumentNullException(nameof(controller));
            }
            if (notifications == null)
            {
                throw new ArgumentNullException(nameof(notifications));
            }
            if (!notifications.Any())
            {
                return(0);
            }


            // Get the User ID from the User principal
            IWebHookUser user   = controller.HttpContext.RequestServices.GetUser();
            string       userId = await user.GetUserIdAsync(controller.User);

            // Send a notification to registered WebHooks with matching filters
            IWebHookManager manager = controller.HttpContext.RequestServices.GetManager();

            return(await manager.NotifyAsync(userId, notifications, predicate));
        }
Ejemplo n.º 3
0
 /// <summary>
 /// Send Notification to register clients
 /// </summary>
 /// <param name="product">product code</param>
 /// <param name="productName"> New Product Name</param>
 /// <returns></returns>
 private static async Task SendWebhookAsync(string product, string productName)
 {
     var notifications = new List <NotificationDictionary> {
         new NotificationDictionary("entryupdating", new { id = product, name = productName })
     }.AsEnumerable();
     var x = await _whManager.NotifyAsync("user1", notifications, null);
 }
Ejemplo n.º 4
0
        private Task TriggerContentEvent(string eventName, ContentContextBase context)
        {
            var contentItem = context.ContentItem;

            // Setup the properties used in the liquid templates
            var properties = new Dictionary <string, object>
            {
                { "Model", contentItem }
            };

            return(Task.WhenAll(
                       // Trigger webhooks for the general content.{event} event
                       _webhooksManager.NotifyAsync($"content.{eventName}", contentItem.Content, properties),

                       // Trigger webhooks for the more specific {content type}.{event} event e.g. article.created
                       _webhooksManager.NotifyAsync($"{contentItem.ContentType.ToLower()}.{eventName}", contentItem.Content, properties)
                       ));
        }
        /// <summary>
        /// Send Notification to register clients
        /// </summary>
        /// <param name="product">product code</param>
        /// <param name="productName"> New Product Name</param>
        /// <returns></returns>
        private static async Task SendWebhookContentChangedAsync(string product, string productName)
        {
            string eventName     = "contentschanged";
            var    notifications = new List <NotificationDictionary> {
                new NotificationDictionary(eventName, new { id = product, name = productName })
            };

            // Send a notification to registered WebHooks with matching filters
            IWebHookManager manager = DependencyResolver.Current.GetManager();
            //ToDo: get all subscribers
            var x = await manager.NotifyAsync("admin", notifications);
        }
Ejemplo n.º 6
0
        private static async Task fireWebhook()
        {
            // create a simple notification that indicates an event occurred
            // this would normally occur in another service
            var notifications = new List <NotificationDictionary> {
                new NotificationDictionary("event1", new { Value = "Response from Webhook", Message = "Your Event occurred" })
            };

            // simulate a different event and try to fire it for the receiver
            // the receiver shouldn't get this notification because it didn't register interest in this event
            notifications.Add(new NotificationDictionary("BogusEvent", new { Value = "Response from Webhook", Message = "Your bogus event occurred" }));

            Console.WriteLine("Firing webhook (normally done by an external service)...");

            // fire the webhook with multiple notifications (invoke the callback URL)
            var x = await whManager.NotifyAsync("user1", notifications);
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Submits a notification to all matching registered WebHooks. To match, the <see cref="WebHook"/> must be registered by the
        /// current <see cref="Controller.User"/> and have a filter that matches one or more of the actions provided for the notification.
        /// </summary>
        /// <param name="controller">The MVC <see cref="Controller"/> instance.</param>
        /// <param name="notifications">The set of notifications to include in the WebHook.</param>
        /// <param name="predicate">A function to test each <see cref="WebHook"/> to see whether it fulfills the condition. The
        /// predicate is passed the <see cref="WebHook"/> and the user who registered it. If the predicate returns <c>true</c> then
        /// the <see cref="WebHook"/> is included; otherwise it is not.</param>
        /// <returns>The number of <see cref="WebHook"/> instances that were selected and subsequently notified about the actions.</returns>
        public static async Task <int> NotifyAsync(this Controller controller, IEnumerable <IWebHookNotification> notifications, Func <WebHook, string, bool> predicate)
        {
            if (controller == null)
            {
                throw new ArgumentNullException("controller");
            }
            if (notifications == null)
            {
                throw new ArgumentNullException("notifications");
            }
            if (!notifications.Any())
            {
                return(0);
            }

            // Get the User ID from the User principal
            string userId = controller.User.Identity.Name;

            // Send a notification to registered WebHooks with matching filters
            IWebHookManager manager = (IWebHookManager)controller.HttpContext.RequestServices.GetService(typeof(IWebHookManager));

            return(await manager.NotifyAsync(userId, notifications, predicate));
        }
        /// <summary>
        /// Submits a notification to all matching registered WebHooks. To match, the <see cref="WebHook"/> must be registered by the
        /// current <see cref="Controller.User"/> and have a filter that matches one or more of the actions provided for the notification.
        /// </summary>
        /// <param name="controller">The MVC <see cref="Controller"/> instance.</param>
        /// <param name="notifications">The set of notifications to include in the WebHook.</param>
        /// <returns>The number of <see cref="WebHook"/> instances that were selected and subsequently notified about the actions.</returns>
        public static async Task <int> NotifyAsync(this Controller controller, params NotificationDictionary[] notifications)
        {
            if (controller == null)
            {
                throw new ArgumentNullException("controller");
            }
            if (notifications == null)
            {
                throw new ArgumentNullException("notifications");
            }
            if (notifications.Length == 0)
            {
                return(0);
            }

            // Get the User ID from the User principal
            IWebHookUser user   = DependencyResolver.Current.GetUser();
            string       userId = await user.GetUserIdAsync(controller.User);

            // Send a notification to registered WebHooks with matching filters
            IWebHookManager manager = DependencyResolver.Current.GetManager();

            return(await manager.NotifyAsync(userId, notifications));
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Submits a notification to all matching registered WebHooks. To match, the <see cref="WebHook"/> must be registered by the
        /// current <see cref="Controller.User"/> and have a filter that matches one or more of the actions provided for the notification.
        /// </summary>
        /// <param name="controller">The MVC <see cref="Controller"/> instance.</param>
        /// <param name="notifications">The set of notifications to include in the WebHook.</param>
        /// <param name="predicate">A function to test each <see cref="WebHook"/> to see whether it fulfills the condition. The
        /// predicate is passed the <see cref="WebHook"/> and the user who registered it. If the predicate returns <c>true</c> then
        /// the <see cref="WebHook"/> is included; otherwise it is not.</param>
        /// <returns>The number of <see cref="WebHook"/> instances that were selected and subsequently notified about the actions.</returns>
        public static async Task <int> NotifyAsync(this Controller controller, IEnumerable <NotificationDictionary> notifications, Func <WebHook, string, bool> predicate)
        {
            if (controller == null)
            {
                throw new ArgumentNullException("controller");
            }
            if (notifications == null)
            {
                throw new ArgumentNullException("notifications");
            }
            if (!notifications.Any())
            {
                return(0);
            }

            // Get the User ID from the User principal
            IWebHookUser user   = DependencyResolver.Current.GetUser();
            string       userId = await user.GetUserIdAsync(controller.User);

            // Send a notification to registered WebHooks with matching filters
            IWebHookManager manager = DependencyResolver.Current.GetManager();

            return(await manager.NotifyAsync(userId, notifications, predicate));
        }