From a5dd97dccbc2dcf3dc4ac7138af8406a58b9736c Mon Sep 17 00:00:00 2001 From: Mestima Date: Fri, 31 Mar 2023 21:39:51 +0300 Subject: [PATCH] created APIHandler, api methods are using APIHandler now --- src/api/cat/index.js | 7 ++----- src/api/fox/index.js | 7 ++----- src/commands/cat/index.js | 2 +- src/commands/fox/index.js | 2 +- src/handlers/APIHandler.js | 26 ++++++++++++++++++++++++++ src/~Luna.js | 5 +++-- 6 files changed, 35 insertions(+), 14 deletions(-) create mode 100644 src/handlers/APIHandler.js diff --git a/src/api/cat/index.js b/src/api/cat/index.js index aa2f097..94c6657 100644 --- a/src/api/cat/index.js +++ b/src/api/cat/index.js @@ -1,10 +1,7 @@ -import axios from 'axios'; import chalk from 'chalk'; -axios.defaults.timeout = 1000; - -const cat = async () => { - return await axios.get('https://aws.random.cat/meow') +const cat = async (httpClient) => { + return await httpClient.get('https://aws.random.cat/meow') .then((res) => { return res.data.file; }) diff --git a/src/api/fox/index.js b/src/api/fox/index.js index 656a00c..8fc3387 100644 --- a/src/api/fox/index.js +++ b/src/api/fox/index.js @@ -1,10 +1,7 @@ -import axios from 'axios'; import chalk from 'chalk'; -axios.defaults.timeout = 1000; - -const fox = async () => { - return await axios.get('https://randomfox.ca/floof/') +const fox = async (httpClient) => { + return await httpClient.get('https://randomfox.ca/floof/') .then((res) => { return res.data.image; }) diff --git a/src/commands/cat/index.js b/src/commands/cat/index.js index d54e0b2..80f570a 100644 --- a/src/commands/cat/index.js +++ b/src/commands/cat/index.js @@ -5,7 +5,7 @@ export default { .setName('cat') .setDescription('Get a random cat!'), async execute(interaction, api) { - await api.get('cat')() + await api.handle('cat') .then(async (res) => { await interaction.reply(res); }) diff --git a/src/commands/fox/index.js b/src/commands/fox/index.js index 48e7c2b..4505e64 100644 --- a/src/commands/fox/index.js +++ b/src/commands/fox/index.js @@ -5,7 +5,7 @@ export default { .setName('fox') .setDescription('Get a random fox!'), async execute(interaction, api) { - await api.get('fox')() + await api.handle('fox') .then(async (res) => { await interaction.reply(res); }) diff --git a/src/handlers/APIHandler.js b/src/handlers/APIHandler.js new file mode 100644 index 0000000..4956f2b --- /dev/null +++ b/src/handlers/APIHandler.js @@ -0,0 +1,26 @@ +import axios from 'axios'; +import APIService from '../services/APIService.js'; + +const APIHandler = class { + constructor() { + this.httpClient = axios.create(); + this.httpClient.defaults.timeout = 1000; + + this.APIService = new APIService(); + } + + async init() { + await this.APIService.init(); + return this; + } + + async handle(api) { + try { + return await this.APIService.get(api)(this.httpClient); + } catch (e) { + throw e; + } + } +}; + +export default APIHandler; diff --git a/src/~Luna.js b/src/~Luna.js index 78b1b6b..97d4d17 100644 --- a/src/~Luna.js +++ b/src/~Luna.js @@ -5,6 +5,7 @@ import pjson from '../package.json' assert { type: 'json' }; import InteractionHandler from './handlers/InteractionHandler.js'; import PresenceHandler from './handlers/PresenceHandler.js'; import APIService from './services/APIService.js'; +import APIHandler from './handlers/APIHandler.js'; config(); const TOKEN = process.env.TOKEN; @@ -24,7 +25,7 @@ const LunaInteractionHandler = await new InteractionHandler().init(TOKEN) console.log(`${chalk.red('Error:')} can not initialize InteractionHandler`) throw e; }); -const LunaAPIService = await new APIService().init(); +const LunaAPIHandler = await new APIHandler().init(); const Luna = new Discord.Client({ allowedMentions: { @@ -107,7 +108,7 @@ Luna.on(Discord.Events.ClientReady, async () => { }); Luna.on(Discord.Events.InteractionCreate, async (interaction) => { - await LunaInteractionHandler.handle(interaction, LunaAPIService); + await LunaInteractionHandler.handle(interaction, LunaAPIHandler); }); await Luna.login(TOKEN);