-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMarvel.js
More file actions
20 lines (18 loc) · 722 Bytes
/
Copy pathMarvel.js
File metadata and controls
20 lines (18 loc) · 722 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
const axios = require('axios');
const crypto = require('crypto');
module.exports = class Marvel {
getUrl (endpoint) {
const timestamp = new Date().getTime();
const hash = crypto.createHash('md5').update(`${timestamp}${process.env.MARVEL_SECRET}${process.env.MARVEL_PUBLIC}`).digest("hex");
return `https://gateway.marvel.com${endpoint}?ts=${timestamp}&apikey=${process.env.MARVEL_PUBLIC}&hash=${hash}`;
}
getCharacters = async () => {
const url = this.getUrl('/v1/public/characters');
const response = await axios.get(url);
if (response.data.code === 200) {
return response.data.data.results;
} else {
throw Error(`Unknown code returned: ${response.data.code}`);
}
}
}