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


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.

Example of a JavaScript class for access to the JS7 REST Web Service API
class JS7RestApi {

    _step = null;
    _accessToken = null;
    _apiExecutor = null;

    constructor(step) {
    	this._step = step;
        
        try {
            this._apiExecutor = new com.sos.js7.job.jocapi.ApiExecutor(this._step.getLogger());
            this._accessToken = this._apiExecutor.login().getAccessToken();
        } catch (e) {
            if (this._apiExecutor) {
                if (this._accessToken) {
			        this._apiExecutor.logout(this._accessToken);
                }

		        this._apiExecutor.close();
            }

            throw e;
		}
    }

	destructor() {
        if (this._apiExecutor) {    
            if (this._accessToken) {
			    this._apiExecutor.logout(this._accessToken);
            }

		    this,_apiExecutor.close();
        }
    }

    post(path, body) {
		var response = this._apiExecutor.post(this._accessToken, path, body);
        this._step.getLogger().debug('[post: response.getStatusCode] ' + response.getStatusCode());
        this._step.getLogger().debug('[post: response.getResponseBody] ' + response.getResponseBody());
        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


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

        let api = new JS7RestApi(js7Step);

        var response = api.post('/joc/api/authentication/joc_cockpit_permissions');
        	js7Step.getLogger().info('response body: ' + response.getResponseBody());
        	js7Step.getLogger().info('response status code: ' + response.getStatusCode());
       
        const permissions = JSON.parse(response.getResponseBody());
	        js7Step.getLogger().info('permissions: ' + permissions.joc.getLog);
       
        api = null;
	}
}


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