How to map url request parameter values correctly to method

Questions, tips and tricks and techniques for scripting in Servoy

How to map url request parameter values correctly to method

Postby j.de.zwaan » Wed Sep 16, 2020 1:49 pm

I have a rest_ws getRequest that I am processing in a ws_read method.
The arguments that I expect to receive are in the form <MethodName>?param1=value1&...&paramn=valuen

I have several methods with varying parameters that need to be called with the argument values in the correct order.
E.g. getArticle?adminId=1&customerId=2&articleId=3 will have to be mapped to method getArticle(adminId, customerId,articleId).
But also getArticle?adminId=1&articleId=3&customerId=2 will have to be mapped to method getArticle(adminId, customerId,articleId).

The values needed for the method can be taken from the last argument by getting the values for the keys.
So far so good, but I don't want the code cluttered with switch statements to explicitly map the arguments to the method parameters.

I don't want to code like
Code: Select all
          case "getArticle": scopes.api.getArticle(arguments[1]["adminId"],arguments[1]["customerId"], arguments[1]["articleId"]).

I want it made generic like
Code: Select all
          var methodParameters = scopes.api[arguments[0]].getMethodParameters();
          var parameterValues = [];
          for(var i=0; i<methodParameters.length; i++){
               parameterValues.push(arguments[1][methodParameters[i]]);
          }
          scopes.api[arguments[0]](parameterValues);


So that even when the parameters are given in a different order, or getting another methodname with other parameters from the request, I don't have to alter this when extending with more methods in my api scope.

I tried to use the JSMethod method "getArguments()" (instead of my pseudo method getMethodParameters()) but that always returns null.
Is there a(nother) way to get the parameters of my scope method and call that method with the parameterValues filled with the request argument values in the right order?
Or is there some way to pass parameters to a method differently?

Of course I will add checks on the validity and completeness of the request arguments (but that is outside the scope of my question)
j.de.zwaan
 
Posts: 2
Joined: Wed Sep 16, 2020 12:56 pm

Re: How to map url request parameter values correctly to met

Postby ROCLASI » Wed Sep 16, 2020 2:45 pm

Hi,

Why do you want to pass the values as separate arguments to your functions.
You can just pass the object with the named properties, this way the order doesn't make any difference.

You simply call it like this:
Code: Select all
scopes.api.getArticle(arguments[1]).

And your api method will look like this:
Code: Select all
/**
* @param {{adminId:Number, customerId:Number, articleId:Number}} queryParams
*/
function getArticle(queryParams) {
    if ( queryParams.adminId ) {
        // do something
    }
    // etc.
}



Hope this helps.
Robert Ivens
SAN Developer / Servoy Valued Professional / Servoy Certified Developer

ROCLASI Software Solutions / JBS Group, Partner
Mastodon: @roclasi
--
ServoyForge - Building Open Source Software.
PostgreSQL - The world's most advanced open source database.
User avatar
ROCLASI
Servoy Expert
 
Posts: 5438
Joined: Thu Oct 02, 2003 9:49 am
Location: Netherlands/Belgium

Re: How to map url request parameter values correctly to met

Postby j.de.zwaan » Wed Sep 16, 2020 3:21 pm

Thanks!

That I did not think of that solution myself!
Focussing too long on one direction makes you blind for other solutions.

It solves my problem.
j.de.zwaan
 
Posts: 2
Joined: Wed Sep 16, 2020 12:56 pm


Return to Methods

Who is online

Users browsing this forum: No registered users and 11 guests