Search This Blog

Tuesday, 14 May 2013

Why to use Javascript-Policy over XML-Policy?

I believe that doing Policy-Steps with Javascript has several advantages:

1. You can read the code easier
2. time saving because of the number of reduced steps
3. possibility to use shared libs

See here a comparison what I mean:
1. Extract the given number from path /method/1234 => will set path.uri to 1234
OR
var IDfromPath=context.getVariable("proxy.pathsuffix").split("/").pop();
context.setVariable("path.uri",IDfromPath);


2. Difference on a service-callout - extracts the ObjectID from a callout and save it to variable "myID"
3 Steps needed: Instead of:
var ex1 = httpClient.get("http://www.someendpoint.com?some=value");
ex1.waitForComplete(); 
var result=JSON.stringify(ex1.getResponse().content);
context.setVariable("myID",result["objectID"]);
I found several examples for this - you know what I mean :-)

4 comments:

  1. I actually agree to most of the points, but the
    question that persists is how big of an
    overhead issue would be to implement most
    of the policies through JS.

    Don't get me wrong, i wholeheartedly agree
    with you, but in cases of a lot of queries such
    overhead can have significant impact.

    Do you have any insight in such performance cost?

    ReplyDelete
  2. Hi Drago,

    thanks for your comment.

    I don't say it is a good practice to transform every xml-policy to a javascript equivalent. But in some cases you can save a lot of work and you can maintain changes easier.
    Because of the current rhino integration in some cases I would recommend xml-policies over javascript to be more stable. (I found some specials with extractVariables-policy and the use of variables in javascript functions)

    As you can see I am writing no new blogs since two weeks, because there will be some new great upcoming apigee features - and they can all be handled with javascript.

    So let us be patient to get the news (in the next few days..) and see what we can do then.

    ReplyDelete
  3. Hi Karsten,

    that was fast response :)

    Well, every direction has pros and cons. I've had enough problems
    with building custom xml queris (with namespaces) to know that
    current Rhino implementation isn't perfect, but it did work in the
    end.

    I hope to find some spare time one day to test the performance
    difference between these two approaches, since i would also like
    to use JS to simplify such cases.

    Oh, btw, i see you use JSON.stringify, i needed to include that as
    library file since from what i remember apigee doesn't provide
    such API, did i miss something?

    ReplyDelete
  4. I really appreciate information shared above. It’s of great help. If someone want to learn Online (Virtual) instructor lead live training in Apigee, kindly contact us http://www.maxmunus.com/contact
    MaxMunus Offer World Class Virtual Instructor led training on Apigee. We have industry expert trainer. We provide Training Material and Software Support. MaxMunus has successfully conducted 100000+ trainings in India, USA, UK, Australlia, Switzerland, Qatar, Saudi Arabia, Bangladesh, Bahrain and UAE etc.
    For Demo Contact us:
    Name : Arunkumar U
    Email : arun@maxmunus.com
    Skype id: training_maxmunus
    Contact No.-+91-9738507310
    Company Website –http://www.maxmunus.com



    ReplyDelete