summaryrefslogtreecommitdiff
path: root/graphs/js/notSoFast/notSoFast.js
blob: 113c8f624ad2fd2ec9426bb5e25a4eb12675acc6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
const axios = require("axios");

async function notSoFast(host, port) {
    let nbArticles = await axios.get(`http://${host}:${port}/articles`);

    nbArticles = nbArticles.data.message;

    const articles = [];

    if (0 + nbArticles === 0) {
        return articles;
    }

    let res = await axios.get(`http://${host}:${port}/articles/${0}`);

    articles.push(res.data);
    for (let i = 1; i < 0 + nbArticles; i++) {
        const delay = res.headers["x-ratelimit-reset"] * 1000 - Date.now();

        if (res.headers["x-ratelimit-remaining"] == 0) {
            await new Promise((oof) => setTimeout(oof, delay + 26));
        }

        res = await axios.get(`http://${host}:${port}/articles/${i}`);
        articles.push(res.data);
    }

    return articles;
}

module.exports = {
    notSoFast,
};