Page History
...
In order to reduce the overhead of REST client implementation a JavaScript class is suggested that can be used by any number of JavaScript for simplified access to the JS7 REST Web Service by jobs.
JavaScript Class
A JavaScript class is suggested that
- connects to JOC Cockpit ,performs login and logout to/from JOC Cockpit from the methodsand performs sign-in to JOC Cockpit using
let api = new JS7RestApi(step)
- performs calls to the JS7 REST Web Service API from the method
post(path, body)
...
Find the source code of the 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 JOC Cockpit URL, user account, password or certificate are used from the JS7 Agent's .
- The class automatically signs off from JOC Cockpit by use of its destructor.
Code Block | ||||||||
---|---|---|---|---|---|---|---|---|
| ||||||||
constclass url = require('url'); const http = require('http'); const Buffer = require('Buffer'); class JS7RestApi { JS7RestApi { _step = null; _accessToken = ''null; constructor(url, controllerId) {_apiExecutor = null; this.url = url.parse(url);constructor(step) { this.controllerId_step = controllerIdstep; } /** * Function to sign in with user credentials * @param {string} user - User Account try { * @param {string} password - Password this._apiExecutor = */ signIn(user, password) { const authString = `${user}:${password}`; new com.sos.js7.job.jocapi.ApiExecutor(this._step.getLogger()); const base64AuthStringthis._accessToken = Buffer.from(authString, 'utf-8').toString('base64'this._apiExecutor.login().getAccessToken(); const path = '/joc/api/authentication/login'; const headers = { 'Authorization': 'Basic ' + base64AuthString }; this.post(path, headers); } /** catch (e) { * Function to sign out */ signOut() { const path = '/joc/api/authentication/logout';if (this._apiExecutor) { let headers; if (this._accessToken !== '') { headers = { 'X-Access-Token': this.accessTokenthis._apiExecutor.logout(this._accessToken); }; } this.post(path, headers_apiExecutor.close(); } } /** * Function to retrieve JOC Cockpit permissionsthrow e; } */ getJocCockpitPermissions} destructor() { const path = '/joc/api/authentication/joc_cockpit_permissions'; if (this._apiExecutor) { let headers; if (this._accessToken !== '') { headers = { 'X-Access-Token': this.accessTokenthis._apiExecutor.logout(this._accessToken); }; this,_apiExecutor.postclose(path, headers); } } /** * Function to send a POST request to the API * @param {string} path - API path * @param {object} headers - Request headers */ post(path, headersbody) { 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 = '' var response = this._apiExecutor.post(this._accessToken, path, body); res.on('data', (chunk) => { data += chunk; }); js7Stepthis._step.getLogger().infodebug('.. http status code:[post: response.getStatusCode] ' + resresponse.statusCodegetStatusCode()); res.on('end', () => { js7Stepthis._step.getLogger().infodebug(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'[post: response.getResponseBody] ' + response.getResponseBody()); }); req.end(); return response; } } |
JavaScript Job
A JavaScript job can make use of the JavaScript class provided by the Script Include:
- The following syntax makes the JavaScript class available from a Script Include available:
//!include JavaScript-JS7RestApi
- The JavaScript class can be instantiated from the job like this:
let api = new JS7RestApi(url, controllerId);js7Step)
- For use of the
js7Step
object see JS7 - Job API.
Download JavaScript job from Workflow (upload .json): pdJavaScriptJS7RestAPI.workflow.json
...
- 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.
Further Resources
Overview
Content Tools