This repository has been archived by the owner on Nov 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapi.js
126 lines (116 loc) · 4.12 KB
/
api.js
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
'use strict';
let _ = require('highland');
let logger = require('./log')();
let util = require('./util')();
let http = require('./http')();
module.exports = function (opts) {
opts = opts || {};
opts.host = opts.host || process.env.VAMP_URL || 'http://127.0.0.1';
opts.path = opts.path || '/api/v1';
opts.cache = util.boolean(opts.cache || process.env.VAMP_API_CACHE || true);
opts.token = opts.token || process.env.VAMP_API_TOKEN || '';
opts.namespace = opts.namespace || process.env.VAMP_NAMESPACE || '';
opts.headers = opts.headers || {'Authorization': opts.token ? 'token ' + opts.token : 'token', 'Accept': 'application/json', 'Content-Type': 'application/json'};
logger.log('API options: ' + JSON.stringify(opts));
let cachedValues = {};
let cachedStreams = {};
return {
url: opts.host + (opts.namespace ? '/' + encodeURIComponent(opts.namespace) : '') + opts.path,
get: function (api, force, qs) {
api = api.charAt(0) === '/' ? api : '/' + api;
qs = qs ? '&' + qs : '';
let allowCache = opts.cache && !force;
if (cachedValues[api] && allowCache) {
logger.log('API GET ' + api + ' [CACHED]');
return _([cachedValues[api]]);
}
if (!cachedStreams[api] || !allowCache) {
logger.log('API GET ' + api);
let self = this;
let end = false;
let current = 1;
cachedStreams[api] = _(function (push, next) {
if (!end) {
push(null, current++);
next();
} else push(null, _.nil);
}).flatMap(function (page) {
return _(http.get(self.url + api + '?page=' + (page++) + qs, {headers: opts.headers}).then(JSON.parse).catch(() => {})).flatMap(function (response) {
if (response && response.constructor === Array) {
if (response.length === 0) end = true;
return _(response);
} else {
end = true;
return _([response]);
}
});
});
} else {
logger.log('API GET ' + api + ' [CACHED]');
}
return cachedStreams[api].fork().tap(function (value) {
cachedValues[api] = value;
});
},
put: function (api, json, qs) {
api = api.charAt(0) === '/' ? api : '/' + api;
qs = qs ? '?' + qs : '';
delete cachedValues[api];
delete cachedStreams[api];
logger.log('API PUT ' + api + qs);
return _(http.request(this.url + api + qs, {
method: 'PUT',
headers: opts.headers
}, JSON.stringify(json)));
},
post: function (api, json, qs) {
api = api.charAt(0) === '/' ? api : '/' + api;
qs = qs ? '?' + qs : '';
delete cachedValues[api];
delete cachedStreams[api];
logger.log('API POST ' + api + qs);
return _(http.request(this.url + api + qs, {
method: 'POST',
headers: opts.headers
}, JSON.stringify(json)));
},
delete: function (api, json, qs) {
api = api.charAt(0) === '/' ? api : '/' + api;
qs = qs ? '?' + qs : '';
delete cachedValues[api];
delete cachedStreams[api];
logger.log('API DELETE ' + api + qs);
return _(http.request(this.url + api + qs, {
method: 'DELETE',
headers: opts.headers
}, json ? JSON.stringify(json) : ''));
},
namify: function (artifacts) {
let result = [];
for (let name in artifacts) {
if (!artifacts.hasOwnProperty(name)) continue;
let artifact = artifacts[name];
artifact['name'] = name;
result.push(artifact);
}
return _(result);
},
info: function (force) {
return this.get('info', force);
},
config: function (force) {
return this.get('config', force, 'flatten=true');
},
gateways: function (force) {
return this.get('gateways', force);
},
deployments: function (force) {
return this.get('deployments', force);
},
event: function (tags, value, type) {
if (!type) type = 'event';
logger.log('API PUT /events ' + JSON.stringify({tags: tags, type: type}));
return _(this.post('/events', {tags: tags, value: value, type: type}));
}
};
};