Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
dimitri-koenig committed Jun 28, 2024
1 parent 4ffe637 commit ce36304
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 169 deletions.
169 changes: 28 additions & 141 deletions lib/simple-vertec-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ xmlDigesterLogger.level(xmlDigesterLogger.WARN_LEVEL);
* Credentials should be out of export scope
*/
let vertecXmlUrl = null;
let vertecAuthUrl = null;
let vertecUsername = null;
let vertecPassword = null;
let vertecApiKey = null;
let vertecSessionForRequest = 1;
let vertecMaxSessionsForRequests = 8;
Expand All @@ -29,40 +26,24 @@ let vertecRequestCount = 0;
* Simple Vertec Api
*
* @param {string} xmlUrl Your Vertec host url
* @param {string} authUrl Your Vertec auth url
* @param {string} username Your Vertec username
* @param {string} password Your Vertec password
* @param {boolean} verbose set true for some debugging data
* @param {string} apiKey Your Vertec api key
* @param {boolean} verbose set true for some debugging data
* @param {object|null} defaultRequestOptions Default options for request
*
* @return {object} SimpleVertecApi
*/
export default class SimpleVertecApi {
constructor() {
if (arguments.length > 4 || typeof arguments[2] === 'string') {
vertecXmlUrl = arguments[0];
vertecAuthUrl = arguments[1];
vertecUsername = arguments[2];
vertecPassword = arguments[3];

this.verbose = arguments[4] || false;
this.defaultRequestOptions = arguments[5] || {};
} else {
vertecXmlUrl = arguments[0];
vertecApiKey = arguments[1];

this.verbose = arguments[2] || false;
this.defaultRequestOptions = arguments[3] || {};
}
constructor(xmlUrl, apiKey, verbose = false, defaultRequestOptions = {}) {
vertecXmlUrl = xmlUrl;
vertecApiKey = apiKey;

this.verbose = verbose;
this.defaultRequestOptions = defaultRequestOptions;

this.authTokenPromise = null;
this.storedPromises = {};

// start own garbage collector for stored promises
setInterval(this.gcPromises.bind(this), 15 * 1000);

// workaround: didn't found a nice way for checking and resetting auth tokens, yet, so reset auth token every 3min
setInterval(this.resetAuthToken.bind(this), 3 * 60 * 1000);
}

/**
Expand All @@ -80,17 +61,6 @@ export default class SimpleVertecApi {
});
}

/**
* Sets authTokenPromise to null so that on the next request it will be fetched again
*
* @private
*
* @return {void}
*/
resetAuthToken() {
this.authTokenPromise = null;
}

/**
* Select for fetching data
*
Expand All @@ -101,8 +71,9 @@ export default class SimpleVertecApi {
* @return {Promise} Promise for the request
*/
select() {
return this.buildSelectString(...arguments)
.then(xmlString => this.doStoredRequest(xmlString));
let xmlString = this.buildSelectString(...arguments);

return this.doStoredRequest(xmlString);
}

/**
Expand Down Expand Up @@ -168,9 +139,9 @@ export default class SimpleVertecApi {
}

let deleteObject = this.buildDeleteBody(ids);
let xmlString = this.buildXml(deleteObject);

return this.buildXml(deleteObject)
.then(xmlString => this.doStoredRequest(xmlString));
return this.doStoredRequest(xmlString);
}

/**
Expand Down Expand Up @@ -213,9 +184,9 @@ export default class SimpleVertecApi {
});

let saveObject = this.buildSaveBody(createObjects, updateObjects);
let xmlString = this.buildXml(saveObject);

return this.buildXml(saveObject)
.then(xmlString => this.doStoredRequest(xmlString));
return this.doStoredRequest(xmlString);
}

/**
Expand Down Expand Up @@ -308,41 +279,18 @@ export default class SimpleVertecApi {
*
* @param {object} body The body object
*
* @return {Promise} Promise for the XML String
* @return {string} Final XML String
*/
buildXml(body) {
return this.buildXmlHeader()
.then(header => this.buildXmlFromHeaderAndBody(header, body));
}

/**
* Builds the xml request string out of the header and body object
*
* @private
*
* @param {object} header The header object
* @param {object} body The body object
*
* @return {string} The built XML String
*/
buildXmlFromHeaderAndBody(header, body) {
let contentObject = {
Envelope: {
Header: {},
Body: {}
}
};

_.extend(contentObject.Envelope.Header, header);
_.extend(contentObject.Envelope.Body, body);

if (vertecApiKey) {
delete contentObject.Envelope.Header;
}

let xmlString = this.buildXmlStringFromObject(contentObject);

return xmlString;
return this.buildXmlStringFromObject(contentObject);
}

/**
Expand All @@ -358,55 +306,6 @@ export default class SimpleVertecApi {
return SimpleXmlConverter.toXml(obj, 4);
}

/**
* Builds the auth part of the XML Request
*
* @private
*
* @return {Promise} Promise for the auth part of the XML request
*/
buildXmlHeader() {
return this.getAuthToken()
.then(authToken => ({BasicAuth: {Token: authToken}}));
}

/**
* Gets the auth token from Vertec
*
* @private
*
* @return {Promise} Promise for the auth token
*/
getAuthToken() {
if (vertecApiKey) {
return q.resolve(vertecApiKey);
}

if (this.authTokenPromise) {
return this.authTokenPromise;
}

const client = axios.create({
timeout: 1000 * 10,
});

axiosRetry(client, {
retries: 5,
retryDelay: () => {
return 2000;
},
});

this.authTokenPromise = client
.post(vertecAuthUrl, `vertec_username=${encodeURIComponent(vertecUsername)}&password=${encodeURIComponent(vertecPassword)}`)
.then(response => {
return response.data;
})
.catch(err => this.error('Error fetching auth token', err.message));

return this.authTokenPromise;
}

/**
* Builds the select request object out of ocl expression data
*
Expand Down Expand Up @@ -477,9 +376,7 @@ export default class SimpleVertecApi {
return this.storedPromises[hash];
}

let promise = this.storedPromises[hash] = this.doRequest(xmlString);

return promise;
return this.storedPromises[hash] = this.doRequest(xmlString);
}

/**
Expand All @@ -494,7 +391,7 @@ export default class SimpleVertecApi {
*/
requestRetryStrategy(error) {
delete error?.config?.headers?.Authorization;

if (!error.response) {
this.error('Retry Strategy Error, No Response given', error);

Expand Down Expand Up @@ -539,20 +436,18 @@ export default class SimpleVertecApi {
'Content-Type': 'application/xml',
};

if (vertecApiKey) {
vertecSessionForRequest++;
vertecSessionForRequest++;

// start with session = 2 to avoid any conflicts of first session, which is maybe used by other processes
if (vertecSessionForRequest > (vertecMaxSessionsForRequests + 1)) {
vertecSessionForRequest = 2;
}
// start with session = 2 to avoid any conflicts of first session, which is maybe used by other processes
if (vertecSessionForRequest > (vertecMaxSessionsForRequests + 1)) {
vertecSessionForRequest = 2;
}

headers.Authorization = `Bearer ${vertecApiKey}`;
headers.VertecSessionTag = `${vertecSessionForRequest}`;
headers.Authorization = `Bearer ${vertecApiKey}`;
headers.VertecSessionTag = `${vertecSessionForRequest}`;

this.log('Vertec session: ' + vertecSessionForRequest);
this.log('Vertec request count: ' + (vertecRequestCount++));
}
this.log('Vertec session: ' + vertecSessionForRequest);
this.log('Vertec request count: ' + (vertecRequestCount++));

const client = axios.create({
headers,
Expand Down Expand Up @@ -684,10 +579,6 @@ export default class SimpleVertecApi {
for (let i = 0; i < arguments.length; i++) {
content = !_.isString(arguments[i]) ? JSON.stringify(arguments[i], null, 4) : arguments[i];

if (vertecPassword) {
content = _.isString(content) ? content.replace(vertecPassword, 'xxxxxxxxxx') : content;
}

console.log(content);
}
}
Expand All @@ -708,10 +599,6 @@ export default class SimpleVertecApi {
for (let i = 0; i < arguments.length; i++) {
content = !_.isString(arguments[i]) ? JSON.stringify(arguments[i], null, 4) : arguments[i];

if (vertecPassword) {
content = _.isString(content) ? content.replace(vertecPassword, 'xxxxxxxxxx') : content;
}

console.error(content);
}
}
Expand Down
Loading

0 comments on commit ce36304

Please sign in to comment.