Skip to content

Commit

Permalink
Merge pull request #654 from codeforequity-at/develop
Browse files Browse the repository at this point in the history
Botium Core 1.11.14
  • Loading branch information
Botium authored Sep 24, 2021
2 parents b743664 + 16d5f47 commit dcc31a3
Show file tree
Hide file tree
Showing 7 changed files with 215 additions and 57 deletions.
20 changes: 19 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "botium-core",
"version": "1.11.13",
"version": "1.11.14",
"description": "The Selenium for Chatbots",
"main": "index.js",
"module": "dist/botium-es.js",
Expand Down Expand Up @@ -86,6 +86,7 @@
"eslint-plugin-promise": "^5.1.0",
"eslint-plugin-standard": "^4.1.0",
"license-checker": "^25.0.1",
"license-compatibility-checker": "^0.3.5",
"mocha": "^9.1.1",
"nock": "^13.1.3",
"npm-check-updates": "^11.8.5",
Expand Down
6 changes: 6 additions & 0 deletions src/Capabilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ module.exports = {
SIMPLEREST_PING_BODY: 'SIMPLEREST_PING_BODY',
SIMPLEREST_PING_BODY_RAW: 'SIMPLEREST_PING_BODY_RAW',
SIMPLEREST_PING_HEADERS: 'SIMPLEREST_PING_HEADERS',
SIMPLEREST_PING_REQUEST_HOOK: 'SIMPLEREST_PING_REQUEST_HOOK',
SIMPLEREST_PING_RETRIES: 'SIMPLEREST_PING_RETRIES',
SIMPLEREST_PING_TIMEOUT: 'SIMPLEREST_PING_TIMEOUT',
SIMPLEREST_PING_UPDATE_CONTEXT: 'SIMPLEREST_PING_UPDATE_CONTEXT',
Expand All @@ -27,6 +28,7 @@ module.exports = {
SIMPLEREST_START_BODY: 'SIMPLEREST_START_BODY',
SIMPLEREST_START_BODY_RAW: 'SIMPLEREST_START_BODY_RAW',
SIMPLEREST_START_HEADERS: 'SIMPLEREST_START_HEADERS',
SIMPLEREST_START_REQUEST_HOOK: 'SIMPLEREST_START_REQUEST_HOOK',
SIMPLEREST_START_RETRIES: 'SIMPLEREST_START_RETRIES',
SIMPLEREST_START_TIMEOUT: 'SIMPLEREST_START_TIMEOUT',
SIMPLEREST_START_UPDATE_CONTEXT: 'SIMPLEREST_START_UPDATE_CONTEXT',
Expand All @@ -36,10 +38,12 @@ module.exports = {
SIMPLEREST_STOP_BODY: 'SIMPLEREST_STOP_BODY',
SIMPLEREST_STOP_BODY_RAW: 'SIMPLEREST_STOP_BODY_RAW',
SIMPLEREST_STOP_HEADERS: 'SIMPLEREST_STOP_HEADERS',
SIMPLEREST_STOP_REQUEST_HOOK: 'SIMPLEREST_STOP_REQUEST_HOOK',
SIMPLEREST_STOP_RETRIES: 'SIMPLEREST_STOP_RETRIES',
SIMPLEREST_STOP_TIMEOUT: 'SIMPLEREST_STOP_TIMEOUT',
SIMPLEREST_INIT_CONTEXT: 'SIMPLEREST_INIT_CONTEXT',
SIMPLEREST_INIT_TEXT: 'SIMPLEREST_INIT_TEXT',
SIMPLEREST_INIT_PROCESS_RESPONSE: 'SIMPLEREST_INIT_PROCESS_RESPONSE',
SIMPLEREST_PROXY_URL: 'SIMPLEREST_PROXY_URL',
SIMPLEREST_STRICT_SSL: 'SIMPLEREST_STRICT_SSL',
SIMPLEREST_URL: 'SIMPLEREST_URL',
Expand All @@ -54,11 +58,13 @@ module.exports = {
SIMPLEREST_START_HOOK: 'SIMPLEREST_START_HOOK',
SIMPLEREST_STOP_HOOK: 'SIMPLEREST_STOP_HOOK',
SIMPLEREST_REQUEST_HOOK: 'SIMPLEREST_REQUEST_HOOK',
SIMPLEREST_PARSER_HOOK: 'SIMPLEREST_PARSER_HOOK',
SIMPLEREST_POLL_URL: 'SIMPLEREST_POLL_URL',
SIMPLEREST_POLL_VERB: 'SIMPLEREST_POLL_VERB',
SIMPLEREST_POLL_BODY: 'SIMPLEREST_POLL_BODY',
SIMPLEREST_POLL_BODY_RAW: 'SIMPLEREST_POLL_BODY_RAW',
SIMPLEREST_POLL_HEADERS: 'SIMPLEREST_POLL_HEADERS',
SIMPLEREST_POLL_REQUEST_HOOK: 'SIMPLEREST_POLL_REQUEST_HOOK',
SIMPLEREST_POLL_INTERVAL: 'SIMPLEREST_POLL_INTERVAL',
SIMPLEREST_POLL_TIMEOUT: 'SIMPLEREST_PING_TIMEOUT',
SIMPLEREST_POLL_UPDATE_CONTEXT: 'SIMPLEREST_POLL_UPDATE_CONTEXT',
Expand Down
1 change: 1 addition & 0 deletions src/Defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ module.exports = {
[Capabilities.SIMPLEREST_PING_VERB]: 'GET',
[Capabilities.SIMPLEREST_PING_UPDATE_CONTEXT]: true,
[Capabilities.SIMPLEREST_PING_PROCESS_RESPONSE]: false,
[Capabilities.SIMPLEREST_INIT_PROCESS_RESPONSE]: false,
[Capabilities.SIMPLEREST_STOP_RETRIES]: 6,
[Capabilities.SIMPLEREST_STOP_TIMEOUT]: 10000,
[Capabilities.SIMPLEREST_STOP_VERB]: 'GET',
Expand Down
148 changes: 97 additions & 51 deletions src/containers/plugins/SimpleRestContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,20 @@ module.exports = class SimpleRestContainer {
this.startHook = getHook(this.caps, this.caps[Capabilities.SIMPLEREST_START_HOOK])
this.stopHook = getHook(this.caps, this.caps[Capabilities.SIMPLEREST_STOP_HOOK])
this.requestHook = getHook(this.caps, this.caps[Capabilities.SIMPLEREST_REQUEST_HOOK])
this.parserHook = getHook(this.caps, this.caps[Capabilities.SIMPLEREST_PARSER_HOOK])
this.responseHook = getHook(this.caps, this.caps[Capabilities.SIMPLEREST_RESPONSE_HOOK])
this.pollRequestHook = getHook(this.caps, this.caps[Capabilities.SIMPLEREST_POLL_REQUEST_HOOK])

this.requestHooks = {}
if (this.caps[Capabilities.SIMPLEREST_PING_REQUEST_HOOK]) {
this.requestHooks.SIMPLEREST_PING = getHook(this.caps, this.caps[Capabilities.SIMPLEREST_PING_REQUEST_HOOK])
}
if (this.caps[Capabilities.SIMPLEREST_START_REQUEST_HOOK]) {
this.requestHooks.SIMPLEREST_START = getHook(this.caps, this.caps[Capabilities.SIMPLEREST_START_REQUEST_HOOK])
}
if (this.caps[Capabilities.SIMPLEREST_STOP_REQUEST_HOOK]) {
this.requestHooks.SIMPLEREST_STOP = getHook(this.caps, this.caps[Capabilities.SIMPLEREST_STOP_REQUEST_HOOK])
}
}

Build () {
Expand Down Expand Up @@ -111,18 +124,34 @@ module.exports = class SimpleRestContainer {
executeHook(this.caps, this.startHook, this.view).then(() => startHookComplete()).catch(startHookComplete)
},

(inboundListenerComplete) => {
this._subscribeInbound()
.then(() => inboundListenerComplete())
.catch(inboundListenerComplete)
},

(startPollingComplete) => {
this._startPolling()
.then(() => startPollingComplete())
.catch(startPollingComplete)
},

(pingComplete) => {
if (this.caps[Capabilities.SIMPLEREST_PING_URL]) {
this._makeCall('SIMPLEREST_PING')
.then(response => {
.then(body => {
if (this.caps[Capabilities.SIMPLEREST_PING_UPDATE_CONTEXT] || this.caps[Capabilities.SIMPLEREST_PING_PROCESS_RESPONSE]) {
if (_.isObject(response) || botiumUtils.isStringJson(response)) {
debug(`Ping Uri ${this.caps[Capabilities.SIMPLEREST_PING_URL]} returned JSON response: ${botiumUtils.shortenJsonString(response)}`)
const body = _.isObject(response) ? response : JSON.parse(response)
return this._processBodyAsync(body, !!this.caps[Capabilities.SIMPLEREST_PING_PROCESS_RESPONSE], !!this.caps[Capabilities.SIMPLEREST_PING_UPDATE_CONTEXT])
} else {
debug(`Ping Uri ${this.caps[Capabilities.SIMPLEREST_PING_URL]} didn't return JSON response, ignoring it.`)
}
return this._parseResponseBody(body)
.then(body => {
if (body) {
debug(`Ping Uri ${this.caps[Capabilities.SIMPLEREST_PING_URL]} returned JSON response: ${botiumUtils.shortenJsonString(body)}`)
return this._processBodyAsync(body, !!this.caps[Capabilities.SIMPLEREST_PING_PROCESS_RESPONSE], !!this.caps[Capabilities.SIMPLEREST_PING_UPDATE_CONTEXT])
} else {
debug(`Ping Uri ${this.caps[Capabilities.SIMPLEREST_PING_URL]} didn't return JSON response, ignoring it.`)
}
}).catch(err => {
debug(`Ping Uri ${this.caps[Capabilities.SIMPLEREST_PING_URL]} didn't return JSON response, ignoring it (${err.message})`)
})
}
}).then(() => {
pingComplete()
Expand All @@ -136,37 +165,29 @@ module.exports = class SimpleRestContainer {

(initComplete) => {
if (_.isString(this.caps[Capabilities.SIMPLEREST_INIT_TEXT])) {
this._doRequest({ messageText: this.caps[Capabilities.SIMPLEREST_INIT_TEXT] }, false, true).then(() => initComplete()).catch(initComplete)
this._doRequest({ messageText: this.caps[Capabilities.SIMPLEREST_INIT_TEXT] }, !!this.caps[Capabilities.SIMPLEREST_INIT_PROCESS_RESPONSE], true).then(() => initComplete()).catch(initComplete)
} else {
initComplete()
}
},

(inboundListenerComplete) => {
this._subscribeInbound()
.then(() => inboundListenerComplete())
.catch(inboundListenerComplete)
},

(startPollingComplete) => {
this._startPolling()
.then(() => startPollingComplete())
.catch(startPollingComplete)
},

(startCallComplete) => {
this.processInbound = true
if (this.caps[Capabilities.SIMPLEREST_START_URL]) {
this._makeCall('SIMPLEREST_START')
.then(response => {
.then(body => {
if (this.caps[Capabilities.SIMPLEREST_START_UPDATE_CONTEXT] || this.caps[Capabilities.SIMPLEREST_START_PROCESS_RESPONSE]) {
if (_.isObject(response) || botiumUtils.isStringJson(response)) {
debug(`Start Uri ${this.caps[Capabilities.SIMPLEREST_START_URL]} returned JSON response: ${botiumUtils.shortenJsonString(response)}`)
const body = _.isObject(response) ? response : JSON.parse(response)
return this._processBodyAsync(body, !!this.caps[Capabilities.SIMPLEREST_START_PROCESS_RESPONSE], !!this.caps[Capabilities.SIMPLEREST_START_UPDATE_CONTEXT])
} else {
debug(`Start Uri ${this.caps[Capabilities.SIMPLEREST_START_URL]} didn't return JSON response, ignoring it.`)
}
return this._parseResponseBody(body)
.then(body => {
if (body) {
debug(`Start Uri ${this.caps[Capabilities.SIMPLEREST_START_URL]} returned JSON response: ${botiumUtils.shortenJsonString(body)}`)
return this._processBodyAsync(body, !!this.caps[Capabilities.SIMPLEREST_START_PROCESS_RESPONSE], !!this.caps[Capabilities.SIMPLEREST_START_UPDATE_CONTEXT])
} else {
debug(`Start Uri ${this.caps[Capabilities.SIMPLEREST_START_URL]} didn't return JSON response, ignoring it.`)
}
}).catch(err => {
debug(`Start Uri ${this.caps[Capabilities.SIMPLEREST_START_URL]} didn't return JSON response, ignoring it (${err.message})`)
})
}
}).then(() => {
startCallComplete()
Expand Down Expand Up @@ -357,9 +378,9 @@ module.exports = class SimpleRestContainer {

this.waitProcessQueue = []

request(requestOptions, (err, response, body) => {
request(requestOptions, async (err, response, body) => {
if (err) {
reject(new Error(`rest request failed: ${err.message}`))
return reject(new Error(`rest request failed: ${err.message}`))
} else {
if (response.statusCode >= 400) {
debug(`got error response: ${response.statusCode}/${response.statusMessage}`)
Expand All @@ -368,16 +389,17 @@ module.exports = class SimpleRestContainer {

if (body) {
debug(`got response code: ${response.statusCode}, body: ${botiumUtils.shortenJsonString(body)}`)
if (_.isString(body)) {
try {
body = JSON.parse(body.trim())
this._processBodyAsync(body, isFromUser, updateContext).then(() => resolve(this)).then(() => this._emptyWaitProcessQueue())
} catch (err) {
debug(`ignoring not JSON formatted response body (${err.message})`)
resolve(this)
this._emptyWaitProcessQueue()
}
} else if (_.isObject(body)) {

try {
body = await this._parseResponseBody(body)
} catch (err) {
debug(`ignoring not JSON formatted response body: ${err.message}`)
resolve(this)
this._emptyWaitProcessQueue()
return
}

if (body) {
this._processBodyAsync(body, isFromUser, updateContext).then(() => resolve(this)).then(() => this._emptyWaitProcessQueue())
} else {
debug('ignoring response body (no string and no JSON object)')
Expand Down Expand Up @@ -502,6 +524,22 @@ module.exports = class SimpleRestContainer {
}
}

async _parseResponseBody (body) {
if (!_.isObject(body) && _.isString(body)) {
try {
body = JSON.parse(body)
} catch (err) {
if (!this.parserHook) throw err
}
}
if (this.parserHook) {
await executeHook(this.caps, this.parserHook, Object.assign({ body, changeBody: (b) => { body = b } }, this.view))
}
if (_.isObject(body)) return body
else if (_.isString(body)) return JSON.parse(body)
else return null
}

_getCapValue (capName) {
return _.isFunction(this.caps[capName]) ? (this.caps[capName])() : this.caps[capName]
}
Expand Down Expand Up @@ -629,7 +667,7 @@ module.exports = class SimpleRestContainer {
}
}

_runPolling () {
async _runPolling () {
if (!this.processInbound) return

if (this.caps[Capabilities.SIMPLEREST_POLL_URL]) {
Expand Down Expand Up @@ -662,22 +700,28 @@ module.exports = class SimpleRestContainer {
}
this._addRequestOptions(pollConfig)

request(pollConfig, (err, response, body) => {
try {
await executeHook(this.caps, this.pollRequestHook, Object.assign({ requestOptions: pollConfig }, this.view))
} catch (err) {
debug(`_runPolling: exeucting request hook failed - (${err.message})`)
return
}
request(pollConfig, async (err, response, body) => {
if (err) {
debug(`_runPolling: rest request failed: ${err.message}, request: ${JSON.stringify(pollConfig)}`)
} else {
if (response.statusCode >= 400) {
debug(`_runPolling: got error response: ${response.statusCode}/${response.statusMessage}, request: ${JSON.stringify(pollConfig)}`)
} else if (body) {
debug(`_runPolling: got response code: ${response.statusCode}, body: ${botiumUtils.shortenJsonString(body)}`)
if (_.isString(body)) {
try {
body = JSON.parse(body)
setTimeout(() => this._processBodyAsync(body, true, !!this.caps[Capabilities.SIMPLEREST_POLL_UPDATE_CONTEXT]), 0)
} catch (err) {
debug(`_runPolling: ignoring not JSON formatted response body (${err.message})`)
}
} else if (_.isObject(body)) {

try {
body = await this._parseResponseBody(body)
} catch (err) {
debug(`_runPolling: ignoring not JSON formatted response body: ${err.message}`)
return
}
if (body) {
setTimeout(() => this._processBodyAsync(body, true, !!this.caps[Capabilities.SIMPLEREST_POLL_UPDATE_CONTEXT]), 0)
} else {
debug('_runPolling: ignoring response body (no string and no JSON object)')
Expand Down Expand Up @@ -733,6 +777,8 @@ module.exports = class SimpleRestContainer {
}
this._addRequestOptions(httpConfig)

await executeHook(this.caps, this.requestHooks[capPrefix], Object.assign({ requestOptions: httpConfig }, this.view))

const retries = this._getCapValue(`${capPrefix}_RETRIES`)
debug(`_makeCall(${capPrefix}): rest request: ${JSON.stringify(httpConfig)}`)
const response = await this._waitForUrlResponse(httpConfig, retries)
Expand Down
7 changes: 3 additions & 4 deletions src/scripting/logichook/logichooks/UpdateCustomLogicHook.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
const util = require('util')
const _ = require('lodash')

const { isStringJson } = require('../../../helpers/Utils')

module.exports = class UpdateCustomLogicHook {
constructor (context, caps = {}, globalArgs = {}) {
this.context = context
Expand Down Expand Up @@ -58,10 +56,11 @@ module.exports = class UpdateCustomLogicHook {
}

_getValue (raw) {
if (isStringJson(raw)) {
try {
return JSON.parse(raw)
} catch (e) {
return raw
}
return raw
}

_update (args, meMsg) {
Expand Down
Loading

0 comments on commit dcc31a3

Please sign in to comment.