-
Notifications
You must be signed in to change notification settings - Fork 0
/
alma-api.js
60 lines (53 loc) · 1.92 KB
/
alma-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
// Author: Jean-Marie Thewes
//Modules:
const fetch = require("node-fetch-commonjs");
const httpsProxyAgent = require("https-proxy-agent");
const config = require("./config.js");
const log = require("./log.js");
//Global vars:
var almaApiKey;
var almaApiPath;
var proxyAgent;
//Functions:
async function initProxy(){
proxyAgent = new httpsProxyAgent(await config.getProxy());
}//------------------------------------------------------------------------------------------
async function request(method,request,body){
if(!["GET","POST"].includes(method)){
throw new Error ("First input for function 'request' must be 'GET' or 'POST'")
}
if(!almaApiKey){//check if API key has been set, if not get it from config
almaApiKey = await config.getApiKey();
}
if(!almaApiPath){//check if API path has been set, if not get it from config
almaApiPath = await config.getApiPath();
}
var proxyConfig = await config.getProxy();
var response;
var request = `${almaApiPath}${request}`;
var requestOptions = {
method:method,
headers:{
"Authorization":`apikey ${almaApiKey}`,
"Accept":"application/json"
}
}
if(proxyConfig){//add proxy settings if configured
if(!proxyAgent){await initProxy()}
requestOptions.agent = proxyAgent
}
if(method == "POST"){
let plainTextBody = JSON.stringify(body);
requestOptions.headers["Content-Type"] = "application/json";
requestOptions.body = plainTextBody;
}
response = await fetch(request,requestOptions)
log.debug(response);
if([200].includes(response.status)){//if status code is ok
return await response.json()
}else{
throw new Error(`API returned bad status: ${response.status}`)
}
}//------------------------------------------------------------------------------------------
//Exports:
module.exports={request:request}