Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Table of Contents

Introduction

The JS7 - REST Web Service API allows to perform any sort of operation on the lifecycle of scheduling objects such as creating, deploying, running, stopping workflows and jobs.

The REST Web Service API is available from JOC Cockpit. Use of the REST Web Service API requires jobs to implement a REST Client for the functionality to login and to logout from JOC Cockpit and any other calls to the API.

In order to reduce the overhead of REST Client implementation a JavaScript class is suggested for simplified access to the JS7 REST Web Service by JS7 - JavaScript Jobs.

REST Client JavaScript Class

A JavaScript class is suggested that

  • connects to JOC Cockpit and performs login to JOC Cockpit for access to the JS7 REST Web Service API using
    • let api = new JS7RestApi(step)
  • performs calls to the JS7 REST Web Service API from the method
    • post(path, body)

For re-usability users can add the JavaScript class to a JS7 - Script Include that can be used by any number of jobs. Changes to the Script Include will be reflected with the next deployment of a workflow.

Download JavaScript class from Script Include (upload .json): JavaScript-JS7RestApi.includescript.json

Image Added


Find the source code of the REST Client JavaScript class:

  • The JavaScript class makes use of the Java ApiExecutor class that ships with JS7 Agents.
  • The class automatically signs in to JOC Cockpit from its constructor.
    • The JOC Cockpit URL, user account, password or certificate are used from the JS7 Agent's ./config/private/private.conf file.
    • For details see JS7 - JITL Common Authentication
  • The class automatically signs off from JOC Cockpit by use of its destructor.

Code Block
languagejs
titleExample of a JavaScript class for access to the JS7 REST Web Service API
linenumberstrue
collapsetrue
constclass http = require('http');

let accessTokenJS7RestApi {

    _step = ''null;
const hostname = 'localhost';
const port _accessToken = 4446null;

/**
 * Function to sign_apiExecutor in with user credentials
 * @param {string} user - User Account
 * @param {string} password - Password
 */
function signIn(user, password= null;

    constructor(step) {
    const authString	this._step = `${user}:${password}`step;
    const base64AuthString = Buffer.from(authString, 'utf-8').toString('base64');
    const path = '/joc/api/authentication/login';
    const headers =  try {
        'Authorization': 'Basic ' + base64AuthString
    };

    postApi(path, headers);
}

/**
 * Function to retrieve JOC Cockpit permissions
 * @param {string} token - Access token
 */
function getJocCockpitPermissions(token) {
    const path = '/joc/api/authentication/joc_cockpit_permissions' this._apiExecutor = new com.sos.js7.job.jocapi.ApiExecutor(this._step.getLogger());
            this._accessToken = this._apiExecutor.login().getAccessToken();
    let headers;
   } ifcatch (token !== ''e) {
        headers  = {
 if (this._apiExecutor) {
         'X-Access-Token': token
      if  };
(this._accessToken) {
			        postApi(path, headersthis._apiExecutor.logout(this._accessToken);
    }
}

/**
 * Function to sign out
 * @param {string} token - Access token}
 */
function signOut(token) {
		      const path = '/joc/api/authentication/logout' this._apiExecutor.close();
    let headers;
    if (token !== '') {
        headers = { }

            'X-Access-Token': token
  throw e;
		}
      };
        postApi(path, headers);

	destructor() {
    }
}

/**
 * Function to send a POST request to the API
 * @param {string} path - API path
 * @param {object} headers - Request headers
 */
function postApi(path, headers) {
    const options = {if (this._apiExecutor) {    
            if (this._accessToken) {
			    this._apiExecutor.logout(this._accessToken);
        hostname: hostname,
        port: port,
        path: path,
}

		        method: 'POST',this,_apiExecutor.close();
        headers: headers}
    };

    const req = http.request(options, (res) => {
        let data = ''post(path, body) {
		var response = this._apiExecutor.post(this._accessToken, path, body);
        res.on('data', (chunk) => {this._step.getLogger().debug('[post: response.getStatusCode] ' + response.getStatusCode());
        this._step.getLogger().debug('[post: response.getResponseBody] '  data += chunk response.getResponseBody());
        });
        console.log(res.statusCode);

        res.on('end', () => {
return response;
	}
} 

JavaScript Job

A JavaScript job can make use of the REST Client JavaScript class provided by the Script Include:

  • The following syntax makes the JavaScript class available from a Script Include:
    • //!include JavaScript-JS7RestApi
  • The JavaScript class can be instantiated by the job like this:
    • let api = new JS7RestApi(js7Step)
    • For use of the js7Step object see JS7 - Job API.

Download sample JavaScript job from Workflow (upload .json): pdJavaScriptJS7RestAPI.workflow.json

Image Added


Find the source code of the job script:

Code Block
languagejs
titleExample of job script using JavaScript class to access the JS7 REST Web Service API
linenumberstrue
//!include JavaScript-JS7RestApi

class JS7Job extends js7.Job {

     processOrder(js7Step) {

        let api = new console.log(data, 'dta'JS7RestApi(js7Step);

        var    let response = JSONapi.parse(datapost('/joc/api/authentication/joc_cockpit_permissions');
            if (response.accessToken && response.accessToken !== undefined) {
                accessToken = response.accessToken	js7Step.getLogger().info('response body: ' + response.getResponseBody());
        	js7Step.getLogger().info('response status code: '     getJocCockpitPermissions(accessToken+ response.getStatusCode());
       
     }
   const permissions    }= JSON.parse(response.getResponseBody());
	    });

    req.onjs7Step.getLogger().info('error', (error) => {
permissions: ' + permissions.joc.getLog);
       
   console.error(error);
    });

 api   req.end()= null;
}

// Call the signIn function with the provided user account and password
signIn('root', 'root');

// Call the signOut function to log out and to invalidate the access token
if (accessToken) {
	signOut(accessToken);
}	}
}


Explanation:

  • Line 1: references the Script Include to make the REST API JavaScript class available.
  • Line 3,5: implement the JavaScript job.
  • Line 7: creates an instance of the REST Client JavaScript class that implicitly signs in to JOC Cockpit.
  • Line 9: executes a call to the JS7 REST Web Service that returns the current user's permissions in JOC Cockpit from the REST response.
  • Line 10,11: include examples for use of methods to retrieve the response body and HTTP status code.
  • Line 13,14: parses the JSON response body to access properties in the response.
  • Line 16: destructs the instance and implicitly signs out from JOC Cockpit.

Further Resources