How to modify REST Request Coming from Other Source  Topic is solved

Discussions relating to plugin development, and the Jiwa API.

How to modify REST Request Coming from Other Source

Postby sameermoin » Sun Dec 04, 2022 4:08 am

Hi Everyone,


I am sending a SalesOrder request from my Custom app to the Jiwa endpoint. Now, I want to modify some properties when the request comes into Jiwa based on some CustomSetting conditions defined on the custom plugin.

I am sending a default StaffId on a request and I want to update it when the request came into Jiwa by getting a StaffId from Custom Settings.


Regards,

Sameer
sameermoin
Regular Contributor
Regular Contributor
 
Posts: 75
Joined: Wed Jun 15, 2022 4:02 am
Topics Solved: 1

Re: How to modify REST Request Coming from Other Source

Postby SBarnes » Sun Dec 04, 2022 7:35 am

You have one of two choices one just using ServiceStack and other is Jiwa Specific

ServiceStack

Set up a request filter and modify things there, but you would need to deserialize the Json change it and then serialize it again

Jiwa

In Jiwa on most objects that run through the api there are events on the serialization and deserialization, the one most appropriate for what you are wanting to do is is DTODeserialiseStart which has the signature shown below, where the sender is obviously the sales order object and the DTO is the incoming Json deserialized onto the DTO object from the Service Model namespace ready to be transposed onto the sales order business object. You would be changing the DTO's values so they thne got transposed to the sales order

Code: Select all
DTODeserialiseStart(sender As Object, e As System.EventArgs, ByRef DTO As JiwaServiceModel.SalesOrders.SalesOrder)


I would use the Jiwa specific version for anything that requires looking up values etc as you have access to a manager on the sales order object.

A further thought on this is if this a business rule in general or you don't care if the values are set on other orders you could put the code in the save start event of the order.
Regards
Stuart Barnes
SBarnes
Shihan
Shihan
 
Posts: 1619
Joined: Fri Aug 15, 2008 3:27 pm
Topics Solved: 175

Re: How to modify REST Request Coming from Other Source

Postby Mike.Sheen » Mon Dec 05, 2022 10:58 am

sameermoin wrote:I am sending a SalesOrder request from my Custom app to the Jiwa endpoint. Now, I want to modify some properties when the request comes into Jiwa based on some CustomSetting conditions defined on the custom plugin.

I am sending a default StaffId on a request and I want to update it when the request came into Jiwa by getting a StaffId from Custom Settings.


Another option in addition to Stuarts' suggestions is to create your own route for handling sales order POST or PATCH requests. You just copy what is our standard Service request, and in there you have full control over what happens when the request is received, so you can apply your custom logic. You'd need your custom app to use your custom routes instead of the Jiwa ones, but other than changing the path your custom app would not need to change at all.
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: How to modify REST Request Coming from Other Source

Postby SBarnes » Mon Dec 05, 2022 3:20 pm

Another option in addition to Stuarts' suggestions is to create your own route for handling sales order POST or PATCH requests. You just copy what is our standard Service request, and in there you have full control over what happens when the request is received, so you can apply your custom logic. You'd need your custom app to use your custom routes instead of the Jiwa ones, but other than changing the path your custom app would not need to change at all.


Whilst I did think of that I ruled it out due to needing to update two routes and because of the possible maintenance issues it may create as upgrades to Jiwa happen going forward but yes it is another option.
Regards
Stuart Barnes
SBarnes
Shihan
Shihan
 
Posts: 1619
Joined: Fri Aug 15, 2008 3:27 pm
Topics Solved: 175

Re: How to modify REST Request Coming from Other Source

Postby sameermoin » Mon Dec 05, 2022 6:37 pm

SBarnes wrote:You have one of two choices one just using ServiceStack and other is Jiwa Specific

ServiceStack

Set up a request filter and modify things there, but you would need to deserialize the Json change it and then serialize it again

Jiwa

In Jiwa on most objects that run through the api there are events on the serialization and deserialization, the one most appropriate for what you are wanting to do is is DTODeserialiseStart which has the signature shown below, where the sender is obviously the sales order object and the DTO is the incoming Json deserialized onto the DTO object from the Service Model namespace ready to be transposed onto the sales order business object. You would be changing the DTO's values so they thne got transposed to the sales order

Code: Select all
DTODeserialiseStart(sender As Object, e As System.EventArgs, ByRef DTO As JiwaServiceModel.SalesOrders.SalesOrder)


I would use the Jiwa specific version for anything that requires looking up values etc as you have access to a manager on the sales order object.

A further thought on this is if this a business rule in general or you don't care if the values are set on other orders you could put the code in the save start event of the order.



Is it possible to make it more specific so that it won't interrupt other plugins? I mean filter it based on header parameters.

The event you are recommending will trigger on all SalesOrder requests coming via API. I want to make it source specific.
sameermoin
Regular Contributor
Regular Contributor
 
Posts: 75
Joined: Wed Jun 15, 2022 4:02 am
Topics Solved: 1

Re: How to modify REST Request Coming from Other Source

Postby SBarnes » Mon Dec 05, 2022 7:05 pm

In that case you could either use the request filter option I mentioned and do all the work there, use a custom field and look for that value in the deserialisation event, you could either have your source fill it in or in a request filter do it based upon something on the request like the presence of a header, or you could go with Mike's suggestion and use stand alone routes.

There is also the option that if you go with creating your own route you can call another service/route this forum post viewtopic.php?f=26&t=1254&p=5102&hilit=csv#p5102 has a plugin that I wrote that does that to call the report service.
Regards
Stuart Barnes
SBarnes
Shihan
Shihan
 
Posts: 1619
Joined: Fri Aug 15, 2008 3:27 pm
Topics Solved: 175

Re: How to modify REST Request Coming from Other Source  Topic is solved

Postby Mike.Sheen » Fri Dec 09, 2022 6:44 pm

This is what Stuart was suggesting:

Code: Select all
namespace JiwaFinancials.Jiwa.JiwaServiceModel
{
    public class CustomRESTAPIPlugin : System.MarshalByRefObject, JiwaFinancials.Jiwa.JiwaApplication.IJiwaRESTAPIPlugin
    {
        public void Configure(JiwaFinancials.Jiwa.JiwaApplication.Plugin.Plugin Plugin, ServiceStack.ServiceStackHost AppHost, Funq.Container Container, JiwaApplication.Manager JiwaApplicationManager)
        {
            AppHost.RegisterService<CustomServices>();
            AppHost.Routes.Add(typeof(SalesOrderPOSTRequest), "/CustomSalesOrders", "POST", "Creates a new sales order with custom actions.", "");
        }
    } 

    #region "Services"   
    public class CustomServices : Service
    {
        [Authenticate]
        public JiwaFinancials.Jiwa.JiwaServiceModel.SalesOrders.SalesOrder Post(SalesOrderPOSTRequest request)
        {
         using (SalesOrderServices salesOrderServices = base.ResolveService<SalesOrderServices>())
         {
            if (DateTime.Now.DayOfWeek == DayOfWeek.Friday)
            {
               // on fridays we add a yippee to the orderno
               request.OrderNo += " YIPEE!!";
            }
            
            return salesOrderServices.Post(request);
         }
        }
    }
    #endregion
}


In the above we add a new route, /CustomSalesOrders and when you POST a sales order DTO to that I meddle with the order no of the DTO if it is a Friday and then pass it onto the standard Jiwa sales order POST.
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: How to modify REST Request Coming from Other Source

Postby SBarnes » Fri Dec 09, 2022 6:52 pm

And if you want to get at the raw request inside the service you can use base.Request to look at the http related parts and you will need to add the REST api plugin to plugin references
Regards
Stuart Barnes
SBarnes
Shihan
Shihan
 
Posts: 1619
Joined: Fri Aug 15, 2008 3:27 pm
Topics Solved: 175

Re: How to modify REST Request Coming from Other Source

Postby Mike.Sheen » Fri Dec 09, 2022 6:56 pm

SBarnes wrote:And if you want to get at the raw request inside the service you can use base.Request to look at the http related parts and you will need to add the REST api plugin to plugin references


Good point - I hadn't considered the OP wanting the headers or anything... maybe they do.

The original statement was:

I want to modify some properties when the request comes into Jiwa based on some CustomSetting conditions defined on the custom plugin


Which could be interpreted a number of ways.

I love a good mystery.
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 Technical and or Programming

Who is online

Users browsing this forum: No registered users and 36 guests

cron