-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.js
61 lines (56 loc) · 1.4 KB
/
utils.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
const rp = require('request-promise');
const devUrl = 'https://ganglia-dev.machaao.com/';
const prodUrl = 'https://ganglia.machaao.com/';
const services = {
annouce: 'v1/messages/announce',
message: 'v1/messages/send',
tag: 'v1/users/tag/',
userProfile: 'v1/users/',
userTags: 'v1/users/tags/',
searchContent: 'v1/content/search/',
searchContentViaSlug: 'v1/content/',
};
async function post(env, payload, service, t, slug) {
env = env === 'dev' ? devUrl : prodUrl;
const url = slug ? `${env}${service}${slug}` : `${env}${service}`;
const options = {
method: 'POST',
uri: url,
json: payload,
headers: {
api_token: t,
'Content-Type': 'application/json',
},
transform: function (body, response) {
if (typeof body === 'string') {
response.body = JSON.parse(body);
return response.body;
} else return response.body;
},
};
return rp(options);
}
async function get(env, service, slug, t) {
env = env === 'dev' ? devUrl : prodUrl;
const url = slug ? `${env}${service}${slug}` : `${env}${service}`;
const options = {
method: 'GET',
uri: url,
headers: {
api_token: t,
'Content-Type': 'application/json',
},
transform: function (body, response) {
if (typeof body === 'string') {
response.body = JSON.parse(body);
return response.body;
} else return response.body;
},
};
return rp(options);
}
module.exports = {
services: services,
get: get,
post: post,
};