API Creation by NodeJS

API Creation by NodeJS
Building node applications can be useful for scripting operations. You can configure node applications to integrate into command lines. For example, a common operation is to move a system from development to production. Use the information in this article to create your API using the 
 Node SDK npm package. This article includes information about how to build a node application that you can invoke from the command line.
In this article:
The CA Live API Creator Admin project API (Admin API) runs with the self-contained, single-user version of 
CA Live API Creator
 based on Jetty and the Business to Business (B2B) sample. This API includes the
 endpoint, which returns the JSON representation of an entire API. You can GET the JSON file export from one API Server and POST the JSON to another using the
 Node SDK npm package. This Node SDK does the following:
  1. Obtains the export JSON from the development API Server by calling the
     endpoint. The following code block shows an example of exporting the
    B2B Northwind
    API by calling the 
  2. Inserts this JSON to the production API Server by calling the
    endpoint. The following shows an example of importing the
    B2B Northwind
    API with the option of replacing the existing API if API name or URL fragment match an API in the production API Server:
  • apiDev
    have the same value, which is not normally the case.
  • The URL contains
    . This designates that the API is using the admin data that is part of the
    internal system TeamSpace.
  • id fixup (see code comments) ensures that internal admin IDs do not conflict with existing admin data during import.
  • This example is a basic illustration. For real use, consider extending it to:
    • Provide argument input for dev/production servers.
    • Fix the passwords. For more information about how to fix imported APIs, see API Samples.
  • The code emits the following log:
    ..getApiExport using @export, with urlfrgament b2derbybnw
    Execution Complete
    ..getApiExport finds and exported: Northwind-B2B
    ....postApiImport - Posting anApiExport using @import
    ....postApiImport - SUCCESS
    Execution Complete
     message occurs early, reflecting the JavaScript's common async execution model. JavaScript for logic running in the API Server operates synchronously, providing a simpler programming model.
Verify the Prerequisites
Before you create your API using Node.js, verify that you have completed the following:
  • You have installed Node.js.
  • You have installed the
    Node SDK npm package.
For more information about how to install Node.js and the 
 Node SDK npm package, see Install NodeJS ande Command Line Utilities.
Run the Example
  1. Create a folder, and then create the 
     file in it that includes the following code:
    var apicreator = require('caliveapicreator'); // http://ca-doc.espressologic.com/docs/live-api/node-sdk var _ = require('underscore'); var fs = require('fs'); // TODO, make these args, or alter the assignments as appropriate // normally these are different servers (this just clones an API within 1 ApiServer) var apiServerDev = apicreator.connect('http://localhost:8080/rest/abl/admin/v2', 'admin', 'Password1'); var apiServerProduction = apicreator.connect('http://localhost:8080/rest/abl/admin/v2', 'admin', 'Password1'); var accountName = 'default account'; var apiName = 'Demo'; var dataSource_names = ['Demo', 'Sample']; // use API Creator to determine which properties apply var dataSource_urls = ['jdbc:derby:LogicDemo$$', 'jdbc:derby:LogicSample$$']; var dataSource_passwords = ['']; var dataSource_catalog_names = ['']; var dataSource_user_names = ['']; var dataSource_schema_names = ['']; var authProviderName = 'myAuthProvider'; /** * post anApiExport (big json export) into the @import endpoint */ function postApiImport(anApiExport) { var anApiID; console.log("..postApiImport - Post using @import"); var importEP = apiServerProduction.endpoint('@import?namecollision=rename_new'); return importEP.post(anApiExport). then( function(postResponse) { console.log(postResponse); if (postResponse.result === 'OK') { var txSummary = postResponse.imported; for (var eachRowNum = 0; eachRowNum < txSummary.length; eachRowNum++) { var eachdRow = txSummary[eachRowNum]; console.log("projectName:" + eachRow.projectName); console.log("projectUrlFragment: "+ eachRow.projectUrlFragment); console.log("projectIdent:" +eachRow.projectIdent); anApiID = eachRow.projectIdent; } return anApiID; } else { console.log("..postApiImport FAILS, bad responseCode: " + postResponse); e = new Error(); console.log(e.stack); throw new Error("..postApiImport FAILS, with responseCode: " + postResponse); } } ); } /** * get dataSources for anApiID */ function getDataSources(anApiID) { console.log('......getDataSources for anApiID: ' + anApiID); var exportEP = apiServerProduction.endpoint('dbaseschemas'); var filter = "sysfilter=equal(project_ident: " + anApiID + ")"; return exportEP.get(filter). then(function(aDataSourceList) { console.log("....getDataSources - returning aDataSourceList"); // console.log(aDataSource); return new Promise(function(resolve, reject) { resolve(aDataSourceList); }); }, function(getStatus) { var msg = "....getDataSources FAILS, getStatus: " + getStatus; console.log(msg); e = new Error(); console.log(e.stack); throw new Error(msg); } ); } /** * update the dataSrcs with a pwd etc (settings from top, or arguments) */ function putDataSources(aDataSourceList) { console.log("..putDataSources - update the dataSrcs with a pwd etc ==>"); // console.log(aDataSourceList); for (var eachDataSrcNum = 0; eachDataSrcNum < aDataSourceList.length; eachDataSrcNum++) { var eachDataSrc = aDataSourceList[eachDataSrcNum]; var eachDataSrcName = eachDataSrc.name; var fixNum = dataSource_names.indexOf(eachDataSrcName); if (fixNum < 0) { console.log('WARNING - unable to find/fix dataSrc in fix list: ' + eachDataSrcName); } else { eachDataSrc.url = dataSource_urls[fixNum]; // TODO - assign other required elements } } console.log("..putDataSources - fixed up ==>"); console.log(aDataSourceList); var exportEP = apiServerProduction.endpoint('dbaseschemas'); return exportEP.put(aDataSourceList). then(function(putResponse) { var responseCode = putResponse.statusCode; if (responseCode != 200) { console.log("..putDataSources FAILS, bad responseCode: " + responseCode); e = new Error(); console.log(e.stack); throw new Error("..putDataSources FAILS, with responseCode: " + responseCode); } return new Promise(function(resolve, reject) { resolve(apiID); }); }, function(postStatus) { console.log("..putDataSource Post FAILS, with postStatus:" + postStatus); e = new Error(); console.log(e.stack); throw new Error("..putDataSource Post FAILS, with postStatus: " + postStatus); } ); } // ************************** MAIN ******************************** var apiID = 0; var filter = {}; var exportEP = apiServerDev.endpoint('@export?urlfragment=demo'); exportEP.get().then(function(theExport) { return(theExport); }).then( function(anExport) { return postApiImport(anExport); // return promise - the ApiID }).then( function(anApiID) { return getDataSources(anApiID); // return promise - dataSourceList }).then( function(aDataSourceList) { return putDataSources(aDataSourceList); }).then( function(arg) { console.log('** Export/Import complete **'); }); console.log('Main Thread - Execution Complete');
  2. Issue the following command from your command line:
    npm install underscore
    Run the example by issuing the following command:
    node exportImport.js