backend: Limitation d'appel à l'api openweathermap.org pour la fonction weatherRequest

This commit is contained in:
Divlo 2020-03-17 22:14:25 +01:00
parent 27d6531a98
commit 6a90fc764d
4 changed files with 54 additions and 6 deletions

View File

@ -1,8 +1,24 @@
const axios = require('axios'); const axios = require('axios');
const Queue = require('smart-request-balancer');
const sendResponse = require('../../utils/sendResponse'); const sendResponse = require('../../utils/sendResponse');
const { requiredFields } = require('../../config/errors'); const { requiredFields } = require('../../config/errors');
const { WEATHER_API_KEY } = require('../../config/config'); const { WEATHER_API_KEY } = require('../../config/config');
const queue = new Queue({
/*
rate: number of requests
per
limit: number of seconds
*/
rules: {
weatherRequest: {
rate: 50,
limit: 60,
priority: 1
},
}
});
/* OUTPUTS */ /* OUTPUTS */
exports.weatherRequestOutput = async (res, argsObject) => { exports.weatherRequestOutput = async (res, argsObject) => {
let { cityName } = argsObject; let { cityName } = argsObject;
@ -12,8 +28,10 @@ exports.weatherRequestOutput = async (res, argsObject) => {
return sendResponse(res, requiredFields); return sendResponse(res, requiredFields);
} }
// Récupère les données météo grâce à l'API : openweathermap.org. // Récupère les données météo grâce à l'API : openweathermap.org. (→ avec limite de 50 requêtes par minute)
queue.request(() => {
axios.get(`https://api.openweathermap.org/data/2.5/weather?q=${cityName}&lang=fr&units=metric&appid=${WEATHER_API_KEY}`) axios.get(`https://api.openweathermap.org/data/2.5/weather?q=${cityName}&lang=fr&units=metric&appid=${WEATHER_API_KEY}`)
.then((response) => sendResponse(res, { result: response.data }, true)) .then((response) => sendResponse(res, { result: response.data }, true))
.catch(() => sendResponse(res, { result: "La ville n'existe pas (dans l'API de openweathermap.org)." })); .catch(() => sendResponse(res, { result: "La ville n'existe pas (dans l'API de openweathermap.org).", httpStatus: 404 }));
}, 'everyone', 'weatherRequest');
} }

View File

@ -4,7 +4,7 @@
* @param {Object} object { httpStatus, customProperties{Object}, result } * @param {Object} object { httpStatus, customProperties{Object}, result }
* @param {Boolean} isSuccess (false par defaut) * @param {Boolean} isSuccess (false par defaut)
*/ */
function sendResponse (res, object, isSuccess = false) { function sendResponse(res, object, isSuccess = false) {
res.status(object.httpStatus || 200).json({ isSuccess, ...object.customProperties, result: object.result }); res.status(object.httpStatus || 200).json({ isSuccess, ...object.customProperties, result: object.result });
} }

View File

@ -1219,6 +1219,30 @@
"integrity": "sha1-tf3AjxKH6hF4Yo5BXiUTK3NkbG0=", "integrity": "sha1-tf3AjxKH6hF4Yo5BXiUTK3NkbG0=",
"dev": true "dev": true
}, },
"smart-request-balancer": {
"version": "2.1.1",
"resolved": "https://registry.npmjs.org/smart-request-balancer/-/smart-request-balancer-2.1.1.tgz",
"integrity": "sha512-4AnMhV4amviBUGKeACOricuj4dhihf2P9Z/s3+cvZ3kjFxUwEV74JL9JTdK3/7Im84cC3J9ANeg+sUjSv0buxQ==",
"requires": {
"debug": "^4.1.1",
"uuid": "^3.3.2"
},
"dependencies": {
"debug": {
"version": "4.1.1",
"resolved": "https://registry.npmjs.org/debug/-/debug-4.1.1.tgz",
"integrity": "sha512-pYAIzeRo8J6KPEaJ0VWOh5Pzkbw/RetuzehGM7QRRX5he4fPHx2rdKMB256ehJCkX+XRQm16eZLqLNS8RSZXZw==",
"requires": {
"ms": "^2.1.1"
}
},
"ms": {
"version": "2.1.2",
"resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz",
"integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w=="
}
}
},
"statuses": { "statuses": {
"version": "1.5.0", "version": "1.5.0",
"resolved": "https://registry.npmjs.org/statuses/-/statuses-1.5.0.tgz", "resolved": "https://registry.npmjs.org/statuses/-/statuses-1.5.0.tgz",
@ -1372,6 +1396,11 @@
"resolved": "https://registry.npmjs.org/utils-merge/-/utils-merge-1.0.1.tgz", "resolved": "https://registry.npmjs.org/utils-merge/-/utils-merge-1.0.1.tgz",
"integrity": "sha1-n5VxD1CiZ5R7LMwSR0HBAoQn5xM=" "integrity": "sha1-n5VxD1CiZ5R7LMwSR0HBAoQn5xM="
}, },
"uuid": {
"version": "3.4.0",
"resolved": "https://registry.npmjs.org/uuid/-/uuid-3.4.0.tgz",
"integrity": "sha512-HjSDRw6gZE5JMggctHBcjVak08+KEVhSIiDzFnT9S9aegmp85S/bReBVTb4QTFaRNptJ9kuYaNhnbNEOkbKb/A=="
},
"vary": { "vary": {
"version": "1.1.2", "version": "1.1.2",
"resolved": "https://registry.npmjs.org/vary/-/vary-1.1.2.tgz", "resolved": "https://registry.npmjs.org/vary/-/vary-1.1.2.tgz",

View File

@ -13,7 +13,8 @@
"axios": "^0.19.2", "axios": "^0.19.2",
"cors": "^2.8.5", "cors": "^2.8.5",
"express": "^4.17.1", "express": "^4.17.1",
"helmet": "^3.21.3" "helmet": "^3.21.3",
"smart-request-balancer": "^2.1.1"
}, },
"devDependencies": { "devDependencies": {
"dotenv": "^8.2.0", "dotenv": "^8.2.0",