From b70116418c66610823de535dc935807cefef3222 Mon Sep 17 00:00:00 2001 From: lamya-rey <61109480+lamya-rey@users.noreply.github.com> Date: Wed, 17 Feb 2021 19:59:26 +0100 Subject: [PATCH] :sparkless: call 3 API --- index.js | 53 ++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 48 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index a282a8e..f5b9faf 100644 --- a/index.js +++ b/index.js @@ -4,11 +4,54 @@ import axios from 'axios'; const app = fastify({ logger: true }); -app.get('/', async (req, res) => { +//call CatFacts API +async function getCatFacts() { + try { + const response = await axios({ + url: 'https://cat-fact.herokuapp.com/facts/random?amount=3', + method: 'GET', + }); + return response.data.map(cat => cat.text) + } catch (err) { + return null; + } +} + +//call Fox API +async function getFoxImage() { + try { + const response = await axios({ + url: 'https://randomfox.ca/floof/', + method: 'GET', + }); + return response.data.image; + } catch (err) { + return null; + } +} + +//call PublicHolidays API +async function getDayOff(countryCode) { + try { + const response = await axios({ + url: `https://date.nager.at/api/v2/PublicHolidays/2021/${countryCode}`, + method: 'GET', + }); + return response.data; + } catch (err) { + return null; + } +} + +app.post('/', async (req, res) => { const countryCode = req.body.countryCode; + const catFacts = await getCatFacts(); + const foxImage = await getFoxImage(); + const dayOff = await getDayOff(countryCode); + return { - message: `Welcome to Node Babel with ${ - req.body?.testValue ?? 'no testValue' - }`, + catFacts: catFacts, + foxImage: foxImage, + dayOff: dayOff, }; }); @@ -21,4 +64,4 @@ const start = async () => { process.exit(1); } }; -start(); +start(); \ No newline at end of file