API Creation by NodeJS

You can configure node applications to integrate into command lines.
lac31
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 build a node application you can invoke from the command line.
An SDK is provided for Node.js programs.
In this article:
The CA Live API Creator Admin project API (Admin API) includes the 
ProjectExport
 resource. This resource returns the JSON representation of an entire API. You can GET the export JSON from one API Server and POST the JSON to another, with a node application that does the following:
  1. Obtains the export JSON from the development API Server by calling 
    getApiExport
    .
  2. Inserts this JSON to the production API Server by calling 
    postApiImport
    .
The 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.
  • apiDev
     and 
    apiProduction
     have the same value, which is not normally the case.
  • The URL contains 
    abl/admin/v2
    . This designates that the API is using the admin data that is part of the abl internal system account.
  • 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 fixing imported APIs, see API Project Samples.
  • The code emits the following log:
    ..getApiExport reading resource ProjectExport, with filter: equal(name:'Northwind-B2B' )
    Execution Complete
    ..getApiExport finds export: Northwind-B2B
    ....postApiImport - Posting anApiExport into ProjectExport
    ....postApiImport - SUCCESS
    Observe that the 
    Execution Complete
     message occurs early, reflecting the JavaScript's common aync 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 installed 
Node.js
.
For more information about how to install Node.js, see Install Node.js.
Run the Example
  1. Install the Node SDK.For more information about how to install Node SDK, see Use Node SDK.
  2. Create the Business to Business (B2B) sample in the self-contained, single-user version of 
    CA Live API Creator
     based on Jetty.
    For more information about how to install the B2B sample, see Install B2B.
  3. Create a new folder and a file in it called 
    exportImport.js
     with 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 resource ProjectExport */ function postApiImport(anApiExport) { var parms = { ProjectImportInProgress : true // required to renumber internal IDs }; // console.log(anApiExport); // lots of output console.log("..postApiImport - Posting anApiExport into ProjectExport"); var exportEP = apiServerProduction.endpoint('ProjectExport'); return exportEP.post(anApiExport, parms). then( function(postResponse) { var responseCode = postResponse.statusCode; if (responseCode != 201) { console.log("..postApiImport FAILS, bad responseCode: " + responseCode); e = new Error(); console.log(e.stack); throw new Error("..postApiImport FAILS, with responseCode: " + responseCode); } var txSummary = postResponse.txsummary; // array of posted rows, for (var eachRowNum = 0; eachRowNum < txSummary.length; eachRowNum++) { var eachUpdatedRow = txSummary[eachRowNum]; var metaData = eachUpdatedRow['@metadata']; // [email protected] is bad syntax var rowType = metaData.resource; if (rowType == 'ProjectExport') { apiID = eachUpdatedRow.ident; console.log('FOUND inserted apiID, ident: ' + apiID + ', ==>'); console.log(eachUpdatedRow); return new Promise(function(resolve, reject) { resolve(apiID); }); } } }, function(postStatus) { console.log("..postApiImport Post FAILS, with postStatus:" + postStatus); e = new Error(); console.log(e.stack); throw new Error("..postApiImport Post FAILS, with postStatus: " + postStatus); } ); } /** * 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 = { sysfilter : "equal(name:'" + apiName + "' )" }; // e.g., equal(name: 'Demo') var exportEP = apiServerDev.endpoint('ProjectExport'); exportEP.get(filter).then(function(theExport) { // get returns promise - the export (big JSON) 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');
  4. Issue the following command from your command line:
    npm install underscore
    Run it by issuing the following command:
    This command might require some 
    npm
     install steps.
    node exportImport.js