@@ -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
8482function _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}
0 commit comments