-
Notifications
You must be signed in to change notification settings - Fork 4
/
api-sample.js
268 lines (257 loc) · 8.24 KB
/
api-sample.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
// Sample script to trigger Cloudbet Feed API at https://docs.cloudbet.com/
// Can be run using node with the command: `node api-sample.js`
"use strict";
const _ = require("lodash");
const got = require("got");
class Sample {
// Cloudbet Affiliate API: obtain from "API Key" section at https://affiliates.cloudbet.com/affiliate_api_token
// Cloudbet Trading API: Please contact Cloudbet Support to obtain your Trading API Key.
static apiKey = "<API Key>";
static apiBaseUrl = "https://sports-api.cloudbet.com";
static async *infoGen(type) {
while (true) {
// Make sure valid API Key is used
if (Sample.apiKey === "<API Key>") {
throw new Error("Use a valid API key and substitute into the script!");
}
try {
const sports = await this.getSports();
const competitions = _.sample(
await Promise.all(
sports.map((sport) => this.getCompetitionsForSport(sport.key))
)
);
const competition = _.sample(competitions); // pick random competition from random sport
switch (type) {
case "event":
const events = await this.getEventsForCompetition(competition);
const event = _.sample(events); // pick random event from competition
yield this.getEventInfo(event.id);
break;
case "competition":
const competitionWithEvents = await this.getEventsForCompetition(
competition
);
const eventsForCompetition = competitionWithEvents.reduce(
(list, event) => {
return [...list, { eventID: event.id, markets: event.markets }];
},
[]
);
yield eventsForCompetition;
break;
}
} catch (ex) {
continue;
}
}
}
// getSports: Get list of sports. Sports are ordered by alphabetical order.
// Sports are ordered by alphabetical order.
// This corresponds to the `/sports` endpoint at https://docs.cloudbet.com
static async getSports(sport) {
/*
Sample response:
{
"sports": [
{
"name": "American Football",
"key": "american-football",
"competitionCount": 2,
"eventCount": 92
},
{
"name": "Archery",
"key": "archery",
"competitionCount": 0,
"eventCount": 0
},
.
.
.
{
"name": "Soccer",
"key": "soccer",
"competitionCount": 142,
"eventCount": 1239
}
]
}
*/
let response;
try {
response = await got.get({
url: `${Sample.apiBaseUrl}/pub/v2/odds/sports`,
headers: { "X-API-Key": Sample.apiKey },
responseType: "json",
});
} catch (ex) {
throw new Error(`${ex.message}\nURL: ${ex.options.url.href}`);
}
if (sport) {
return response.body.sports.filter((sport) => sport.key === sport);
} else {
return response.body.sports.filter((sport) => sport.eventCount > 0);
}
}
// getCompetitionsForSport: Get competitions of a sport.
// Competitions are grouped by categories.
// This corresponds to the `/sports/{key}` endpoint at https://docs.cloudbet.com
static async getCompetitionsForSport(sportKey) {
/*
Sample response:
{
"name": "Soccer",
"key": "soccer",
"competitionCount": 141,
"eventCount": 1240,
"categories": [
{
"name": "England",
"key": "england",
"competitions": [
{
"name": "Premier League",
"key": "soccer-england-premier-league",
"eventCount": 29
},
{
"name": "FA Cup",
"key": "soccer-england-fa-cup",
"eventCount": 3
},
.
.
.
]
},
{
"name": "International",
"key": "international",
"competitions": [
{
"name": "UEFA Euro 2020",
"key": "soccer-international-euro-cup",
"eventCount": 146
},
{
"name": "World Cup",
"key": "soccer-international-world-cup",
"eventCount": 1
},
.
.
.
]
]
}
*/
let response;
try {
response = await got.get({
url: `${Sample.apiBaseUrl}/pub/v2/odds/sports/${sportKey}`,
headers: {
"X-API-Key": Sample.apiKey,
},
responseType: "json",
});
} catch (ex) {
throw new Error(`${ex.message}\nURL: ${ex.options.url.href}`);
}
const { categories } = response.body;
if (categories.length > 0) {
return categories.reduce(
(allCompetitions, { competitions }) =>
allCompetitions.concat(competitions),
[]
);
}
}
// getEventsForCompetition: Get live and upcoming events for given competition key.
// This corresponds to the `/competitions/{key}` endpoint at https://docs.cloudbet.com
static async getEventsForCompetition(competition) {
/*
Sample response.
This is a simplified sample of the response with only Event ID and markets. Refer to `api-responses.md` for detailed sample with all fields.
[
{
eventID: 4597460,
markets: {
'basketball.handicap': { submarkets: [Object], liability: 900 },
'basketball.moneyline': { submarkets: [Object], liability: 900 },
'basketball.totals': { submarkets: [Object], liability: 900 }
},
},
{
eventID: 4597461,
markets: {
'basketball.handicap': { submarkets: [Object], liability: 900 },
'basketball.moneyline': { submarkets: [Object], liability: 900 },
'basketball.totals': { submarkets: [Object], liability: 900 }
}
}
]
*/
let response;
if (!competition) {
throw new Error("Competition is not found!");
}
try {
response = await got.get({
url: `${Sample.apiBaseUrl}/pub/v2/odds/competitions/${competition.key}`,
headers: {
"X-API-Key": Sample.apiKey,
},
responseType: "json",
});
} catch (ex) {
throw new Error(`${ex.message}\nURL: ${ex.options.url.href}`);
}
if (response.body.events.length === 0) {
throw new Error("Events list is empty!");
} else {
return response.body.events;
}
}
// getEventInfo: Get event with markets for given event ID
// This corresponds to the `/events/{id}` endpoint at https://docs.cloudbet.com
static async getEventInfo(eventId) {
/*
Sample response (refer to `api-responses.md` for detailed sample).
This is a simplified sample of the response with only Event ID and markets. Refer to `api-responses.md` for detailed sample with all fields.
{
eventID: 4031413,
markets: {
'basketball.handicap': { submarkets: [Object], liability: 900 },
'basketball.moneyline': { submarkets: [Object], liability: 900 },
'basketball.totals': { submarkets: [Object], liability: 900 }
}
}
*/
let response;
try {
response = await got.get({
url: `${Sample.apiBaseUrl}/pub/v2/odds/events/${eventId}`,
headers: {
"X-API-Key": Sample.apiKey,
},
responseType: "json",
});
} catch (ex) {
throw new Error(`${ex.message}\nURL: ${ex.options.url.href}`);
}
return {
eventID: response.body.id,
markets: response.body.markets,
};
}
}
(async () => {
// Event sample response
const gen = await Sample.infoGen("event");
// Uncomment following and comment out line above if you want a sample competition response instead
// const gen = await Sample.infoGen("competition");
for await (let info of gen) {
console.log(JSON.stringify(info, null, 4));
}
})();