comprodls-sdk 2.12.0 → 2.12.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintrc +28 -28
- package/.npmignore +5 -0
- package/README.md +371 -371
- package/dist/comprodls-sdk.js +11493 -11471
- package/dist/comprodls-sdk.min.js +14 -14
- package/grunt/publish.js +148 -148
- package/lib/comprodls.js +146 -146
- package/lib/config/index.js +337 -337
- package/lib/helpers/index.js +29 -29
- package/lib/helpers/lib/api/converter.js +119 -119
- package/lib/helpers/lib/api/index.js +120 -120
- package/lib/helpers/lib/api/validations.js +72 -72
- package/lib/helpers/lib/errors.js +129 -129
- package/lib/helpers/lib/utils.js +23 -23
- package/lib/helpers/lib/validator.js +100 -100
- package/lib/open_access/index.js +121 -121
- package/lib/services/activity/activity.js +209 -209
- package/lib/services/activity/attempt.js +431 -431
- package/lib/services/activity/index.js +28 -28
- package/lib/services/analytics/index.js +1555 -1555
- package/lib/services/attempts/index.js +342 -342
- package/lib/services/auth/classProduct.js +37 -37
- package/lib/services/auth/index.js +2541 -2541
- package/lib/services/collab/index.js +468 -468
- package/lib/services/drive/index.js +144 -144
- package/lib/services/integrations/index.js +279 -279
- package/lib/services/invitations/index.js +313 -313
- package/lib/services/lrs/index.js +459 -459
- package/lib/services/product/index.js +267 -267
- package/lib/services/pub/index.js +407 -407
- package/lib/services/push/index.js +187 -187
- package/lib/services/push/pubnubClientWrapper.js +557 -557
- package/lib/services/push/sessionStorage.js +64 -64
- package/lib/services/pushX/index.js +190 -190
- package/lib/services/pushX/pubnubClientWrapper.js +211 -211
- package/lib/services/sisevents/index.js +113 -113
- package/lib/services/spaces/index.js +976 -929
- package/lib/services/superuser/index.js +175 -175
- package/lib/services/workflows/index.js +464 -464
- package/lib/services/xapi/index.js +232 -232
- package/lib/token/index.js +114 -114
- package/lib/token/validations.js +88 -88
- package/package-lock.json +5095 -0
- package/package.json +1 -1
- package/test.js +50 -50
- package/.vscode/launch.json +0 -23
- package/npm-debug.log.189866131 +0 -0
- package/npm-debug.log.712840116 +0 -26
package/lib/helpers/index.js
CHANGED
|
@@ -1,30 +1,30 @@
|
|
|
1
|
-
/*************************************************************************
|
|
2
|
-
*
|
|
3
|
-
* COMPRO CONFIDENTIAL
|
|
4
|
-
* __________________
|
|
5
|
-
*
|
|
6
|
-
* [2015] - [2020] Compro Technologies Private Limited
|
|
7
|
-
* All Rights Reserved.
|
|
8
|
-
*
|
|
9
|
-
* NOTICE: All information contained herein is, and remains
|
|
10
|
-
* the property of Compro Technologies Private Limited. The
|
|
11
|
-
* intellectual and technical concepts contained herein are
|
|
12
|
-
* proprietary to Compro Technologies Private Limited and may
|
|
13
|
-
* be covered by U.S. and Foreign Patents, patents in process,
|
|
14
|
-
* and are protected by trade secret or copyright law.
|
|
15
|
-
*
|
|
16
|
-
* Dissemination of this information or reproduction of this material
|
|
17
|
-
* is strictly forbidden unless prior written permission is obtained
|
|
18
|
-
* from Compro Technologies Pvt. Ltd..
|
|
19
|
-
***************************************************************************/
|
|
20
|
-
/***********************************************************
|
|
21
|
-
* comproDLS SDK Helpers Module
|
|
22
|
-
* This module provides definition of helper functions for SDK.
|
|
23
|
-
************************************************************/
|
|
24
|
-
/****************************************************
|
|
25
|
-
* Setting up Exports/Public functions and variables
|
|
26
|
-
*****************************************************/
|
|
27
|
-
exports.api = require('./lib/api');
|
|
28
|
-
exports.errors = require('./lib/errors');
|
|
29
|
-
exports.utils = require('./lib/utils');
|
|
1
|
+
/*************************************************************************
|
|
2
|
+
*
|
|
3
|
+
* COMPRO CONFIDENTIAL
|
|
4
|
+
* __________________
|
|
5
|
+
*
|
|
6
|
+
* [2015] - [2020] Compro Technologies Private Limited
|
|
7
|
+
* All Rights Reserved.
|
|
8
|
+
*
|
|
9
|
+
* NOTICE: All information contained herein is, and remains
|
|
10
|
+
* the property of Compro Technologies Private Limited. The
|
|
11
|
+
* intellectual and technical concepts contained herein are
|
|
12
|
+
* proprietary to Compro Technologies Private Limited and may
|
|
13
|
+
* be covered by U.S. and Foreign Patents, patents in process,
|
|
14
|
+
* and are protected by trade secret or copyright law.
|
|
15
|
+
*
|
|
16
|
+
* Dissemination of this information or reproduction of this material
|
|
17
|
+
* is strictly forbidden unless prior written permission is obtained
|
|
18
|
+
* from Compro Technologies Pvt. Ltd..
|
|
19
|
+
***************************************************************************/
|
|
20
|
+
/***********************************************************
|
|
21
|
+
* comproDLS SDK Helpers Module
|
|
22
|
+
* This module provides definition of helper functions for SDK.
|
|
23
|
+
************************************************************/
|
|
24
|
+
/****************************************************
|
|
25
|
+
* Setting up Exports/Public functions and variables
|
|
26
|
+
*****************************************************/
|
|
27
|
+
exports.api = require('./lib/api');
|
|
28
|
+
exports.errors = require('./lib/errors');
|
|
29
|
+
exports.utils = require('./lib/utils');
|
|
30
30
|
exports.validations = require('./lib/validator');
|
|
@@ -1,119 +1,119 @@
|
|
|
1
|
-
/*************************************************************************
|
|
2
|
-
*
|
|
3
|
-
* COMPRO CONFIDENTIAL
|
|
4
|
-
* __________________
|
|
5
|
-
*
|
|
6
|
-
* [2015] - [2020] Compro Technologies Private Limited
|
|
7
|
-
* All Rights Reserved.
|
|
8
|
-
*
|
|
9
|
-
* NOTICE: All information contained herein is, and remains
|
|
10
|
-
* the property of Compro Technologies Private Limited. The
|
|
11
|
-
* intellectual and technical concepts contained herein are
|
|
12
|
-
* proprietary to Compro Technologies Private Limited and may
|
|
13
|
-
* be covered by U.S. and Foreign Patents, patents in process,
|
|
14
|
-
* and are protected by trade secret or copyright law.
|
|
15
|
-
*
|
|
16
|
-
* Dissemination of this information or reproduction of this material
|
|
17
|
-
* is strictly forbidden unless prior written permission is obtained
|
|
18
|
-
* from Compro Technologies Pvt. Ltd..
|
|
19
|
-
***************************************************************************/
|
|
20
|
-
|
|
21
|
-
/***********************************************************
|
|
22
|
-
* comproDLS SDK AUTH API Adaptor
|
|
23
|
-
* Functions for calling AUTH API.
|
|
24
|
-
************************************************************/
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* This function converts user entity to generate SDK response.
|
|
28
|
-
* @param { *data: {userEntityDDB} } options
|
|
29
|
-
*/
|
|
30
|
-
function __convertUserResponse(options) {
|
|
31
|
-
var userEntity = options.data,
|
|
32
|
-
userRoles = userEntity.roles;
|
|
33
|
-
|
|
34
|
-
if(Array.isArray(userRoles)) {
|
|
35
|
-
|
|
36
|
-
//convert to SDK response.
|
|
37
|
-
var userRolesInResponse = {};
|
|
38
|
-
for(var roleIndex in userRoles) {
|
|
39
|
-
userRolesInResponse[userRoles[roleIndex]] = [];
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
userEntity.roles = userRolesInResponse;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
return userEntity;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* This function converts class entity from DDB to generate SDK response.
|
|
50
|
-
* @param { *data: {classEntityDDB} } options
|
|
51
|
-
*/
|
|
52
|
-
function __convertClassResponse(options) {
|
|
53
|
-
var classEntity = options.data;
|
|
54
|
-
|
|
55
|
-
if (classEntity.hasOwnProperty('pk') === false) {
|
|
56
|
-
// BAAS structure returned, no need to convert response.
|
|
57
|
-
return classEntity;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
var response = {
|
|
61
|
-
uuid: classEntity.classid,
|
|
62
|
-
type: 'group'
|
|
63
|
-
};
|
|
64
|
-
|
|
65
|
-
Object.assign(response, classEntity);
|
|
66
|
-
|
|
67
|
-
var firstName = classEntity.class.instructor.first_name,
|
|
68
|
-
lastName = classEntity.class.instructor.last_name;
|
|
69
|
-
|
|
70
|
-
response.class.instructor.uuid = classEntity.class.instructor.dls_user_id;
|
|
71
|
-
|
|
72
|
-
if (firstName || lastName) {
|
|
73
|
-
response.class.instructor.name = firstName ? firstName : '';
|
|
74
|
-
|
|
75
|
-
if (lastName) {
|
|
76
|
-
response.class.instructor.name += firstName ? (' ' + lastName) : lastName;
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
return response;
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
/**
|
|
85
|
-
* This function converts enrollment entities to generate SDK response.
|
|
86
|
-
* @param { *data: [enrollmentEntities] } options
|
|
87
|
-
*/
|
|
88
|
-
function __convertEnrollmetsResponse(options) {
|
|
89
|
-
var enrollmentEntities = options.data;
|
|
90
|
-
|
|
91
|
-
//convert to SDK response.
|
|
92
|
-
for(var enrollmentIdx in enrollmentEntities) {
|
|
93
|
-
var enrollment = enrollmentEntities[enrollmentIdx];
|
|
94
|
-
var firstName = enrollment.first_name,
|
|
95
|
-
lastName = enrollment.last_name;
|
|
96
|
-
if(firstName || lastName) {
|
|
97
|
-
enrollment.name = firstName ? firstName : '';
|
|
98
|
-
|
|
99
|
-
if(lastName) {
|
|
100
|
-
enrollment.name += firstName ? (' ' + lastName) : lastName;
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
enrollment.uuid = enrollment.dls_user_id;
|
|
105
|
-
|
|
106
|
-
if(enrollment.username) {
|
|
107
|
-
enrollment.ext_username = enrollment.username;
|
|
108
|
-
}
|
|
109
|
-
enrollment.username = enrollment.userid + '--ACCOUNT';
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
return enrollmentEntities;
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
module.exports = {
|
|
116
|
-
convertUserResponse: __convertUserResponse,
|
|
117
|
-
convertClassResponse: __convertClassResponse,
|
|
118
|
-
convertEnrollmetsResponse: __convertEnrollmetsResponse
|
|
119
|
-
};
|
|
1
|
+
/*************************************************************************
|
|
2
|
+
*
|
|
3
|
+
* COMPRO CONFIDENTIAL
|
|
4
|
+
* __________________
|
|
5
|
+
*
|
|
6
|
+
* [2015] - [2020] Compro Technologies Private Limited
|
|
7
|
+
* All Rights Reserved.
|
|
8
|
+
*
|
|
9
|
+
* NOTICE: All information contained herein is, and remains
|
|
10
|
+
* the property of Compro Technologies Private Limited. The
|
|
11
|
+
* intellectual and technical concepts contained herein are
|
|
12
|
+
* proprietary to Compro Technologies Private Limited and may
|
|
13
|
+
* be covered by U.S. and Foreign Patents, patents in process,
|
|
14
|
+
* and are protected by trade secret or copyright law.
|
|
15
|
+
*
|
|
16
|
+
* Dissemination of this information or reproduction of this material
|
|
17
|
+
* is strictly forbidden unless prior written permission is obtained
|
|
18
|
+
* from Compro Technologies Pvt. Ltd..
|
|
19
|
+
***************************************************************************/
|
|
20
|
+
|
|
21
|
+
/***********************************************************
|
|
22
|
+
* comproDLS SDK AUTH API Adaptor
|
|
23
|
+
* Functions for calling AUTH API.
|
|
24
|
+
************************************************************/
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* This function converts user entity to generate SDK response.
|
|
28
|
+
* @param { *data: {userEntityDDB} } options
|
|
29
|
+
*/
|
|
30
|
+
function __convertUserResponse(options) {
|
|
31
|
+
var userEntity = options.data,
|
|
32
|
+
userRoles = userEntity.roles;
|
|
33
|
+
|
|
34
|
+
if(Array.isArray(userRoles)) {
|
|
35
|
+
|
|
36
|
+
//convert to SDK response.
|
|
37
|
+
var userRolesInResponse = {};
|
|
38
|
+
for(var roleIndex in userRoles) {
|
|
39
|
+
userRolesInResponse[userRoles[roleIndex]] = [];
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
userEntity.roles = userRolesInResponse;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
return userEntity;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* This function converts class entity from DDB to generate SDK response.
|
|
50
|
+
* @param { *data: {classEntityDDB} } options
|
|
51
|
+
*/
|
|
52
|
+
function __convertClassResponse(options) {
|
|
53
|
+
var classEntity = options.data;
|
|
54
|
+
|
|
55
|
+
if (classEntity.hasOwnProperty('pk') === false) {
|
|
56
|
+
// BAAS structure returned, no need to convert response.
|
|
57
|
+
return classEntity;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
var response = {
|
|
61
|
+
uuid: classEntity.classid,
|
|
62
|
+
type: 'group'
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
Object.assign(response, classEntity);
|
|
66
|
+
|
|
67
|
+
var firstName = classEntity.class.instructor.first_name,
|
|
68
|
+
lastName = classEntity.class.instructor.last_name;
|
|
69
|
+
|
|
70
|
+
response.class.instructor.uuid = classEntity.class.instructor.dls_user_id;
|
|
71
|
+
|
|
72
|
+
if (firstName || lastName) {
|
|
73
|
+
response.class.instructor.name = firstName ? firstName : '';
|
|
74
|
+
|
|
75
|
+
if (lastName) {
|
|
76
|
+
response.class.instructor.name += firstName ? (' ' + lastName) : lastName;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
return response;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* This function converts enrollment entities to generate SDK response.
|
|
86
|
+
* @param { *data: [enrollmentEntities] } options
|
|
87
|
+
*/
|
|
88
|
+
function __convertEnrollmetsResponse(options) {
|
|
89
|
+
var enrollmentEntities = options.data;
|
|
90
|
+
|
|
91
|
+
//convert to SDK response.
|
|
92
|
+
for(var enrollmentIdx in enrollmentEntities) {
|
|
93
|
+
var enrollment = enrollmentEntities[enrollmentIdx];
|
|
94
|
+
var firstName = enrollment.first_name,
|
|
95
|
+
lastName = enrollment.last_name;
|
|
96
|
+
if(firstName || lastName) {
|
|
97
|
+
enrollment.name = firstName ? firstName : '';
|
|
98
|
+
|
|
99
|
+
if(lastName) {
|
|
100
|
+
enrollment.name += firstName ? (' ' + lastName) : lastName;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
enrollment.uuid = enrollment.dls_user_id;
|
|
105
|
+
|
|
106
|
+
if(enrollment.username) {
|
|
107
|
+
enrollment.ext_username = enrollment.username;
|
|
108
|
+
}
|
|
109
|
+
enrollment.username = enrollment.userid + '--ACCOUNT';
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
return enrollmentEntities;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
module.exports = {
|
|
116
|
+
convertUserResponse: __convertUserResponse,
|
|
117
|
+
convertClassResponse: __convertClassResponse,
|
|
118
|
+
convertEnrollmetsResponse: __convertEnrollmetsResponse
|
|
119
|
+
};
|
|
@@ -1,121 +1,121 @@
|
|
|
1
|
-
/*************************************************************************
|
|
2
|
-
*
|
|
3
|
-
* COMPRO CONFIDENTIAL
|
|
4
|
-
* __________________
|
|
5
|
-
*
|
|
6
|
-
* [2015] - [2020] Compro Technologies Private Limited
|
|
7
|
-
* All Rights Reserved.
|
|
8
|
-
*
|
|
9
|
-
* NOTICE: All information contained herein is, and remains
|
|
10
|
-
* the property of Compro Technologies Private Limited. The
|
|
11
|
-
* intellectual and technical concepts contained herein are
|
|
12
|
-
* proprietary to Compro Technologies Private Limited and may
|
|
13
|
-
* be covered by U.S. and Foreign Patents, patents in process,
|
|
14
|
-
* and are protected by trade secret or copyright law.
|
|
15
|
-
*
|
|
16
|
-
* Dissemination of this information or reproduction of this material
|
|
17
|
-
* is strictly forbidden unless prior written permission is obtained
|
|
18
|
-
* from Compro Technologies Pvt. Ltd..
|
|
19
|
-
***************************************************************************/
|
|
20
|
-
/***********************************************************
|
|
21
|
-
* comproDLS SDK Generic API Caller
|
|
22
|
-
* This module provides generic function(s) to call any comproDLS API.
|
|
23
|
-
* The generic approach for calling API(s) require a prior knowledge of
|
|
24
|
-
* the specific URLs and parameters (as per the API documentation).
|
|
25
|
-
************************************************************/
|
|
26
|
-
var request = require('superagent');
|
|
27
|
-
var q = require('q');
|
|
28
|
-
var format = require("string-template");
|
|
29
|
-
|
|
30
|
-
var config = require('../../../config');
|
|
31
|
-
var helpers = require('../../../helpers');
|
|
32
|
-
var errors = require('../errors');
|
|
33
|
-
var DLSError = errors.DLSError;
|
|
34
|
-
var validations = require('./validations');
|
|
35
|
-
|
|
36
|
-
/*********************************
|
|
37
|
-
* Setting up Exports/Public functions
|
|
38
|
-
**********************************/
|
|
39
|
-
exports.genericAPICaller = genericAPICaller;
|
|
40
|
-
exports.constructAPIUrl = constructAPIUrl;
|
|
41
|
-
exports.setupAPIToken = setupAPIToken;
|
|
42
|
-
exports.addClassIdQueryParam = addClassIdQueryParam
|
|
43
|
-
|
|
44
|
-
/*********************************
|
|
45
|
-
* Public Function definitions
|
|
46
|
-
**********************************/
|
|
47
|
-
|
|
48
|
-
//Generic comproDLS API caller
|
|
49
|
-
function genericAPICaller(apiName, method, url, params) {
|
|
50
|
-
var self = this;
|
|
51
|
-
var dfd = q.defer();
|
|
52
|
-
|
|
53
|
-
//Validation
|
|
54
|
-
var err = validations.genericAPICaller(apiName, method, url, params) || helpers.validations.isAuthenticated(self.orgId, self.token);
|
|
55
|
-
if (err) {
|
|
56
|
-
dfd.reject(err);
|
|
57
|
-
} else {
|
|
58
|
-
|
|
59
|
-
//Passed all validations, setup API Url and parameters
|
|
60
|
-
var url = config.DEFAULT_HOSTS[apiName.toUpperCase()] + url;
|
|
61
|
-
|
|
62
|
-
//Setup Delete method for superagent
|
|
63
|
-
if (method === 'DELETE') {
|
|
64
|
-
method = 'DEL';
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
var requestAPI = request(method, url);
|
|
68
|
-
|
|
69
|
-
//Setting token in Authorization header
|
|
70
|
-
requestAPI = setupAPIToken(requestAPI, self.token);
|
|
71
|
-
|
|
72
|
-
//Setup request parameters
|
|
73
|
-
if (Object.keys(params).length) {
|
|
74
|
-
if (method === 'GET' || method === 'HEAD') {
|
|
75
|
-
requestAPI = requestAPI.query(params);
|
|
76
|
-
} else {
|
|
77
|
-
requestAPI = requestAPI.send(params);
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
//Call Specified API
|
|
82
|
-
requestAPI.end(function(err, response) {
|
|
83
|
-
if (err) {
|
|
84
|
-
err = new DLSError(errors.ERROR_TYPES.API_ERROR, err);
|
|
85
|
-
dfd.reject(err);
|
|
86
|
-
} else {
|
|
87
|
-
dfd.resolve(response.body);
|
|
88
|
-
}
|
|
89
|
-
});
|
|
90
|
-
}
|
|
91
|
-
return dfd.promise;
|
|
92
|
-
};
|
|
93
|
-
|
|
94
|
-
/*********************************************************
|
|
95
|
-
* This function constructs the url of comproDLS API.
|
|
96
|
-
* Options: JSON Object having actual values to replace in API URLs
|
|
97
|
-
* Following are valid feilds in options object:
|
|
98
|
-
{
|
|
99
|
-
"orgId" : "",
|
|
100
|
-
"productId" : "",
|
|
101
|
-
"userId" : "",
|
|
102
|
-
"activityId" : "",
|
|
103
|
-
"attemptId" : "",
|
|
104
|
-
"questionId" : ""
|
|
105
|
-
}
|
|
106
|
-
*********************************************************/
|
|
107
|
-
function constructAPIUrl(url, options) {
|
|
108
|
-
return format(url, options);
|
|
109
|
-
};
|
|
110
|
-
|
|
111
|
-
function addClassIdQueryParam(url, classId) {
|
|
112
|
-
var queryParam = '';
|
|
113
|
-
if(typeof(classId) !== "undefined" && classId !== null)
|
|
114
|
-
queryParam = '?classId=' + classId;
|
|
115
|
-
return url + queryParam;
|
|
116
|
-
};
|
|
117
|
-
|
|
118
|
-
//This function sets up comproDLS API token in request header
|
|
119
|
-
function setupAPIToken(request, token) {
|
|
120
|
-
return request.set('Authorization', token.access_token);
|
|
1
|
+
/*************************************************************************
|
|
2
|
+
*
|
|
3
|
+
* COMPRO CONFIDENTIAL
|
|
4
|
+
* __________________
|
|
5
|
+
*
|
|
6
|
+
* [2015] - [2020] Compro Technologies Private Limited
|
|
7
|
+
* All Rights Reserved.
|
|
8
|
+
*
|
|
9
|
+
* NOTICE: All information contained herein is, and remains
|
|
10
|
+
* the property of Compro Technologies Private Limited. The
|
|
11
|
+
* intellectual and technical concepts contained herein are
|
|
12
|
+
* proprietary to Compro Technologies Private Limited and may
|
|
13
|
+
* be covered by U.S. and Foreign Patents, patents in process,
|
|
14
|
+
* and are protected by trade secret or copyright law.
|
|
15
|
+
*
|
|
16
|
+
* Dissemination of this information or reproduction of this material
|
|
17
|
+
* is strictly forbidden unless prior written permission is obtained
|
|
18
|
+
* from Compro Technologies Pvt. Ltd..
|
|
19
|
+
***************************************************************************/
|
|
20
|
+
/***********************************************************
|
|
21
|
+
* comproDLS SDK Generic API Caller
|
|
22
|
+
* This module provides generic function(s) to call any comproDLS API.
|
|
23
|
+
* The generic approach for calling API(s) require a prior knowledge of
|
|
24
|
+
* the specific URLs and parameters (as per the API documentation).
|
|
25
|
+
************************************************************/
|
|
26
|
+
var request = require('superagent');
|
|
27
|
+
var q = require('q');
|
|
28
|
+
var format = require("string-template");
|
|
29
|
+
|
|
30
|
+
var config = require('../../../config');
|
|
31
|
+
var helpers = require('../../../helpers');
|
|
32
|
+
var errors = require('../errors');
|
|
33
|
+
var DLSError = errors.DLSError;
|
|
34
|
+
var validations = require('./validations');
|
|
35
|
+
|
|
36
|
+
/*********************************
|
|
37
|
+
* Setting up Exports/Public functions
|
|
38
|
+
**********************************/
|
|
39
|
+
exports.genericAPICaller = genericAPICaller;
|
|
40
|
+
exports.constructAPIUrl = constructAPIUrl;
|
|
41
|
+
exports.setupAPIToken = setupAPIToken;
|
|
42
|
+
exports.addClassIdQueryParam = addClassIdQueryParam
|
|
43
|
+
|
|
44
|
+
/*********************************
|
|
45
|
+
* Public Function definitions
|
|
46
|
+
**********************************/
|
|
47
|
+
|
|
48
|
+
//Generic comproDLS API caller
|
|
49
|
+
function genericAPICaller(apiName, method, url, params) {
|
|
50
|
+
var self = this;
|
|
51
|
+
var dfd = q.defer();
|
|
52
|
+
|
|
53
|
+
//Validation
|
|
54
|
+
var err = validations.genericAPICaller(apiName, method, url, params) || helpers.validations.isAuthenticated(self.orgId, self.token);
|
|
55
|
+
if (err) {
|
|
56
|
+
dfd.reject(err);
|
|
57
|
+
} else {
|
|
58
|
+
|
|
59
|
+
//Passed all validations, setup API Url and parameters
|
|
60
|
+
var url = config.DEFAULT_HOSTS[apiName.toUpperCase()] + url;
|
|
61
|
+
|
|
62
|
+
//Setup Delete method for superagent
|
|
63
|
+
if (method === 'DELETE') {
|
|
64
|
+
method = 'DEL';
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
var requestAPI = request(method, url);
|
|
68
|
+
|
|
69
|
+
//Setting token in Authorization header
|
|
70
|
+
requestAPI = setupAPIToken(requestAPI, self.token);
|
|
71
|
+
|
|
72
|
+
//Setup request parameters
|
|
73
|
+
if (Object.keys(params).length) {
|
|
74
|
+
if (method === 'GET' || method === 'HEAD') {
|
|
75
|
+
requestAPI = requestAPI.query(params);
|
|
76
|
+
} else {
|
|
77
|
+
requestAPI = requestAPI.send(params);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
//Call Specified API
|
|
82
|
+
requestAPI.end(function(err, response) {
|
|
83
|
+
if (err) {
|
|
84
|
+
err = new DLSError(errors.ERROR_TYPES.API_ERROR, err);
|
|
85
|
+
dfd.reject(err);
|
|
86
|
+
} else {
|
|
87
|
+
dfd.resolve(response.body);
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
return dfd.promise;
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
/*********************************************************
|
|
95
|
+
* This function constructs the url of comproDLS API.
|
|
96
|
+
* Options: JSON Object having actual values to replace in API URLs
|
|
97
|
+
* Following are valid feilds in options object:
|
|
98
|
+
{
|
|
99
|
+
"orgId" : "",
|
|
100
|
+
"productId" : "",
|
|
101
|
+
"userId" : "",
|
|
102
|
+
"activityId" : "",
|
|
103
|
+
"attemptId" : "",
|
|
104
|
+
"questionId" : ""
|
|
105
|
+
}
|
|
106
|
+
*********************************************************/
|
|
107
|
+
function constructAPIUrl(url, options) {
|
|
108
|
+
return format(url, options);
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
function addClassIdQueryParam(url, classId) {
|
|
112
|
+
var queryParam = '';
|
|
113
|
+
if(typeof(classId) !== "undefined" && classId !== null)
|
|
114
|
+
queryParam = '?classId=' + classId;
|
|
115
|
+
return url + queryParam;
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
//This function sets up comproDLS API token in request header
|
|
119
|
+
function setupAPIToken(request, token) {
|
|
120
|
+
return request.set('Authorization', token.access_token);
|
|
121
121
|
};
|