-
Notifications
You must be signed in to change notification settings - Fork 1
/
routes.js
78 lines (60 loc) · 2.43 KB
/
routes.js
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
const api = require('express').Router();
const comics = require('./mocks/comics/comics-mock.json');
const axios = require('axios');
const md5 = require('md5');
const dotenv = require('dotenv');
dotenv.config();
module.exports = () => {
api.post('/comics', (req, res) => {
const BASE_URL = 'http://gateway.marvel.com/v1/public/comics?';
const limit = `limit=${req.body.limit}`;
const TIME_STAMP = Date.now();
const API_KEY = process.env.API_KEY;
const PRIVATE_KEY = process.env.PRIVATE_KEY;
const mixin = TIME_STAMP + PRIVATE_KEY + API_KEY;
const HASH = md5(mixin);
let comicsArray = [];
axios.get(`${BASE_URL}${limit}&ts=${TIME_STAMP}&apikey=${API_KEY}&hash=${HASH}`)
.then(response => {
const results = response.data.data.results;
results.forEach((hq) =>{
let title = hq.title;
title = title.slice(0,11);
title = title + '...';
const obj = {
'id':hq.id,
'title': title,
'name': hq.title,
'thumb': hq.thumbnail.path + "." + hq.thumbnail.extension
}
comicsArray.push(obj);
});
res.json(comicsArray);
})
.catch((err) => console.log(`O erro é:${err}`));
})
api.post('/details', (req, res) => {
const comicId = req.body.id;
const BASE_URL = 'http://gateway.marvel.com/v1/public/comics/';
const TIME_STAMP = Date.now();
const API_KEY = process.env.API_KEY;
const PRIVATE_KEY = process.env.PRIVATE_KEY;
const mixin = TIME_STAMP + PRIVATE_KEY + API_KEY;
const HASH = md5(mixin);
let comicDetails = [];
axios.get(`${BASE_URL}${comicId}?ts=${TIME_STAMP}&apikey=${API_KEY}&hash=${HASH}`)
.then(response => {
const comic = response.data.data.results[0];
const obj = {
"id" : comic.id,
"title": comic.title,
"description": comic.description,
"thumb": `${comic.thumbnail.path}.${comic.thumbnail.extension}`
}
comicDetails.push(obj);
res.json(comicDetails);
})
.catch((err) => console.log(`O erro é:${err}`));
})
return api;
}