Categories:
aura-mocks-json-server-api plugin
Module that mocks the behavior of any of the REST APIs called by aura-bot
Description
aura-mocks-json-server-api is a tool using express that allows you to generate REST services with predefined responses from a Javascript file that can be easily modified.
Find more information in the Github repository:
https://github.com/Telefonica/aura-mocks-server/tree/master/src/plugins/aura-mocks-json-server-api
This is part of aura-mocks-server project.
Create a simple “hello” response
You must create a file to store the information of your new response mocks in data/db/module (for our example, greetings.js):
data
├── db
│ ├── module
│ │ ├── greetings.js
Add to greetings.js the response to the request GET /greetings/hello:
module.exports = {
"configuration": {
"name": "greetings",
"basePath": "/greetings"
},
"routes": {
"/hello": {
"GET": {
"examples": {
"default": {
"status": 200,
"body": {
"response": "Hello from mock"
}
}
}
}
}
}
};
Now, you can make a call to:
<URL_MOCK_SERVER>/greetings/hello
That should answer with a:
{
"response": "Hello from mock"
}
Creating a more complex answer
For example, to create a POST service /aura-services/v1/users/:id, where the user is dynamic:
"routes": {
"/aura-services/v1/users/:id": {
"GET": {
"configuration": {
"getExampleKey": (req, res) => { return req.params && req.params.id ? req.params.id : 'default'; }
},
"examples": {
"default": {
"status": 200,
"body": {
"auraIdGlobal": "929285cb270001356476db33a4e31ce86402948213097e81717b50e549054cf2",
"auraId": "fb7926db-0283-41ef-a02f-541946090c92",
"userId": "85826044177A204DDEBCCEA98CE9439C3C770E03",
"channelId": "60f0ffda-e58a-4a96-aad9-d42be70b7b42",
"authorizationId": "",
"phoneNumber": "",
"idTokenHint": null
}
},
"fb7926db-0283-41ef-a02f-541946090c92": {
"status": 200,
"body": {
"auraIdGlobal": "929285cb270001356476db33a4e31ce86402948213097e81717b50e549054cf2",
"auraId": "fb7926db-0283-41ef-a02f-541946090c92",
"userId": "85826044177A204DDEBCCEA98CE9439C3C770E03",
"channelId": "60f0ffda-e58a-4a96-aad9-d42be70b7b42",
"authorizationId": "",
"phoneNumber": "",
"idTokenHint": null
}
}
}
}
}
}
How to get requests and responses automatically
The main idea is to obtain and generate all the requests in an automated way from the data sent and received by the aura-bot platform itself. In this way, there is no need to generate by hand each of the services required by the bot.
To obtain these requests, it is possible to use a sniffer and filter by the domains used by the aura-bot platform itself or use the aura-bot platform to record in a file all the requests and responses made (chosen option).
Modifications in Aura Bot Platform (temporary)
To use aura-bot platform as a request/response generator, you can temporarily modify the code as follows:
http-monkey-patcher.ts
line 1
import * as fs from 'fs';
before line 90
const requestResponse = {};
after line 122
parsedOptions.port = args[0].port;
after line 200: request.once ‘finish’
(requestResponse as any).request = {
type: 'request',
domain: host,
port: parsedOptions.port,
method: parsedOptions.method,
path: parsedOptions.path,
corr: this.getCorrelatorFromRequest(request) || CorrelatorUtil.noCorrelator,
headers: request.getHeaders(),
body: requestBody
};
after line 250: response.once ’end
(requestResponse as any).response = {
type: 'response',
domain: host,
method: parsedOptions.method,
status: response.statusCode,
path: parsedOptions.path,
body: responseBody,
drt: duration,
corr
};
fs.appendFile('/tmp/request-response-data.txt', `${JSON.stringify(requestResponse)}\n\n`, () => { });
To import the data obtained, check the documentation for json-server-import script
aura-mocks-json-server-api plugin scripts
Import requests/response to aura-mocks-server database
script: json-server-import.js
To import the information obtained previously, the script src/scripts/json-server-import.ts is used.
This script allows you to load the request / response information into the mock server database.
npm run jsonserver:import --file=/tmp/hello-greeting.txt
The previous sentence will load the requests, overwriting existing ones.
You can also debug the script code:
npm run jsonserver:import:debug --file=/tmp/hello-greeting.txt
How it works
The script will load in the JSON server database information of each request associated with the example named default. (All loaded requests are loaded as “samples” by default).
Examples are each of the responses that a request can return. You can create examples with a different identifier to default using the getExampleKey function (in configuration object) for that request:
...
"routes": {
"/aura-services/v1/users/:id": {
"GET": {
"configuration": {
"getExampleKey": (req, res) => { return req.params && req.params.id ? req.params.id : 'default'; }
},
...
In the previous configuration, the json-server-import script will get the example identifier using the id parameter obtained from the request URL itself. Thus, the request /aura-services/v1/users/e7c26f93-bf15-419b-8893-2728afad3b6c will create the example:
...
"examples": {
"e7c26f93-bf15-419b-8893-2728afad3b6c": {
"status": 200,
"body": {
"auraIdGlobal": "5c7ba5d26fa2945b3b9b29b64d6822edd914dd0d82cf28eae5287dd2a28b7768",
"auraId": "e7c26f93-bf15-419b-8893-2728afad3b6c",
"userId": "17c1ee02-0140-11ea-a69e-362b8e000000",
"channelId": "45494a5b-835a-4fff-a813-b3d2be529dbe",
"authorizationId": "",
"phoneNumber": "phone_number",
"idTokenHint": null
}
}
}
...
Show routes in aura-mocks-server database
script: json-server-routes.js
This script will show the routes stored in the JSON server database and the examples that each route contains.
npm run jsonserver:routes
aura-mocks-json-server-api plugin database
/v3/domain_classifier/default_query
Currently, the mocks service for apigw url /auracognitive/v3/domain_classifier/default_query is ready to respond to the following POST queries:
| Intent | Channel | Phrase (spanish) |
|---|---|---|
| intent.common.goodbyes | mp | hasta pronto |
| intent.common.greetings | mp | hola |
| intent.common.help | mp | ayuda |
| intent.common.swearwords | mp | joder |
| intent.common.thankyous | mp | gracias |
| intent.tv.content_get_info | mp | peliculas de fox |
| intent.tv.display | mp | quiero ver cuatro |
| intent.tv.none | mp | aabb |
| intent.tv.question_time_loc | mp | hora del partido del barcelona |
| intent.tv.search | mp | busca masterchef |
| intent.tv.search | mp | busca telediario |
| intent.tv.search_similar | mp | similar a telediario |
/aura-services/v1/users/:id
| User (auraId) | Channel Name | channelId |
|---|---|---|
| fb7926db-0283-41ef-a02f-541946090c92 | mp | 60f0ffda-e58a-4a96-aad9-d42be70b7b42 |