Skip to content

Commit ddbeec2

Browse files
[#186510319] Add Actions support (#41)
* Add Actions support * Upgrade dependencies * Bump versions * Convert request object property names to snake case
1 parent 623923f commit ddbeec2

File tree

7 files changed

+1227
-372
lines changed

7 files changed

+1227
-372
lines changed

lib/APIHelper.js

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,33 @@ var APIHelper = {
133133
}
134134
return input;
135135
},
136-
136+
137+
/**
138+
* Converts request object property names
139+
* for IP address and user agent to snake case.
140+
* @param {object} input Object.
141+
* @return {object} Returns the edited version of the object.
142+
*/
143+
snakeCase: function(input){
144+
function convert(model) {
145+
if (Object.hasOwn(model, "request")) {
146+
if (Object.hasOwn(model.request, "ipAddress")) {
147+
model.request["ip_address"] = model.request["ipAddress"]
148+
delete model.request["ipAddress"]
149+
}
150+
if (Object.hasOwn(model.request, "userAgentString")) {
151+
model.request["user_agent_string"] = model.request["userAgentString"]
152+
delete model.request["userAgentString"]
153+
}
154+
}
155+
}
156+
if (Array.isArray(input)) {
157+
input.forEach(model => convert(model))
158+
} else {
159+
convert(input)
160+
}
161+
return input
162+
},
137163

138164
/**
139165
* Shallow merges the properties of two objects

lib/Controllers/ApiController.js

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,8 +376,80 @@ var ApiController = {
376376
_options,
377377
buildCallback(callback)
378378
);
379-
}
379+
},
380+
381+
/**
382+
* Send a single action
383+
* @param {ActionModel} body Required parameter: Example:
384+
* @param {function} callback Required parameter: Callback function in the form of function(error, response)
385+
*
386+
* @return {void}
387+
*/
388+
sendAction: function (body, callback) {
389+
390+
//prepare query string for API call;
391+
var _baseUri = _configuration.BaseUri;
392+
393+
var _queryBuilder = _baseUri + "/v1/actions";
394+
395+
//validate and preprocess url
396+
var _queryUrl = _APIHelper.cleanUrl(_queryBuilder);
397+
398+
//Remove null values
399+
_APIHelper.cleanObject(body);
400+
//Convert request object property names to snake case
401+
_APIHelper.snakeCase(body);
402+
403+
//Construct the request
404+
var _options = {
405+
queryUrl: _queryUrl,
406+
method: "POST",
407+
headers: buildHeaders(),
408+
body: _APIHelper.jsonSerialize(body),
409+
};
410+
411+
console.log(_options.body)
412+
_request(
413+
_options,
414+
buildCallback(callback)
415+
);
416+
},
380417

418+
/**
419+
* Send multiple actions in a single batch (batch size must be less than 250kb)
420+
* @param {array} body Required parameter: Example:
421+
* @param {function} callback Required parameter: Callback function in the form of function(error, response)
422+
*
423+
* @return {void}
424+
*/
425+
sendActionsBatch: function (body, callback) {
426+
427+
//prepare query string for API call;
428+
var _baseUri = _configuration.BaseUri;
429+
430+
var _queryBuilder = _baseUri + "/v1/actions/batch";
431+
432+
//validate and preprocess url
433+
var _queryUrl = _APIHelper.cleanUrl(_queryBuilder);
434+
435+
//Remove null values
436+
_APIHelper.cleanObject(body);
437+
//Convert request object properties to snake case
438+
_APIHelper.snakeCase(body);
439+
440+
//Construct the request
441+
var _options = {
442+
queryUrl: _queryUrl,
443+
method: "POST",
444+
headers: buildHeaders(),
445+
body: _APIHelper.jsonSerialize(body),
446+
};
447+
448+
_request(
449+
_options,
450+
buildCallback(callback)
451+
);
452+
},
381453

382454
};
383455

lib/Models/ActionModel.js

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/**
2+
* MoesifAPILib
3+
*
4+
* transactionId: Option[String],
5+
* actionName: String,
6+
* sessionToken: Option[String],
7+
* userId: String,
8+
* companyId: Option[String],
9+
* metadata: Option[JsObject],
10+
* request: Option[JsObject]
11+
*/
12+
var BaseModel = require("./BaseModel");
13+
/**
14+
* Creates a instance of ActionModel
15+
*
16+
* @constructor
17+
*/
18+
var ActionModel = function (obj) {
19+
if (!obj) {
20+
this.transactionId = null;
21+
this.actionName = null;
22+
this.sessionToken = null;
23+
this.userId = null;
24+
this.companyId = null;
25+
this.metadata = null;
26+
this.request = null;
27+
} else {
28+
this.transactionId = obj.transactionId;
29+
this.actionName = obj.actionName;
30+
this.sessionToken = obj.sessionToken;
31+
this.userId = obj.userId;
32+
this.companyId = obj.companyId;
33+
this.metadata = obj.metadata;
34+
this.request = obj.request;
35+
}
36+
37+
// Append to variable dictionary
38+
this._variableDict["userId"] = "user_id";
39+
this._variableDict["companyId"] = "company_id";
40+
this._variableDict["sessionToken"] = "session_token";
41+
};
42+
43+
ActionModel.prototype = new BaseModel();
44+
ActionModel.prototype.constructor = ActionModel;
45+
46+
/**
47+
* The user Id
48+
*
49+
* @return {string}
50+
*/
51+
ActionModel.prototype.getUserId = function () {
52+
return this.userId;
53+
};
54+
55+
/**
56+
* Setter for the user Id
57+
*
58+
* @param {string} value
59+
*/
60+
ActionModel.prototype.setUserId = function (value) {
61+
this.userId = value;
62+
};
63+
64+
/**
65+
* The company Id
66+
*
67+
* @return {string|null}
68+
*/
69+
ActionModel.prototype.getCompanyId = function () {
70+
return this.companyId;
71+
};
72+
73+
/**
74+
* Setter for the company Id
75+
*
76+
* @param {string|null} value
77+
*/
78+
ActionModel.prototype.setCompanyId = function (value) {
79+
this.companyId = value;
80+
};
81+
82+
/**
83+
* Get the session token
84+
*
85+
* @return {string|null}
86+
*/
87+
ActionModel.prototype.getSessionToken = function () {
88+
return this.sessionToken;
89+
};
90+
91+
/**
92+
* Setter for the session token
93+
*
94+
* @param {string|null} value
95+
*/
96+
ActionModel.prototype.setSessionToken = function (value) {
97+
this.sessionToken = value;
98+
};
99+
100+
/**
101+
* Metadata in JSON.
102+
*
103+
* @return {object|null}
104+
*/
105+
ActionModel.prototype.getMetadata = function () {
106+
return this.metadata;
107+
};
108+
109+
/**
110+
* Setter for Metadata
111+
*
112+
* @param {object|null} value
113+
*/
114+
ActionModel.prototype.setMetadata = function (value) {
115+
this.metadata = value;
116+
};
117+
118+
module.exports = ActionModel;

lib/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ var configuration = require('./configuration'),
1414
UserModel = require('./Models/UserModel'),
1515
CompanyModel = require('./Models/CompanyModel'),
1616
CampaignModel = require('./Models/CampaignModel'),
17-
SubscriptionModel = require('./Models/SubscriptionModel')
17+
SubscriptionModel = require('./Models/SubscriptionModel'),
18+
ActionModel = require('./Models/ActionModel')
1819

1920

2021
function initializer(){}
@@ -33,5 +34,6 @@ initializer.SubscriptionModel = SubscriptionModel;
3334
initializer.CampaignModel = CampaignModel;
3435
initializer.EventResponseModel = EventResponseModel;
3536
initializer.StatusModel = StatusModel;
37+
initializer.ActionModel = ActionModel;
3638

3739
module.exports = initializer;

0 commit comments

Comments
 (0)