Skip to content

Commit e5ba3aa

Browse files
committed
performing the checks on connect instead
1 parent 465e6d8 commit e5ba3aa

File tree

2 files changed

+14
-11
lines changed

2 files changed

+14
-11
lines changed

connections.js

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@ function _setup (apis, keys, opts) {
2121
if (endpoint) {
2222
output[ endpoint.key ] = endpoint
2323
}
24-
if (opts.checkAPIs) {
25-
_checkAPI(endpoint, opts.log || defaultLog)
26-
}
24+
2725
})
2826
return output
2927
})
@@ -82,32 +80,37 @@ function _createClients (nodeApi, apiKey) {
8280

8381
// get connect to all configured api endpoints
8482
function _getPaths (endpoints, opts) {
83+
if (!opts.log) opts.log = defaultLog
84+
if (!opts.timeout) opts.timeout = defaultTimeout
8585
const tasks = endpoints.map((api) => { // for each endpoint
86-
return _connect(api, opts.log || defaultLog, opts.reconnectTimeout || defaultTimeout)
86+
return _connect(api, opts)
8787
})
8888
return Promise.all(tasks)
8989
}
9090

9191
// connect to an api endpoint and get _paths
92-
function _connect (api, log, timeout) {
92+
function _connect (api, opts) {
9393
const uri = `${api.config.proxyBasePath}/_paths` // get the proxyBasePath eg. api/publications
9494
return api.client.getAsync(uri) // return the api paths for the api
9595
.then((data) => {
9696
if (data.statusCode === 200) {
9797
api.paths = data.body.api
9898
api.connected = true
99-
log.info('Connected to api: ' + api.key)
99+
opts.log.info('Connected to api: ' + api.key)
100+
if (opts.checkAPIs) {
101+
_checkAPI(api, opts.log || defaultLog)
102+
}
100103
return api
101104
} else {
102105
throw new Error(data.statusCode + ' We can\'t access this API server. Check path and keys')
103106
}
104107
})
105108
.catch((err) => {
106-
log.error({ err: err }, 'Failed to get API paths from API: ', api.key, 'host: ', api.config.host, ' proxyBasePath: ', api.config.proxyBasePath)
109+
opts.log.error({ err: err }, 'Failed to get API paths from API: ', api.key, 'host: ', api.config.host, ' proxyBasePath: ', api.config.proxyBasePath)
107110
setTimeout(function () {
108-
log.info('Reconnecting to api: ' + api.key)
109-
_connect(api, log, timeout)
110-
}, timeout)
111+
opts.log.info('Reconnecting to api: ' + api.key)
112+
_connect(api, opts)
113+
}, opts.timeout)
111114
return api
112115
})
113116
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "kth-node-api-call",
3-
"version": "1.0.1",
3+
"version": "1.0.2",
44
"description": "Node.js module to make JSON calls against APIs.",
55
"main": "index.js",
66
"repository": {

0 commit comments

Comments
 (0)