Skip to content

Commit

Permalink
Move API calls to the API file context
Browse files Browse the repository at this point in the history
  • Loading branch information
pedrobmarin committed May 30, 2021
1 parent 17a6ebe commit 057dc09
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 27 deletions.
35 changes: 33 additions & 2 deletions lib/api.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const axios = require('axios');
const sha1 = require('crypto-js/sha1');
const conf = require('./conf');

Expand Down Expand Up @@ -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,
};
29 changes: 4 additions & 25 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand All @@ -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}`);
Expand All @@ -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;
},
Expand Down

0 comments on commit 057dc09

Please sign in to comment.