-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
114 lines (94 loc) · 3.02 KB
/
script.js
File metadata and controls
114 lines (94 loc) · 3.02 KB
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
const output = document.getElementById('output');
const statusPill = document.getElementById('statusPill');
const apiBaseInput = document.getElementById('apiBase');
const apiBasePreview = document.getElementById('apiBasePreview');
function baseUrl() {
return apiBaseInput.value.trim().replace(/\/$/, '');
}
function apiHeaders() {
const headers = { 'Content-Type': 'application/json' };
const key = document.getElementById('apiKey').value.trim();
if (key) headers['X-API-Key'] = key;
return headers;
}
function setStatus(text, kind = '') {
statusPill.textContent = text;
statusPill.className = 'status-pill';
if (kind === 'ok') statusPill.classList.add('status-pill--ok');
if (kind === 'error') statusPill.classList.add('status-pill--error');
}
function render(data) {
output.textContent = typeof data === 'string' ? data : JSON.stringify(data, null, 2);
}
async function request(path, options = {}) {
const url = `${baseUrl()}${path}`;
render(`Requesting ${url} ...`);
setStatus('Loading');
try {
const response = await fetch(url, {
...options,
headers: {
...apiHeaders(),
...(options.headers || {}),
},
});
const text = await response.text();
let parsed;
try {
parsed = text ? JSON.parse(text) : {};
} catch {
parsed = { raw: text };
}
const fullPayload = {
url,
method: options.method || 'GET',
status: response.status,
ok: response.ok,
data: parsed,
};
render(fullPayload);
setStatus(response.ok ? 'Success' : `Error ${response.status}`, response.ok ? 'ok' : 'error');
} catch (error) {
render({
url,
error: error.message,
note: 'If this is a CORS error, allow your GitHub Pages domain in the backend or add CORSMiddleware in FastAPI.',
});
setStatus('Request failed', 'error');
}
}
document.getElementById('healthBtn').addEventListener('click', () => {
request('/healthz');
});
document.getElementById('oddsBtn').addEventListener('click', () => {
const sport = document.getElementById('sport').value;
const windowMinutes = document.getElementById('windowMinutes').value || '240';
const books = document.getElementById('books').value.trim();
const params = new URLSearchParams({
sport,
window_minutes: windowMinutes,
});
if (books) params.set('books', books);
request(`/odds?${params.toString()}`);
});
document.getElementById('parlaysBtn').addEventListener('click', () => {
request('/ev_parlays', {
method: 'POST',
body: JSON.stringify({}),
});
});
document.getElementById('logBtn').addEventListener('click', () => {
request('/log_result', {
method: 'POST',
body: JSON.stringify({ source: 'github-pages-demo', timestamp: new Date().toISOString() }),
});
});
document.getElementById('clearOutputBtn').addEventListener('click', () => {
render('Ready.');
setStatus('Idle');
});
apiBaseInput.addEventListener('input', () => {
apiBasePreview.textContent = baseUrl() || 'Set your backend URL';
});
render('Ready.');
setStatus('Idle');