45 lines
1.5 KiB
JavaScript
Raw Normal View History

const BASE = '/api'
async function req(method, path, body) {
const opts = { method, headers: { 'Content-Type': 'application/json' } }
if (body) opts.body = JSON.stringify(body)
const r = await fetch(BASE + path, opts)
if (!r.ok) throw new Error(`${method} ${path} -> ${r.status}`)
return r.json()
}
export const api = {
health: () => req('GET', '/healthz'),
interfaces: () => req('GET', '/interfaces'),
mode: () => req('GET', '/mode'),
// Flows
flows: () => req('GET', '/flows'),
createFlow: (f) => req('POST', '/flows', f),
getFlow: (id) => req('GET', `/flows/${id}`),
updateFlow: (id, f) => req('PUT', `/flows/${id}`, f),
deleteFlow: (id) => req('DELETE', `/flows/${id}`),
startFlow: (id) => req('POST', `/flows/${id}/start`),
stopFlow: (id) => req('POST', `/flows/${id}/stop`),
flowStats: (id) => req('GET', `/flows/${id}/stats`),
// Tests
tests: () => req('GET', '/tests'),
createTest: (t) => req('POST', '/tests', t),
getTest: (id) => req('GET', `/tests/${id}`),
startTest: (id) => req('POST', `/tests/${id}/start`),
stopTest: (id) => req('POST', `/tests/${id}/stop`),
testResults: (id) => req('GET', `/tests/${id}/results`),
// Presets
presets: () => req('GET', '/presets'),
loadPreset: (name, overrides) => req('POST', `/presets/${name}`, overrides),
// Stats
statsHistory: () => req('GET', '/stats/history'),
// Responder
responderStats: () => req('GET', '/responder/stats'),
responderReset: () => req('POST', '/responder/reset'),
}