Attached is a plugin which creates a new "form.opened" webhook event, and whenever a form in Jiwa is opened, it sends to the webhook a simple json payload containing the form classname.
1. Enable the REST API plugin, and make sure the WebhooksHostURL is configured on the System Settings tab. The URL should be that of the API itself. In my environment I'm using localhost:81 - but keep in mind in production environments you need to use a DNS or IP address which can be reached by other machines - localhost would be no good for that.
2. Import the attached plugin, "Add new webhook" and save
3. Configure and Start the API service. The URLBase should be the same as the WebhooksHostURL as configured in Step 1.
4. Using Postman, or any other tool, verify the new form.opened event defined by the plugin imported in step 2 is present by performing a GET on the /Webhooks/events route
- Postman List Events.png (51.65 KiB) Viewed 2244 times
5. Add a new subscriber (if you don't already have one)
- PostMan Create Subscriber.png (44.15 KiB) Viewed 2244 times
6. Create a new subscription. I opted to use a free online tool, webhook.site for receiving my webhook so that's the URL I provided
7. Close Jiwa.exe if it was already open, and login - open any form and you should see your webhook sent:
The code in entirety of the plugin is as below
- Code: Select all
using System;
using JiwaFinancials.Jiwa;
using ServiceStack;
public class MyWebhooksRESTAPIPlugin : System.MarshalByRefObject, JiwaFinancials.Jiwa.JiwaApplication.IJiwaRESTAPIPlugin
{
public void Configure(JiwaFinancials.Jiwa.JiwaApplication.Plugin.Plugin Plugin, ServiceStack.ServiceStackHost AppHost, Funq.Container Container, JiwaFinancials.Jiwa.JiwaApplication.Manager JiwaApplicationManager)
{
// Add a new webhook event for subscribers to subscribe to when it occurs.
JiwaFinancials.Jiwa.JiwaServiceModel.RESTAPIPlugin.WebHookEvents.Add(new JiwaFinancials.Jiwa.JiwaServiceModel.WebHookEvent { Name = "form.opened", Description = "Occurs when a form in Jiwa is opened." });
}
}
// DTO to send
public class FormOpenedWebhookRequest
{
virtual public string FormClassName { get; set; }
}
// Code in here will send a webhook to the API whenever a form is opened, and the API will then relay that to subscribers.
// You must ensure your REST API plugin is enabled, the service running and the WebhooksHostURL system setting of the REST API plugin is set to be the address of the API.
#region "ApplicationManagerPlugin"
public class ApplicationManagerPlugin : System.MarshalByRefObject, JiwaFinancials.Jiwa.JiwaApplication.IJiwaApplicationManagerPlugin
{
public override object InitializeLifetimeService()
{
// returning null here will prevent the lease manager
// from deleting the Object.
return null;
}
public void Setup(JiwaFinancials.Jiwa.JiwaApplication.Plugin.Plugin Plugin)
{
Plugin.Manager.FormFactory.AfterFormStart += delegate(JiwaFinancials.Jiwa.JiwaApplication.IJiwaForm JiwaForm) { AfterFormStart(JiwaForm, Plugin); };
}
private void AfterFormStart(JiwaFinancials.Jiwa.JiwaApplication.IJiwaForm JiwaForm, JiwaFinancials.Jiwa.JiwaApplication.Plugin.Plugin Plugin)
{
FormOpenedWebhookRequest formOpened = new FormOpenedWebhookRequest() { FormClassName = JiwaForm.GetType().ToString() };
// Call the webhook. Note we pass "REST API" as the plugin name, not our own, because I want to use the WebhooksHostURL system setting set against that plugin.
BusinessLogicPlugin.Webhook(JiwaForm.Manager, formOpened.ToJson<FormOpenedWebhookRequest>(), "form.opened", "REST API");
}
}
#endregion
The guts of it is simply this line:
- Code: Select all
BusinessLogicPlugin.Webhook(JiwaForm.Manager, formOpened.ToJson<FormOpenedWebhookRequest>(), "form.opened", "REST API");
BusinessLogicPlugin is a reference to the class in the REST API plugin - because I added a plugin reference so my plugin knows about the REST API plugin, I could invoke methods (such as BusinessLogicPlugin.Webhook) which are inside the REST API plugin from within my plugin.