Skip to content

Commit 476316f

Browse files
RomanBachaloSigmaSoftwarepaigesrossimeihDS
authored
Added post web query code example (#75)
* added new monitor example * fixed indents * default dates * other text changes * step comments * removed extra colon in API method * Deleted extraneous title, extra colon, extra period * Removed extra colon Co-authored-by: Paige Rossi <paige.rossi@docusign.com> Co-authored-by: meihDS <70775251+meihDS@users.noreply.github.com>
1 parent 97b0c8d commit 476316f

File tree

8 files changed

+3131
-20
lines changed

8 files changed

+3131
-20
lines changed

index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ const {
4242
eg007rooms, eg008rooms, eg009rooms,
4343
} = require("./lib/rooms/controllers");
4444

45-
const { eg001monitor } = require("./lib/monitor/controllers/index");
45+
const {
46+
eg001monitor, eg002monitor
47+
} = require("./lib/monitor/controllers/index");
4648

4749
const {
4850
eg001admin, eg002admin, eg003admin, eg004admin, eg005admin
@@ -139,6 +141,8 @@ if (examplesApi.examplesApi.isRoomsApi) {
139141
} else if (examplesApi.examplesApi.isMonitorApi) {
140142
app.get('/eg001', eg001monitor.getController)
141143
.post('/eg001', eg001monitor.createController)
144+
.get('/eg002', eg002monitor.getController)
145+
.post('/eg002', eg002monitor.createController)
142146
} else if (examplesApi.examplesApi.isAdminApi) {
143147
app.get('/eg001', eg001admin.getController)
144148
.post('/eg001', eg001admin.createController)

lib/monitor/controllers/eg001GetMonitoringData.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ eg001GetMonitoringData.createController = async(req, res) => {
5454
if (results) {
5555
res.render('pages/example_done', {
5656
title: "Monitoring data",
57-
h1: "Monitoring data result",
57+
h1: "Get monitoring data",
5858
message: `Results from DataSet:getStream method:`,
5959
json: JSON.stringify(results).replace(/'/g,'')
6060
});
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/**
2+
* @file
3+
* Example 002: Post web query
4+
* @author DocuSign
5+
*/
6+
7+
const path = require('path');
8+
const validator = require('validator');
9+
const { postQuery } = require('../examples/postWebQuery');
10+
const dsConfig = require('../../../config/index.js').config;
11+
12+
const eg002PostWebQuery = exports;
13+
const eg = 'eg002'; // This example reference.
14+
const mustAuthenticate = '/ds/mustAuthenticate';
15+
const minimumBufferMin = 3;
16+
17+
/**
18+
* Create the envelope
19+
* @param {object} req Request obj
20+
* @param {object} res Response obj
21+
*/
22+
eg002PostWebQuery.createController = async(req, res) => {
23+
// Step 1. Check the token
24+
const tokenOK = req.dsAuth.checkToken(minimumBufferMin);
25+
if (!tokenOK) {
26+
req.flash('info', 'Sorry, you need to re-authenticate.');
27+
// Save the current operation so it will be resumed after authentication
28+
req.dsAuth.setEg(req, eg);
29+
res.redirect(mustAuthenticate);
30+
}
31+
32+
const { body } = req;
33+
const args = {
34+
accessToken: req.user.accessToken,
35+
basePath: dsConfig.monitorApiUrl,
36+
accountId: req.session.accountId,
37+
version: '2.0',
38+
dataset: 'monitor',
39+
startDate: validator.escape(body.startDate),
40+
endDate: validator.escape(body.endDate),
41+
};
42+
let results = null;
43+
44+
try {
45+
results = await postQuery(args);
46+
} catch (error) {
47+
const errorBody = error && error.response && error.response.body;
48+
const errorCode = errorBody && errorBody.errorCode;
49+
const errorMessage = errorBody && errorBody.message;
50+
51+
res.render('pages/error', { err: error, errorCode, errorMessage });
52+
}
53+
if (results) {
54+
res.render('pages/example_done', {
55+
title: "Query monitoring data with filters",
56+
h1: "Query monitoring data with filters",
57+
message: `Results from DataSet:postWebQuery method:`,
58+
json: JSON.stringify(results).replace(/'/g,'')
59+
});
60+
}
61+
}
62+
63+
/**
64+
* Form page for this application
65+
*/
66+
eg002PostWebQuery.getController = (req, res) => {
67+
// Check that the authentication token is ok with a long buffer time.
68+
// If needed, now is the best time to ask the user to authenticate
69+
// since they have not yet entered any information into the form.
70+
const tokenOK = req.dsAuth.checkToken();
71+
const currentDate = new Date();
72+
const startDate = new Date();
73+
startDate.setDate(startDate.getDate() - 10);
74+
75+
if (tokenOK) {
76+
sourceFile = (path.basename(__filename))[5].toLowerCase() + (path.basename(__filename)).substr(6);
77+
res.render('pages/monitor-examples/eg002PostWebQuery', {
78+
eg: eg,
79+
csrfToken: req.csrfToken(),
80+
title: "Query monitoring data with filters",
81+
sourceFile: sourceFile,
82+
sourceUrl: dsConfig.githubExampleUrl + "monitor/examples/" + sourceFile,
83+
documentation: dsConfig.documentation + eg,
84+
showDoc: dsConfig.documentation,
85+
currentDate: currentDate.toISOString().substring(0,10),
86+
startDate: startDate.toISOString().substring(0,10)
87+
});
88+
} else {
89+
// Save the current operation so it will be resumed after authentication
90+
req.dsAuth.setEg(req, eg);
91+
res.redirect(mustAuthenticate);
92+
}
93+
}

lib/monitor/controllers/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
module.exports.eg001monitor = require('./eg001GetMonitoringData');
2+
module.exports.eg002monitor = require('./eg002PostWebQuery');
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/**
2+
* @file
3+
* Example 002: Post web query
4+
* @author DocuSign
5+
*/
6+
7+
const docusign = require('docusign-monitor');
8+
9+
/**
10+
* This function does the work of getting the monitoring data with filters
11+
*/
12+
const postQuery = async (args) => {
13+
14+
// Step 2 start
15+
let dsApiClient = new docusign.ApiClient();
16+
dsApiClient.setBasePath(args.basePath);
17+
dsApiClient.addDefaultHeader("Authorization", "Bearer " + args.accessToken);
18+
const datasetApi = new docusign.DataSetApi(dsApiClient);
19+
// Step 2 end
20+
21+
// Step 4 start
22+
const queryData = getQuery(args);
23+
const result = await datasetApi.postWebQuery(queryData, args.version, args.dataset);
24+
// Step 4 end
25+
26+
return result;
27+
}
28+
29+
// Step 3 start
30+
const getQuery = (args) => {
31+
return {
32+
"filters": [
33+
{
34+
"FilterName": "Time",
35+
"BeginTime": args.startDate,
36+
"EndTime": args.endDate
37+
},
38+
{
39+
"FilterName": "Has",
40+
"ColumnName": "AccountId",
41+
"Value": args.accountId
42+
}
43+
],
44+
"aggregations": [
45+
{
46+
"aggregationName": "Raw",
47+
"limit": "1",
48+
"orderby": [
49+
"Timestamp, desc"
50+
]
51+
}
52+
]
53+
}
54+
}
55+
// Step 3 end
56+
module.exports = { postQuery };

0 commit comments

Comments
 (0)