diff --git a/lib/api.js b/lib/api.js index 4ff2d45..4e5875e 100644 --- a/lib/api.js +++ b/lib/api.js @@ -1,3 +1,4 @@ +const axios = require('axios'); const sha1 = require('crypto-js/sha1'); const conf = require('./conf'); @@ -86,8 +87,38 @@ const getJoinURL = (username, options) => { return getURL('join', params, options); }; +const call = async (url) => { + let data = null; + await axios.get(url).then(response => { + if (response.data.response && response.data.response.returncode === 'FAILED') { + const { messageKey, message } = response.data.response; + logger.error(`${messageKey}: ${message}`); + } else { + data = response.data; + } + }).catch(error => { + logger.error(error); + }); + + return data; +}; + +const create = async (options) => { + const url = getCreateURL(options); + const data = await call(url); + + return data; +}; + +const end = async (options) => { + const url = getEndURL(options); + const data = await call(url); + + return data; +}; + module.exports = { - getCreateURL, - getEndURL, + create, + end, getJoinURL, }; diff --git a/lib/util.js b/lib/util.js index b52f0af..5c035fe 100644 --- a/lib/util.js +++ b/lib/util.js @@ -11,12 +11,6 @@ const delay = async time => new Promise(resolve => setTimeout(resolve, time)); const timestamp = () => Math.floor(new Date() / 1000); const random = collection => collection[Math.floor(Math.random() * collection.length)]; -const getAPIJoinURL = (username, options) => { - return api.getJoinURL(username, options); -}; - -const url = (username, options) => getAPIJoinURL(username, options); - const once = async (page, event, callback) => { return new Promise((resolve, reject) => { let fired = false; @@ -104,22 +98,6 @@ const translate = (locale, element) => { return selector; }; -const call = async (url) => { - let data = null; - await axios.get(url).then(response => { - if (response.data.response && response.data.response.returncode === 'FAILED') { - const { messageKey, message } = response.data.response; - logger.error(`${messageKey}: ${message}`); - } else { - data = response.data; - } - }).catch(error => { - logger.error(error); - }); - - return data; -}; - module.exports = { delay, random, @@ -130,7 +108,8 @@ module.exports = { logger.info(`${username}: join ${options.host || config.url.host} at ${options.room || config.url.meeting.name}`); const { width, height } = config.browser.window; await page.setViewport({ width, height }); - await page.goto(url(username, options)); + const url = api.getJoinURL(username, options); + await page.goto(url); const selector = translate(locale, conf.label.main.options.button); await page.waitForSelector(selector, { timeout: timeout.selector }); logger.debug(`${username}: notice ${selector}`); @@ -140,13 +119,13 @@ module.exports = { }, create: async (options) => { logger.info('Creating meeting'); - const data = await call(api.getCreateURL(options)); + const data = await api.create(options); return data !== null; }, end: async (options) => { logger.info('Ending meeting'); - const data = await call(api.getEndURL(options)); + const data = await api.end(options); return data !== null; },