-
Notifications
You must be signed in to change notification settings - Fork 0
/
verifyCredentials.js
36 lines (30 loc) · 1.14 KB
/
verifyCredentials.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
"use strict";
const request = require('request-promise');
module.exports = verify;
/**
* Executes the verification logic by sending a simple to the Petstore API using the provided apiKey.
* If the request succeeds, we can assume that the apiKey is valid. Otherwise it is not valid.
*
* @param credentials object to retrieve apiKey from
*
* @returns Promise sending HTTP request and resolving its response
*/
function verify(credentials) {
// access the value of the apiKey field defined in credentials section of component.json
const apiKey = credentials.apiKey;
const apiBaseUrl = credentials.apiBaseUrl;
if (!apiKey) {
throw new Error('API key is missing');
}
const requestOptions = { // TODO add Content-Type if normal request fails
followAllRedirects: true,
url: apiBaseUrl + '/getCountries', // TODO - this needs to be updated with a keep alive / ping url - we don't have it now in prod
method: 'POST',
json: true,
body: {
'api_key': apiKey
}
};
// if the request succeeds, we can assume the api key is valid
return request(requestOptions);
}