forked from pantsel/konga
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.js
73 lines (65 loc) · 2.58 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
var unirest = require('unirest')
module.exports = {
enabled : true,
schema : {
"name": "API",
"value": "api",
"description": "Import Consumers by issuing a GET request to an API endpoint",
"form_fields": {
"connection": {
"endpoint": {
"name": "endpoint",
"required" : true,
"type": "text",
"description": "The API endpoint. ex: <code>http://myapi.io/users?limit=3000</code>"
},
"headers": {
"name": "headers",
"type": "text",
"description": "A comma separated list of headers to send with the request. ex: <code>Authorization:Bearer some-token-key,Content-type:application/json</code>"
}
},
"consumer": {
"data_property" : {
"name": "Data property",
"type": "text",
"description": "If specified, the consumers will be extracted from this resulting object's property."
},
"username": {
"name": "username field",
"type": "text",
"required": true,
"description": "The property that will be used as the consumer <code>username</code>."
},
"custom_id": {
"name": "custom_id field",
"type": "text",
"required": true,
"description": "The property will be used as the consumer <code>custom_id</code>."
}
}
}
},
methods : {
loadConsumers : function(req,res) {
var headers = {}
if(req.param('headers')) {
var string = req.param('headers')
var split = string.split(",")
split.forEach(function(keyVal){
var array = keyVal.split(":")
headers[array[0]] = array[1]
})
console.log("Headers =>",headers)
}
var request = unirest.get(req.param('endpoint'))
request.headers(headers)
request.end(function (response) {
if (response.error) return res.negotiate(response)
console.log("response.body",response.body)
var jsonRes = response.body
return res.json(req.param('data_property') ? jsonRes[req.param('data_property')] : jsonRes)
})
}
}
}