Search code examples
c#podio

How to Test Podio Webhooks Locally


I am wanting to test Podio webhooks locally. I am using Conveyor.cloud for tunneling and tested it successfully with their Twilio example. The problem I am having with converting the code to work with Podio is that the Twilio example used a Controller and the Podio webhook example at http://podio.github.io/podio-dotnet/webhooks/ uses IHttpHandler.

I tried implementing IHttpHandler to the controller in the code below and it's not working.

using System;
using System.Web;
using System.Web.Mvc;
using PodioAPI;

namespace WebhooksProject.Controllers
{
    public class WebController : IHttpHandler
    {
        public static string clientId = "abcd";
        public static string clientSecret = "abcd";
        public static string username = "a@b.com";
        public static string password = "abcd";

        public static Podio podio = new Podio(clientId, clientSecret);

        public void ProcessRequest(HttpContext context)
        {
            podio.AuthenticateWithPassword(username, password);

            var request = context.Request;

            switch (request["type"])
            {
                case "hook.verify":
                    podio.HookService.ValidateHookVerification(int.Parse(request["hook_id"]), request["code"]);
                    break;
                // An item was created
                case "item.create":
                    // For item events you will get "item_id", "item_revision_id" and "external_id". in post params
                    int itemIdOfCreatedItem = int.Parse(request["item_id"]);
                    // Fetch the item and do what ever you want
                    break;

                // An item was updated
                case "item.update":
                    // For item events you will get "item_id", "item_revision_id" and "external_id". in post params
                    int itemIdOfUpdatedItem = int.Parse(request["item_id"]);
                    // Fetch the item and do what ever you want
                    break;

                // An item was deleted    
                case "item.delete":
                    // For item events you will get "item_id", "item_revision_id" and "external_id". in post params
                    int deletedItemId = int.Parse(request["item_id"]);
                    break;
            }
        }

        public bool IsReusable
        {
            get{return false;}
        }
    }
}

What am I missing?


Solution

  • You can do as below instead of using HttpContext. I basically use requestbin to get the request then try it directly using Postman.

        [HttpPost]
        public HttpResponseMessage ProcessRequest(PodioHook hook)
        {
            var oAuth = _podio.AuthenticateWithApp(_appId, _appToken); // keep the oauth if you are reusing it.
    
            switch (hook.type)
            {
                case "hook.verify":
                    _podio.HookService.ValidateHookVerification(hook.hook_id, hook.code);
                    break;
                // An item was created
                case "item.create":
                    // For item events you will get "item_id", "item_revision_id" and "external_id". in post params
                    long itemIdOfCreatedItem = hook.item_id;
                    // Fetch the item and do what ever you want
                    break;
    
                // An item was updated
                case "item.update":
                    // For item events you will get "item_id", "item_revision_id" and "external_id". in post params
                    long itemIdOfUpdatedItem = hook.item_id;
                    // Fetch the item and do what ever you want
                    break;
    
                // An item was deleted    
                case "item.delete":
                    // For item events you will get "item_id", "item_revision_id" and "external_id". in post params
                    long deletedItemId = hook.item_id;
                    break;
            }
    
            return new HttpResponseMessage(HttpStatusCode.OK);
        }
    

    my PodioHook model will looks like below

    public class PodioHook
    {
        public string code { get; set; }
        public string type { get; set; }
        public long item_id { get; set; }
        public int hook_id { get; set; }
    }
    

    .NET framework will take care of the request to model conversion.

    link to request bin https://requestbin.com/