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
·144 lines (127 loc) · 3.95 KB
/
base.js
File metadata and controls
executable file
·144 lines (127 loc) · 3.95 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
const request = require('request-promise-native');
const url = require('url');
const async = require('async');
/**
* 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;
}
/**
* query the given path
* @param path
* @param data
* @returns {*}
*/
post(path, data) {
data.private_token = this.token;
return request.post(`${this.url}${path}`, {
json: true,
body: data,
proxy: this._proxy,
resolveWithFullResponse: true,
headers: {
'PRIVATE-TOKEN': this.token
}
});
}
/**
* query the given path
* @param path
* @param page
* @param perPage
* @returns {Promise}
*/
get(path, page = 1, perPage = this._perPage) {
path += (path.includes('?') ? '&' : '?') + `private_token=${this.token}`;
path += `&page=${page}&per_page=${perPage}`;
return request(`${this.url}${path}`, {
json: true,
proxy: this._proxy,
resolveWithFullResponse: true,
headers: {
'PRIVATE-TOKEN': this.token
}
});
}
/**
* 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);
}
/**
* 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;
}
}
module.exports = base;