use of conditionals to replace text values with numeric codes

Not applicable

I am fairly new to Apigee Edge and can't seem to find a direct answer to my issue in the documentation. I apologize in advance if I missed it.

Here is my situation.

I have a backend service that is looking for a numeric value that represents a relationship... for example,

01 = Self

02 = Spouse

03 = Child

04 = Parent

etc...

I am passing in the information I need to the proxy in Headers. I don't want the user to have to know these codes... I want them to be able to specify Self, Spouse, Child, Parent, etc.... when passing the information in the header.

I want to then be able to test the value of the text in that header and assign the appropriate code to the variable, which will then be used in my Soap request to the backend.

How do I go about doing this?

Thanks

1 3 382
3 REPLIES 3

@Don Long , Great Question & Welcome to Apigee Community.

Just little bit more info needed, is it a fixed small set of maps / is it a large set of maps that you would like to manage ? If it's a few key value maps, you can hard code same using Javascript Policy. If you would like to manage the sets & store it in a datastore, we have Key Value Maps datastore in Apigee Edge where you can store the maps & retrieve same in API proxy during runtime.

Keep us posted.

Small set of pairs... 5...

@Don Long ,

There are multiple ways you can solve above query in Apigee Edge. I am going to suggest you simple & fastest way using which you can achieve what you are looking for.

As You know, Apigee Edge Gateway sits in between Client & Target server. You will have full control of request / response payload in API Proxy & you can manipulate as per your need. Edge gateway offers multiple out of the box policies using which you can add functionality on top of APIs. Javascript Policy is one such policy using which you can write simple code & manipulate request payload.

Add a Javascript Policy anywhere in request pipeline & copy page below code into js file,

 /* I am reading the original header named "relation" using below code */
 var relationString = context.getVariable("request.header.relation");
 /* Maps which converts string to integer that will be sent to target server */
 var relationMaps = {
     "Self" : 1,
     "Spouse" : 2,
     "Child" : 3,
     "Parent" : 4,
     "Sibling" : 5,
 };
 
 var relationId = 0;
 if (relationMaps[relationString]) {
     /* found map */
     relationId = relationMaps[relationString];
 } else {
     /* not found */
     print ("not found");
 }
 
  /* replace the header named "relation" */
 
 context.setVariable("request.header.relation", relationId);

Please find sample proxy that demonstrates same below,

replaceconditional-rev2-2016-07-21.zip

See the sample API call,

curl -X GET -H "relation: Spouse" -H "Cache-Control: no-cache" -H "Postman-Token: 9d6b70f5-6503-6af4-5382-28094e5b10b4" "http://anildevportal-test.apigee.net/replaceconditional"

Endpoint if you would like to play , http://anildevportal-test.apigee.net/replaceconditional

3212-szoter-image.png

Since you said you are new to Apigee, I would suggest to go through academy site, http://academy.apigee.com/courses/elearning , Specially foundational training videos. Also, check out Apigee 4MV4D in youtube.

Hope it helps. Keep us posted.