-
Notifications
You must be signed in to change notification settings - Fork 0
/
timr.js
60 lines (60 loc) · 2.04 KB
/
timr.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
const URI = require('urijs');
const request = require('request')
const nconf = require('nconf')
const logger = require('./winston');
const utils = require('./utils')
module.exports = {
getAccessToken: function (orchestrations, callback) {
let url = URI(nconf.get("oauth2:url"))
let before = new Date()
var auth = "Basic " + Buffer.from(nconf.get("oauth2:client_id") + ":" + nconf.get("oauth2:client_secret")).toString("base64");
var options = {
url: url.toString(),
headers: {
Authorization: auth
},
body: `grant_type=password&username=${nconf.get("oauth2:username")}&password=${nconf.get("oauth2:password")}&scope=${nconf.get("oauth2:fhirScope")}`
}
request.post(options, (err, res, body) => {
if (err) {
logger.error(err)
return callback(err)
}
orchestrations.push(utils.buildOrchestration('Getting Access Token From TImR', before, 'POST', url.toString(), options.body, res, body))
try {
body = JSON.parse(body)
} catch (error) {
logger.error(error)
return callback(error)
}
callback(err, res, body)
})
},
getData: (query, resource, access_token, orchestrations, callback) => {
return new Promise((resolve, reject) => {
let url = URI(nconf.get("timr:url")).segment(resource).addQuery('_format', 'json')
if (query) {
for (let index in query) {
url = url.addQuery(index, query[index])
}
}
let options = {
url: url.toString(),
headers: {
Authorization: `BEARER ${access_token}`
}
}
let before = new Date()
request.get(options, (err, res, body) => {
try {
body = JSON.parse(body)
} catch (error) {
logger.error(error)
return reject(error)
}
orchestrations.push(utils.buildOrchestration(`Getting ${resource} Data`, before, 'GET', nconf.get("timr:url").toString(), JSON.stringify(options.headers), res, body))
return resolve({err, res, body})
})
})
}
}