-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
141 lines (132 loc) · 3.97 KB
/
app.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
const { response } = require("express");
const express = require("express");
const fetch = require("node-fetch");
const app = express();
require("dotenv").config();
const port = 5000;
const apiKey = process.env.API_KEY;
const PlayerModel = require("./PlayerModel");
// Retrieve data about the player and their battlelog
app.get("/playerData/:playerID", (req, res) => {
let playerData = {};
const playerID = req.params.playerID;
const handleError = (err) => console.log(err);
// Secure player data
fetch(`https://api.brawlstars.com/v1/players/%23${playerID}`, {
method: "GET",
headers: {
Accept: "application/json",
Authorization: "Bearer " + apiKey,
},
})
.then((response) => {
// Send 404 if player does not exist
// If that happens then rejected promise is returned and triggers catch block at the end
// All that does is console.log the error
if (response.ok) {
return response.json();
} else {
res.sendStatus(404);
return;
}
})
.then((data) => {
playerData = data;
// Secure battlelog data
fetch(
"https://api.brawlstars.com/v1/players/%23" + playerID + "/battlelog",
{
method: "GET",
headers: {
Accept: "application/json",
Authorization: "Bearer " + apiKey,
},
}
)
.then((response) => response.json())
.then((data) => {
let battleLogData = data;
let model = new PlayerModel.PlayerModel({
...playerData,
battles: battleLogData.items,
});
let response = {
...playerData,
trophiesOverTime: model.TrophiesOverTime(),
recentGameModes: model.RecentGameModes(),
winLose: model.NumberWins(),
recentBrawlers: model.RecentBrawlers(),
brawlerData: model.GetBrawlersTrophies(),
starPlayerCount: model.StarPlayerCount(),
...battleLogData
};
// Combine battlelog and player data and send back as single object
res.send(response);
})
.then(() => console.log("Request completed."))
.catch(handleError);
});
});
// Retrieve player battlelog data
app.get("/playerData/:playerID/battlelog", (req, res) => {
const playerID = req.params.playerID;
fetch("https://api.brawlstars.com/v1/players/%23" + playerID + "/battlelog", {
method: "GET",
headers: {
Accept: "application/json",
Authorization: "Bearer " + apiKey,
},
})
.then((response) => {
// Send 404 if player does not exist
// If that happens then rejected promise is returned and triggers catch block at the end
// All that does is console.log the error
if (response.ok) {
return response.json();
} else {
res.sendStatus(404);
return;
}
})
.then((data) => {
battlelog = data;
fetch("https://api.brawlstars.com/v1/players/%23" + playerID, {
method: "GET",
headers: {
Accept: "application/json",
Authorization: "Bearer " + apiKey,
},
})
.then((response) => response.json())
.then((data) => {
let model = new PlayerModel.PlayerModel({
...data,
battles: battlelog.items,
});
let response = {
battles: battlelog,
...data,
starPlayerCount: model.StarPlayerCount()
};
res.send(response);
});
});
});
// Club support
app.get("/clubData/:clubID", (req, res) => {
const clubID = req.params.clubID;
let club = {};
fetch(`https://api.brawlstars.com/v1/clubs/%23${clubID}`, {
method: "GET",
headers: {
Accept: "application/json",
Authorization: "Bearer " + apiKey,
},
})
.then((response) => response.json())
.then((data) => {
club = data;
res.send(club);
});
});
app.listen(port, () => console.log(`Listening on port ${port}`));