Last active
September 20, 2016 14:30
-
-
Save maksis/f498208401d5bd933dc4eaf41a9685cd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fetch = require('node-fetch'); | |
const SESSION_IDLE_TIMEOUT = 60; // minutes | |
// USAGE: | |
// | |
// const RestApiConnector = require('./RestApiConnector'); | |
// const api = RestApiConnector({ | |
// username: 'test', | |
// password: 'test', | |
// }); | |
// | |
// api.connect().then(res => { | |
// (initialize) | |
// ); | |
module.exports = (userOptions) => { | |
// INIT | |
const options = Object.assign({}, { | |
url: 'localhost:5600', | |
secure: false, | |
}, userOptions); | |
let authToken = null; | |
const baseUrl = (options.secure ? 'https://' : 'http://') + options.url + '/api/'; | |
// INTERNAL HELPERS | |
const getHeaders = () => { | |
return { | |
'Accept': 'application/json', | |
'Content-Type': 'application/json', | |
'Authorization': authToken, | |
} | |
}; | |
const sendRequest = (method, path, data) => { | |
return new Promise((resolve, reject) => { | |
console.log(method, path, data ? data : ''); | |
fetch(baseUrl + path, { | |
method, | |
headers: getHeaders(), | |
body: JSON.stringify(data), | |
}) | |
.then(res => { | |
if (res.status >= 200 && res.status < 300) { | |
console.log('SUCCESS', method, path); | |
res.json().then(data => resolve(data)); | |
} else { | |
res.json().then(error => { | |
console.warn('API ERROR', method, path, res.status, error.message, error.field ? error.field : ''); | |
reject({ | |
message: error.message, | |
code: res.status, | |
json: error | |
}); | |
}); | |
} | |
}) | |
.catch(error => { | |
console.log('NETWORK ERROR', method, path, error.code, error.message); | |
reject(error); | |
}); | |
}); | |
}; | |
const updateActivity = () => { | |
post('session/v0/activity') | |
.catch(() => console.log('Failed to update activity')); | |
}; | |
// EXPORTED | |
const get = (path) => { | |
return sendRequest('GET', path); | |
}; | |
const deleteImpl = (path) => { | |
return sendRequest('DELETE', path); | |
}; | |
const post = (path, data) => { | |
return sendRequest('POST', path, data); | |
}; | |
const put = (path, data) => { | |
return sendRequest('PUT', path, data); | |
}; | |
const patch = (path, data) => { | |
return sendRequest('PATCH', path, data); | |
}; | |
const connect = () => { | |
return new Promise((resolve, reject) => { | |
post('session/v0/auth', { | |
username: options.username, | |
password: options.password, | |
max_inactivity: SESSION_IDLE_TIMEOUT, | |
}) | |
.then(res => { | |
console.log('Authentication completed'); | |
authToken = res.token; | |
// Don't let the session expire | |
setInterval(updateActivity, (SESSION_IDLE_TIMEOUT - 1) * 60 * 1000); | |
resolve(res); | |
}) | |
.catch(error => { | |
console.log('Authentication failed'); | |
reject(error); | |
}); | |
}); | |
}; | |
return { | |
get, | |
delete: deleteImpl, | |
post, | |
put, | |
patch, | |
connect, | |
}; | |
}; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment