You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 4 Next »

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


Find the source code of the JavaScript class:

Example of a JavaScript class for access to the JS7 REST Web Service API
const url = require('url');
const http = require('http');
const Buffer = require('Buffer');

class JS7RestApi {

    accessToken = '';

    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
     */
    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
        };

        this.post(path, headers);
    }

    /**
     * Function to sign out
     */
    signOut() {
        const path = '/joc/api/authentication/logout';
        let headers;
        if (this.accessToken !== '') {
            headers = {
                'X-Access-Token': this.accessToken
            };
            this.post(path, headers);
        }
    }

    /**
     * Function to retrieve JOC Cockpit permissions
     */
    getJocCockpitPermissions() {
        const path = '/joc/api/authentication/joc_cockpit_permissions';
        let headers;
        if (this.accessToken !== '') {
            headers = {
                'X-Access-Token': this.accessToken
            };
            this.post(path, headers);
        }
    }

    /**
     * Function to send a POST request to the API
     * @param {string} path - API path
     * @param {object} headers - Request headers
     */
    post(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;
            });
            js7Step.getLogger().info('.. http status code: ' + res.statusCode);

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

        req.on('error', (error) => {
            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


Find the source code of the job script:

Example of job script using JavaScript class to access the JS7 REST Web Service API
//!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);

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

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

        // call the signOut function to log out and to invalidate the access token
        api.signOut();
	}
}




  • No labels