Skip to content

Commit

Permalink
fix(skills): fix subsequent call api (#5628)
Browse files Browse the repository at this point in the history
  • Loading branch information
allardy committed Nov 1, 2021
1 parent b8b86a2 commit 4f53ce8
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
5 changes: 3 additions & 2 deletions modules/basic-skills/src/actions/call_api.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const callApi = async (url, method, body, memory, variable, headers) => {
}
const renderedHeaders = bp.cms.renderTemplate(headers, context)
const renderedBody = bp.cms.renderTemplate(body, context)
const keySuffix = args.randomId ? `_${args.randomId}` : ''

try {
const response = await axios({
Expand All @@ -23,13 +24,13 @@ const callApi = async (url, method, body, memory, variable, headers) => {
})

event.state[memory][variable] = { body: response.data, status: response.status }
event.state.temp.valid = true
event.state.temp[`valid${keySuffix}`] = true
} catch (error) {
const errorCode = (error.response && error.response.status) || error.code || ''
bp.logger.error(`Error: ${errorCode} while calling resource "${url}"`)

event.state[memory][variable] = { status: errorCode }
event.state.temp.valid = false
event.state.temp[`valid${keySuffix}`] = false
}
}

Expand Down
11 changes: 7 additions & 4 deletions modules/basic-skills/src/backend/callApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import _ from 'lodash'

const generateFlow = async (data: any, metadata: sdk.FlowGeneratorMetadata): Promise<sdk.FlowGenerationResult> => {
return {
transitions: createTransitions(),
transitions: createTransitions(data),
flow: {
nodes: createNodes(data),
catchAll: {
Expand All @@ -22,6 +22,7 @@ const createNodes = data => {
type: sdk.NodeActionType.RunAction,
name: 'basic-skills/call_api',
args: {
randomId: data.randomId,
url: data.url,
method: data.method,
body: data.body,
Expand All @@ -37,10 +38,12 @@ const createNodes = data => {
return nodes
}

const createTransitions = (): sdk.NodeTransition[] => {
const createTransitions = (data): sdk.NodeTransition[] => {
const keySuffix = data.randomId ? `_${data.randomId}` : ''

return [
{ caption: 'On success', condition: 'temp.valid', node: '' },
{ caption: 'On failure', condition: '!temp.valid', node: '' }
{ caption: 'On success', condition: `temp.valid${keySuffix}`, node: '' },
{ caption: 'On failure', condition: `!temp.valid${keySuffix}`, node: '' }
]
}

Expand Down
4 changes: 4 additions & 0 deletions modules/basic-skills/src/views/full/callApi.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import Select from 'react-select'
import style from './style.scss'
import { BotpressTooltip } from 'botpress/tooltip'
import { LinkDocumentationProvider } from 'botpress/documentation'
import nanoid from 'nanoid/generate'

const methodOptions = [
{ label: 'Get', value: 'get' },
Expand Down Expand Up @@ -37,6 +38,7 @@ export class CallAPI extends React.Component {
selectedMemory: memoryOptions[0],
variable: 'response',
body: undefined,
randomId: nanoid('abcdefghijklmnopqrstuvwxyz0123456789', 10),
headers: undefined,
url: undefined,
invalidJson: false
Expand All @@ -51,6 +53,7 @@ export class CallAPI extends React.Component {
this.setState({
selectedMethod: this.createSelectOption(this.getInitialDataProps('method')) || this.state.selectedMethod,
selectedMemory: this.createSelectOption(this.getInitialDataProps('memory')) || this.state.selectedMemory,
randomId: this.getOrDefault('randomId', 'randomId'),
headers: stringify(this.getInitialDataProps('headers')) || this.state.headers,
variable: this.getOrDefault('variable', 'variable'),
body: this.getOrDefault('body', 'body'),
Expand All @@ -65,6 +68,7 @@ export class CallAPI extends React.Component {
const data = {
method: selectedMethod.value,
memory: selectedMemory.value,
randomId: this.state.randomId,
body,
headers: headers ? JSON.parse(headers) : undefined,
url,
Expand Down

0 comments on commit 4f53ce8

Please sign in to comment.