How to retrieve the content of resource files defined at env level ( JavaScript )

Hi, 

I have a resource file of type Javascript, defined at the environment level. I want to create a shared flow which will be capable of reading this resource file (which contains a configuration) and process it using some javascript code. How can I do that?

My shared flow contains a javascript policy with the logic of how to read an interpret the data from that resource file. But how I can "include" that in the shared flow?

The final aim is to have that shared flow deployed on various Apigee environments, and depending on the resource file, the behaviour of shared flow to be different. 

Thank you.

Solved Solved
0 3 966
1 ACCEPTED SOLUTION

I think you can do something like this: 

<AssignMessage name='AM-get-resource'>
  <AssignVariable>
    <Name>assigned-variable</Name>
    <ResourceURL>jsc://resourcename.json</ResourceURL>
  </AssignVariable>
</AssignMessage>

This will embed the content of the resource into the assigned-variable variable. And the resource can be resolved from environment scope. I believe the format of the resource file can be anything. Text, json, whatever you like. 

Try it and see. 

If that does not work, you can also do this in JS: 

<Javascript name='JS-read-settings' timeLimit='200' >
  <IncludeURL>jsc://settings.js</IncludeURL>
  <ResourceURL>jsc://readSettings.js</ResourceURL>
</Javascript>

But in this case the "settings.js" file must be valid JavaScript.  So you'd have to encode your settings as JavaScript (not json!)

View solution in original post

3 REPLIES 3

I think you can do something like this: 

<AssignMessage name='AM-get-resource'>
  <AssignVariable>
    <Name>assigned-variable</Name>
    <ResourceURL>jsc://resourcename.json</ResourceURL>
  </AssignVariable>
</AssignMessage>

This will embed the content of the resource into the assigned-variable variable. And the resource can be resolved from environment scope. I believe the format of the resource file can be anything. Text, json, whatever you like. 

Try it and see. 

If that does not work, you can also do this in JS: 

<Javascript name='JS-read-settings' timeLimit='200' >
  <IncludeURL>jsc://settings.js</IncludeURL>
  <ResourceURL>jsc://readSettings.js</ResourceURL>
</Javascript>

But in this case the "settings.js" file must be valid JavaScript.  So you'd have to encode your settings as JavaScript (not json!)

I used the first approach 

<AssignMessage name='AM-get-resource'>
  <AssignVariable>
    <Name>configStr</Name>
    <ResourceURL>jsc://resourcename.json</ResourceURL>
  </AssignVariable>
</AssignMessage>

and the variable configStr is storing a text string. I used JSON.parse to obtain the JSON object. 

configuration = JSON.parse(configStr)

 

I think in JavaScript you would have to do this:

 

// 1. import the context variable that was set by AssignMessage into JavaScript
var configStr = context.getVariable('configStr');

// 2. parse the JSON
var configuration = JSON.parse(configStr);