zudello-integration-sdk 1.0.14 → 1.0.15

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zudello-integration-sdk",
3
- "version": "1.0.14",
3
+ "version": "1.0.15",
4
4
  "description": "Zudello Integrations SDK",
5
5
  "main": "./src/index.js",
6
6
  "repository": {
package/src/index.js CHANGED
@@ -22,6 +22,7 @@ const Trigger = require('./utils/trigger')
22
22
  const Properties = require('./utils/properties')
23
23
  const MiscHelper = require('./utils/miscHelper')
24
24
  const ModelHelper = require('./utils/modelHelper')
25
+ const DatasetHelper = require('./utils/datasetHelper')
25
26
  const {
26
27
  getFileContent,
27
28
  uploadFile,
@@ -60,5 +61,6 @@ module.exports = {
60
61
  Properties,
61
62
  MiscHelper,
62
63
  ModelHelper,
64
+ DatasetHelper,
63
65
  S3Client
64
66
  }
@@ -9,6 +9,7 @@ class ApiInstance {
9
9
  */
10
10
  constructor () {
11
11
  this.bearerToken = null
12
+ this.retryStatuses = [502, 504]
12
13
 
13
14
  this.responseHandler = new ResponseHandler()
14
15
 
@@ -39,7 +40,7 @@ class ApiInstance {
39
40
  * @param {object} headers Request Headers.
40
41
  * @returns {object} Response Data.
41
42
  */
42
- async get (url, params, headers, returnHeaders = false, callback = () => {}) {
43
+ async get (url, params, headers, returnHeaders = false, callback = () => {}, retryCount = 0) {
43
44
  try {
44
45
  const requestBody = { params, headers }
45
46
 
@@ -67,6 +68,11 @@ class ApiInstance {
67
68
  headers
68
69
  })
69
70
 
71
+ if (this.retryStatuses.includes(error?.response?.status) && retryCount === 0) {
72
+ await this.sleep(20000)
73
+ return await this.get(url, params, headers, returnHeaders, callback, retryCount + 1)
74
+ }
75
+
70
76
  if (error?.response?.status === 302) {
71
77
  return this.responseHandler.success(error?.response?.headers?.location)
72
78
  }
@@ -93,7 +99,7 @@ class ApiInstance {
93
99
  * @param {object} params Request Params.
94
100
  * @returns {object} Response Data.
95
101
  */
96
- async post (url, data, headers, params = {}, callback = () => {}) {
102
+ async post (url, data, headers, params = {}, callback = () => {}, retryCount = 0) {
97
103
  try {
98
104
  const response = await this.axiosInstance.post(url, data, { headers, params })
99
105
 
@@ -119,6 +125,11 @@ class ApiInstance {
119
125
  body: data
120
126
  })
121
127
 
128
+ if (this.retryStatuses.includes(error?.response?.status) && retryCount === 0) {
129
+ await this.sleep(20000)
130
+ return await this.post(url, data, headers, params, callback, retryCount + 1)
131
+ }
132
+
122
133
  if (error?.response?.status === 500) {
123
134
  return this.responseHandler.error(['Something went wrong.'])
124
135
  }
@@ -135,7 +146,7 @@ class ApiInstance {
135
146
  * @param {object} data PUT Data.
136
147
  * @returns {object} Response Data.
137
148
  */
138
- async put (url, data, headers, params = {}, callback = () => {}) {
149
+ async put (url, data, headers, params = {}, callback = () => {}, retryCount = 0) {
139
150
  try {
140
151
  const response = await this.axiosInstance.put(url, data, { headers, params })
141
152
 
@@ -157,6 +168,11 @@ class ApiInstance {
157
168
  body: data
158
169
  })
159
170
 
171
+ if (this.retryStatuses.includes(error?.response?.status) && retryCount === 0) {
172
+ await this.sleep(20000)
173
+ return await this.put(url, data, headers, params, callback, retryCount + 1)
174
+ }
175
+
160
176
  if (error?.response?.data) {
161
177
  return this.responseHandler.error([error?.response?.data])
162
178
  }
@@ -170,7 +186,7 @@ class ApiInstance {
170
186
  * @param {string} url Request URL.
171
187
  * @returns {object} Response Data.
172
188
  */
173
- async delete (url, callback = () => {}) {
189
+ async delete (url, callback = () => {}, retryCount = 0) {
174
190
  try {
175
191
  const response = await this.axiosInstance.delete(url)
176
192
 
@@ -186,6 +202,11 @@ class ApiInstance {
186
202
  response: error?.response?.data
187
203
  })
188
204
 
205
+ if (this.retryStatuses.includes(error?.response?.status) && retryCount === 0) {
206
+ await this.sleep(20000)
207
+ return await this.delete(url, callback, retryCount + 1)
208
+ }
209
+
189
210
  if (error?.response?.data) {
190
211
  return this.responseHandler.error([error?.response?.data])
191
212
  }
@@ -193,6 +214,10 @@ class ApiInstance {
193
214
  return this.responseHandler.error(['Something went wrong.'])
194
215
  }
195
216
  }
217
+
218
+ async sleep(ms) {
219
+ return new Promise(resolve => setTimeout(resolve, ms))
220
+ }
196
221
  }
197
222
 
198
223
  module.exports = ApiInstance
@@ -0,0 +1,81 @@
1
+ 'use strict'
2
+
3
+ const _ = require('lodash')
4
+
5
+ class DatasetHelper {
6
+ constructor(zudello, logger, data = null, uuid = null) {
7
+ this.zudello = zudello
8
+ this.logger = logger
9
+
10
+ this.data = data
11
+ this.uuid = uuid
12
+ }
13
+
14
+ async load(uuid, failCallback) {
15
+ failCallback = failCallback || ((data) => this.defaultFailCallback(data))
16
+
17
+ this.logger.info('DatasetUUID', uuid)
18
+
19
+ return await this.get(uuid, failCallback)
20
+ }
21
+
22
+ async get(uuid, failCallback) {
23
+ failCallback = failCallback || ((data) => this.defaultFailCallback(data))
24
+
25
+ if (!uuid) {
26
+ return failCallback({ uuid })
27
+ }
28
+
29
+ const fetchedDataset = await this.zudello.search({
30
+ model: 'DataSetRow',
31
+ offset: 0,
32
+ limit: 10000000,
33
+ select: ['uuid', 'custom'],
34
+ order_by: ['ordering'],
35
+ filter: {
36
+ dataset_uuid: uuid
37
+ }
38
+ })
39
+
40
+ this.logger.debug('Fetched Dataset', fetchedDataset)
41
+
42
+ if (fetchedDataset && fetchedDataset.data) {
43
+ const data = fetchedDataset.data?.data || null
44
+
45
+ if (data) {
46
+ return new DatasetHelper(this.zudello, this.logger, data, uuid)
47
+ }
48
+ }
49
+
50
+ return failCallback({ uuid })
51
+ }
52
+
53
+ length() {
54
+ return this.data ? this.data.length : 0
55
+ }
56
+
57
+ rows(mapping = null, filter = null) {
58
+ let data = this.data
59
+
60
+ if (!data) {
61
+ return []
62
+ }
63
+
64
+ if (filter) {
65
+ data = _.filter(data, filter)
66
+ }
67
+
68
+ if (mapping) {
69
+ data = _.map(data, mapping)
70
+ }
71
+
72
+ return data
73
+ }
74
+
75
+ defaultFailCallback(data) {
76
+ this.logger.error('Unable to find Dataset', data)
77
+ throw new Error('Unable to find Dataset')
78
+ }
79
+ }
80
+
81
+ module.exports = DatasetHelper