Versions Compared

Key

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

...

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

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

...

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 let urlapi = 'http://localhost:4446';
 new JS7RestApi(js7Step);

        constvar controllerIdresponse = 'controller';
api.post('/joc/api/authentication/joc_cockpit_permissions');
         const account = 'root'	js7Step.getLogger().info('response body: ' + response.getResponseBody());
        const password = 'root';

		let api = new JS7RestApi(url, controllerId);
	js7Step.getLogger().info('response status code: ' + response.getStatusCode());
       
 // call the signIn method with the providedconst userpermissions account and password
        api.signIn(account, password= JSON.parse(response.getResponseBody());

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

js7Step.getLogger().info('permissions: ' + permissions.joc.getLog);
        //
 call the signOut method to log out andapi to invalidate the access token
        api.signOut()= null;
	}
}


Explanation:

  • Line 1: references the Script Include to mail the REST API JavaScript class available.
  • Line 3,5: implement the JavaScript job
  • Line 7: creates an instance of the REST API 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: 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.