Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import fetch from 'node-fetch';
- const MESSAGES_TO_SEND = 20;
- const CHANNEL_ID = 'bd89eb93-9d5c-49d7-bb18-7f1d750247e8'; // #Kanal 2
- const USER = { username: 'admin', password: 'Sommer2016!' };
- const URL = 'http://192.168.0.50/Feed';
- let accessToken = '';
- const handleError = res => {
- if (res.status >= 200 && res.status < 300) {
- return res;
- } else {
- const error = new Error(res.statusText);
- error.response = res;
- throw error;
- }
- };
- const parseJson = res => {
- switch (res.status) {
- case 204:
- return [];
- default:
- return res.json();
- }
- };
- fetch(`${URL}/users/login`, {
- method: 'post',
- headers: {
- 'Authorization': '',
- 'Content-Type': 'application/json'
- },
- body: JSON.stringify(USER)
- })
- .then(handleError)
- .then(parseJson)
- .then(res => accessToken = res.accessToken)
- .then(() => {
- const messagePromises = [];
- for (let i = 1; i < MESSAGES_TO_SEND+1; i++) {
- console.log(`Melding ${i} sendt.`); // eslint-disable-line no-console
- messagePromises.push(
- fetch(`${URL}/channels/${CHANNEL_ID}/entries`, {
- method: 'post',
- headers: {
- 'Authorization': `Bearer ${accessToken}`,
- 'Content-Type': 'application/json'
- },
- body: JSON.stringify({message: `Testmelding ${i}`})
- }).then(
- res => console.log(`Melding ${i}: ${res.status}`) // eslint-disable-line no-console
- )
- );
- }
- Promise.all(messagePromises).then(
- () => console.log('Ferdig!') // eslint-disable-line no-console
- );
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement