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. When used by jobs then the jobs are required to implement a REST client implementing the functionality to login and to logout from JOC Cockpit.

In order to reduce the overhead of REST client implementation a JavaScript class is suggested that can be used by any number of JavaScript jobs.

JavaScript Class

A JavaScript class is suggested that

  • connects to JOC Cockpit,
  • performs login and logout to/from JOC Cockpit from the methods
    • signIn( user, password )
    • signOut()
  • performs arbitrary calls to the REST Web Service API from the method
    • post( path, headers )

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

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

Image Added


Find the source code of the JavaScript class:

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

letconst Buffer = require('Buffer');

class JS7RestApi {

    accessToken = '';
const hostname = 'localhost';
const port = 4446;



    constructor(url, controllerId) {
        this.url = url.parse(url);
        this.controllerId = controllerId;
    }

    /**
     * Function to sign in with user credentials
     * @param {string} user - User Account
     * @param {string} password - Password
     */
function    signIn(user, password) {
        const authString = `${user}:${password}`;
        const base64AuthString = Buffer.from(authString, 'utf-8').toString('base64');
        const path = '/joc/api/authentication/login';
        const headers = {
            'Authorization': 'Basic ' + base64AuthString
        };

    postApi    this.post(path, headers);
    }

    /**
     * Function to retrieve JOC Cockpit permissions
 * @param {string} token - Access token
sign out
     */
function    getJocCockpitPermissionssignOut(token) {
        const path = '/joc/api/authentication/joc_cockpit_permissionslogout';
        let headers;
        if (tokenthis.accessToken !== '') {
            headers = {
                'X-Access-Token': token
this.accessToken
            };
         postApi   this.post(path, headers);
        }
    }

    /**
     * Function to signretrieve out
JOC * @param {string} token - Access token
Cockpit permissions
     */
function    signOutgetJocCockpitPermissions(token) {
        const path = '/joc/api/authentication/logoutjoc_cockpit_permissions';
        let headers;
        if (tokenthis.accessToken !== '') {
            headers = {
                'X-Access-Token': token
this.accessToken
            };
          postApi  this.post(path, headers);
        }
    }

    /**
     * Function to send a POST request to the API
     * @param {string} path - API path
     * @param {object} headers - Request headers
     */
function    postApipost(path, headers) {
        const options = {
            hostname: this.url.hostname,
            port: this.url.port,
            protocol: this.url.protocol,
            path: path,
            method: 'POST',
            headers: headers
        };

        const req = http.request(options, (res) => {
            let data = '';
            res.on('data', (chunk) => {
                data += chunk;
            });
        console.log(    js7Step.getLogger().info('.. http status code: ' + res.statusCode);

            res.on('end', () => {
                console.logjs7Step.getLogger().info(data, 'dta');
                let response = JSON.parse(data);
                if (response.accessToken && response.accessToken !== undefined) {
                    this.accessToken = response.accessToken;
                getJocCockpitPermissions(accessToken);}
            });
        });

    });

    req.on('error', (error) => {
        console    js7Step.getLogger().error(error);
        });

        req.end();
    }
}

JavaScript Job

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

  • The following syntax makes the Script Include available:
    • //!include JavaScript-JS7RestApi
  • The JavaScript class can be instantiated from the job like this:
    • let api = new JS7RestApi(url, controllerId);

Download 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) {

       	const url = 'http://localhost:4446';
        const controllerId = 'controller';
        const account = 'root';
        const password = 'root';

		let api = new JS7RestApi(url, controllerId);

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

        // get curent account's permissions for JOC Cockpit operations
        api.getJocCockpitPermissions();

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