forked from kriskbx/gitlab-time-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbase.js
More file actions
executable file
·193 lines (171 loc) · 5.5 KB
/
base.js
File metadata and controls
executable file
·193 lines (171 loc) · 5.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
const request = require('request-promise-native');
const url = require('url');
const async = require('async');
const crypto = require('crypto');
/**
* base model
*/
class base {
/**
* construct
* @param config
*/
constructor(config) {
this.config = config;
this.url = config.get('url').endsWith('/') ? config.get('url') : `${config.get('url')}/`;
this.token = config.get('token');
this._perPage = this.config ? this.config.get('_perPage') : 100;
this._parallel = this.config ? this.config.get('_parallel') : 4;
this._proxy = this.config && this.config.get('proxy') ? this.config.get('proxy') : undefined;
this._insecure = this.config && this.config.get('unsecure') ? this.config.get('unsecure') : false;
}
/**
* query the given path
* @param path
* @param data
* @returns {*}
*/
post(path, data) {
let key = base.createDumpKey(path, data);
if (this.config.dump) return this.getDump(key);
data.private_token = this.token;
return new Promise((resolve, reject) => {
request.post(`${this.url}${path}`, {
json: true,
body: data,
insecure: this._insecure,
proxy: this._proxy,
resolveWithFullResponse: true,
headers: {
'PRIVATE-TOKEN': this.token
}
}).then(response => {
if (this.config.get('_createDump')) this.setDump(response, key);
resolve(response);
}).catch(e => reject(e));
});
}
/**
* query the given path
* @param path
* @param page
* @param perPage
* @returns {Promise}
*/
get(path, page = 1, perPage = this._perPage) {
let key = base.createDumpKey(path, page, perPage);
if (this.config.dump) return this.getDump(key);
path += (path.includes('?') ? '&' : '?') + `private_token=${this.token}`;
path += `&page=${page}&per_page=${perPage}`;
return new Promise((resolve, reject) => {
request(`${this.url}${path}`, {
json: true,
insecure: this._insecure,
proxy: this._proxy,
resolveWithFullResponse: true,
headers: {
'PRIVATE-TOKEN': this.token
}
}).then(response => {
if (this.config.get('_createDump')) this.setDump(response, key);
resolve(response);
}).catch(e => reject(e));
});
}
/**
* query the given path and paginate automatically and in parallel
* through all available pages
* @param path
* @param perPage
* @param runners
* @returns {Promise}
*/
all(path, perPage = this._perPage, runners = this._parallel) {
return new Promise((resolve, reject) => {
let collect = [];
this.get(path, 1, perPage).then(response => {
response.body.forEach(item => collect.push(item));
let pages = parseInt(response.headers['x-total-pages']);
if (pages === 1) return resolve(collect);
let tasks = base.createGetTasks(path, pages, 2, perPage);
this.getParallel(tasks, collect, runners).then(() => {
resolve(collect);
}).catch(error => reject(error));
}).catch(err => reject(err));
});
}
/**
* perform the given worker function on the given tasks in parallel
* @param tasks
* @param worker
* @param runners
* @returns {Promise}
*/
parallel(tasks, worker, runners = this._parallel) {
return new Promise((resolve, reject) => {
async.eachLimit(Array.from(tasks), runners, worker, error => {
if (error) return reject(error);
resolve();
});
});
}
/**
* make multiple get requests by the given tasks and apply the
* data to the given set
* @param tasks
* @param collect
* @param runners
* @returns {Promise}
*/
getParallel(tasks, collect = [], runners = this._parallel) {
return this.parallel(tasks, (task, done) => {
this.get(task.path, task.page, task.perPage).then((response) => {
response.body.forEach(item => collect.push(item));
done();
}).catch(error => done(error));
}, runners);
}
/**
* save the given response to dump
* @param response
* @param key
*/
setDump(response, key) {
this.config.setDump(key, {
headers: response.headers,
body: response.body
});
}
/**
* get from dump
* @param key
* @returns {Promise}
*/
getDump(key) {
return new Promise(r => r(this.config.getDump(key)));
}
/**
* create a task list to get all pages from
* the given path
* @param path
* @param to
* @param from
* @param perPage
* @returns {Array}
*/
static createGetTasks(path, to, from = 2, perPage = this._perPage) {
let tasks = [];
for (let i = from; i <= to; i++) {
tasks.push({path, perPage, page: i})
}
return tasks;
}
/**
* create a key representing a request
* @param args
*/
static createDumpKey(...args) {
return crypto.createHash('md5').update(JSON.stringify(args)).digest("hex");
}
}
module.exports = base;