The name Webhook does not available in current context.  Topic is solved

Discussions relating to the REST API of Jiwa 7.

The name Webhook does not available in current context.

Postby AlexSteven » Tue Jul 19, 2022 3:56 pm

Code: Select all
async private void Inventory_SaveEnd(object sender, EventArgs e)
    {
        JiwaFinancials.Jiwa.JiwaInventory.Inventory inventory = (JiwaFinancials.Jiwa.JiwaInventory.Inventory)sender;
        string body = inventory.DTO_Serialise().ToJson<JiwaFinancials.Jiwa.JiwaServiceModel.Inventory.InventoryItem>();

        Webhook(inventory.Manager, body, (inventory.InsertFlag) ? "inventory.created" : (inventory.DeleteFlag) ? "inventory.deleted" : "inventory.updated");
    }


Getting error on ToJson also saying InventoryItem does not contain a definition for ToJson
AlexSteven
I'm new here
I'm new here
 
Posts: 4
Joined: Tue Jul 19, 2022 3:46 pm

Re: The name Webhook does not available in current context.

Postby Mike.Sheen » Tue Jul 19, 2022 4:00 pm

AlexSteven wrote:Getting error on ToJson also saying InventoryItem does not contain a definition for ToJson


ToJson is an extension method - so you're probably missing a using statement to import the namespace (ServiceStack.Text I believe).
Mike Sheen
Chief Software Engineer
Jiwa Financials

If I do answer your question to your satisfaction, please mark it as the post solving the topic so others with the same issue can readily identify the solution
User avatar
Mike.Sheen
Overflow Error
Overflow Error
 
Posts: 2444
Joined: Tue Feb 12, 2008 11:12 am
Location: Perth, Republic of Western Australia
Topics Solved: 756

Re: The name Webhook does not available in current context.

Postby AlexSteven » Tue Jul 19, 2022 4:16 pm

Yes fixed the ToJON error but what about the Webhook method ?
AlexSteven
I'm new here
I'm new here
 
Posts: 4
Joined: Tue Jul 19, 2022 3:46 pm

Re: The name Webhook does not available in current context.  Topic is solved

Postby Mike.Sheen » Tue Jul 19, 2022 4:22 pm

AlexSteven wrote:Yes fixed the ToJON error but what about the Webhook method ?


Sorry - I didn't see that you also had a problem with that.

You need to add a reference to the REST API plugin where the Webhook method is defined. Note you want to add a plugin reference, and not an assembly reference.

It's best to show the exact reported error to us when trying to troubleshoot.
Mike Sheen
Chief Software Engineer
Jiwa Financials

If I do answer your question to your satisfaction, please mark it as the post solving the topic so others with the same issue can readily identify the solution
User avatar
Mike.Sheen
Overflow Error
Overflow Error
 
Posts: 2444
Joined: Tue Feb 12, 2008 11:12 am
Location: Perth, Republic of Western Australia
Topics Solved: 756

Re: The name Webhook does not available in current context.

Postby AlexSteven » Tue Jul 19, 2022 5:13 pm

can you share the complete example for creating Webhook?

I saw the documentation for using existing webhooks but I didn't find the documentation for creating own webhook
AlexSteven
I'm new here
I'm new here
 
Posts: 4
Joined: Tue Jul 19, 2022 3:46 pm

Re: The name Webhook does not available in current context.

Postby Mike.Sheen » Tue Jul 19, 2022 5:19 pm

AlexSteven wrote:can you share the complete example for creating Webhook?

I saw the documentation for using existing webhooks but I didn't find the documentation for creating own webhook


Webhooks are created for new things - like your own table / object.

We already have webhooks for when an inventory item is created, modified or deleted. You don't "create" webhooks for those, you simply subscribe to them. You don't do this in code, that's already done for you - you can subscribe to webhooks via the API itself.

This is documented on our Webhooks docs page. It shows some examples for creating a subscriber and subscription - but note you don't need to use code - you can do this as well by the Swagger UI or Postman.
Mike Sheen
Chief Software Engineer
Jiwa Financials

If I do answer your question to your satisfaction, please mark it as the post solving the topic so others with the same issue can readily identify the solution
User avatar
Mike.Sheen
Overflow Error
Overflow Error
 
Posts: 2444
Joined: Tue Feb 12, 2008 11:12 am
Location: Perth, Republic of Western Australia
Topics Solved: 756

Re: The name Webhook does not available in current context.

Postby AlexSteven » Tue Jul 19, 2022 5:26 pm

I completely agree with you, but how can I override the existing webhook or create a new one?

The code I posted is just for reproducing the error. I checked the webhook for inventory in the documentation you referred to.
AlexSteven
I'm new here
I'm new here
 
Posts: 4
Joined: Tue Jul 19, 2022 3:46 pm

Re: The name Webhook does not available in current context.

Postby Mike.Sheen » Tue Jul 19, 2022 6:42 pm

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.
REST API Plugin.png

2. Import the attached plugin, "Add new webhook" and save
Add new webhook plugin.png

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
Postman List Events.png (51.65 KiB) Viewed 2231 times

5. Add a new subscriber (if you don't already have one)
PostMan Create Subscriber.png
PostMan Create Subscriber.png (44.15 KiB) Viewed 2231 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
Postman Create Subscription.png

7. Close Jiwa.exe if it was already open, and login - open any form and you should see your webhook sent:
Webhook.Site.png


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.
Attachments
Plugin Add new webhook.xml
(10.51 KiB) Downloaded 37 times
Mike Sheen
Chief Software Engineer
Jiwa Financials

If I do answer your question to your satisfaction, please mark it as the post solving the topic so others with the same issue can readily identify the solution
User avatar
Mike.Sheen
Overflow Error
Overflow Error
 
Posts: 2444
Joined: Tue Feb 12, 2008 11:12 am
Location: Perth, Republic of Western Australia
Topics Solved: 756


Return to REST API

Who is online

Users browsing this forum: No registered users and 2 guests