-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
69 lines (63 loc) Β· 1.66 KB
/
index.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
const axios = require("axios");
const AWS = require("aws-sdk").DynamoDB;
const uuid = require("uuid");
const myDocumentClient = new AWS.DocumentClient();
exports.handler = async (event) => {
let matches = await getUpcomingMatches();
if (matches === undefined) {
return {
statusCode: 500,
body: JSON.stringify("Internal Server Error"),
};
}
await addMatchesToDB(matches);
return {
statusCode: 200,
body: JSON.stringify("Successfully updated DynamoDB"),
};
};
const getUpcomingMatches = async () => {
const ODDS_API_KEY = process.env.ODDS_API_KEY;
const REGION = "au";
const SPORT_KEY = "upcoming";
const MARKET_TYPE = "h2h";
let responseFromOddsAPI;
try {
responseFromOddsAPI = await axios.get(
"https://api.the-odds-api.com/v3/odds",
{
params: {
"api_key": ODDS_API_KEY,
"sport": SPORT_KEY,
"region": REGION, // uk | us | eu | au
"mkt": MARKET_TYPE, // h2h | spreads | totals
},
}
);
if (responseFromOddsAPI.success == false) {
throw "Odds-Api server refused";
}
} catch (er) {
console.log("Error fetching data from odds API");
console.log(er.message);
return;
}
return JSON.stringify(responseFromOddsAPI.data.data);
};
const addMatchesToDB = async(matches) => {
if(matches === undefined) return;
const params = {
Item: {
"id": uuid.v1(),
"betsDate": Date.now(),
"matches": JSON.parse(matches),
},
"TableName": process.env.TABLE_NAME
}
try{
await myDocumentClient.put(params).promise();
} catch(error) {
console.log(`Could not post: ${error.stack}`);
throw error.stack;
}
};