Make multiple management API calls

Have a csv file(1000 entries) with app details like product, developer, custom attributes. 

Need to convert that csv file to json

Then iterate over the json and Build a POST call to management API by building the url with proper headers and payload from the json array. This has to be done for each json entry.

Can we achieve this with javascript policy? If not, please suggest how can we handle this?

0 4 209
4 REPLIES 4

Can we achieve this with javascript policy?

No

You could use a JS policy to invoke 2 entries, or to send 3 or 4, maybe up to 10 requests. But the JS policy is designed to run inline w.r.t. an HTTP request. The JS policy will timeout after 200ms or perhaps more if you configure it so. But possibly a maximum of 2000ms. That's not a large enough time box to make 1000 invocations and receive 1000 responses. Neither the JS policy itself nor an Apigee API proxy in general is designed to host long-running transaction sessions like this. An Apigee proxy is not the right "container" for such logic.

What are you really trying to do? I understand you want to invoke a management API. But why? Tell me more. What's the purpose of invoking the management API 1000 times with 1000 different requests? How often do you need to invoke these 1000 transactions? Every hour?  Once a day?  Is this session occurring based on an inbound request, or is it triggered by a timer? What happens after you invoke these transactions? 

We have Api name, key, rate limit details in csv as of now.  Want to explore how can these details can be entered in to apigee as part of migration task. It's a one time process.

oh I see.

In that case, I advise you strongly to not try to build that into an API Proxy.  If it's a one-time thing, I would want to build a script that does the work. Just run it from your workstation terminal. 

For scripting these kinds of things, you can choose whatever you like: Powershell, nodejs, Python, and I suppose you could use bash too.  

I am most familiar with nodejs, and find it to be a good tool for scripting automated tasks. 

There is a library that can help - apigee-edge-js.  It's a npm module that provides an interface for connecting to an Apigee organization and invoking the management API.  It uses JavaScript promises, and if you understand that, then the programming model is pretty easy to use, I think. Here's an example for how you would create a new developer: 

 

const apigeejs = require('apigee-edge-js'),
      apigee = apigeejs.apigee;

const options = {
    org : config.org,
    user: config.username,
    password: config.password
  };

apigee.connect(options)
  .then ( org => {
    const options = {
          developerEmail : "JDimaggio@example.org",
          lastName : "Dimaggio",
          firstName : "Josephine",
          userName : "JD1"
        };

    return org.developers.create(options)
      .then( result => console.log('ok. developer: ' + JSON.stringify(result)) )
  })
  .catch ( error => console.log('error: ' + error) );

 

The library exposes collections that support CRUD operations on developers, products, developerapps, appcredentials, and so on. Lots of examples available too. And here's a screencast that gives an overview.

If I wanted to build a script to perform 1000 steps, then I would :

  • start with the code above, but build in a loop
  • build a little logic to read in the CSV file
  • build in some error checking in case the script fails, and I need to re-run it. 
  • build in some logging, so I can monitor progress

If you don't know JavaScript, that might sound complicated, but it's pretty straightforward for an experienced scripter.  You could do the same thing in Python, too. 

Thank you@dchiesa1 , I'll try this solution and update.