From 658c676be8370af09b11ae6b5ae27ac5511f12b4 Mon Sep 17 00:00:00 2001 From: arianagiroux Date: Sun, 26 Jun 2022 19:15:59 -0600 Subject: [PATCH] Initial api access functionality --- .gitignore | 3 ++- index.html | 11 ++++++--- resources/script.js | 54 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+), 4 deletions(-) create mode 100644 resources/script.js diff --git a/.gitignore b/.gitignore index c03ba5e..09a6a8a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /bower_components/ -/node_modules/ \ No newline at end of file +/node_modules/ +resources/data.js \ No newline at end of file diff --git a/index.html b/index.html index f9f6b80..d18f666 100644 --- a/index.html +++ b/index.html @@ -13,9 +13,14 @@
-
+
+

Current Viewers: . Session Peak Viewers: .

+

Stream Online?

+
- + + + - + \ No newline at end of file diff --git a/resources/script.js b/resources/script.js new file mode 100644 index 0000000..4c61440 --- /dev/null +++ b/resources/script.js @@ -0,0 +1,54 @@ +const headers = new Headers(); +// note, userName and streamKey are both derived in a file called data.js +headers.append('Authorization', 'Basic ' + btoa(userName + ":" + streamKey)); + +async function getEndpoint(url = '') { + const response = await fetch(url, {method:'GET', headers: headers}) + const data = await response.json() + return data + } + + async function postEndpoint(url = '', data = {}) { + const response = await fetch(url, { + method: 'POST', + body: JSON.stringify(data), // serialized data + headers: headers // authentication header + }) + return response + } + + async function getStatus() { + return getEndpoint(api_url + 'admin/status') + } + + async function updateElements(data) { + var {online, viewerCount, sessionPeakViewerCount} = data + updateViewers(viewerCount); + updateOnline(online); + updateSessionPeak(sessionPeakViewerCount); + +} + +async function updateOnline(online) { + document.getElementById('online').innerHTML = online +} +async function updateViewers(viewers) { + document.getElementById('currentViewers').innerHTML = viewers +} +async function updateSessionPeak(viewers) { + document.getElementById('sessionPeak').innerHTML = viewers +} + +async function Main() { + // update visual elements using data from 'api/admin/status' + updateElements(await getStatus()) + + // update the broadcast title + // postEndpoint(`` + // api_url + 'admin/config/streamtitle', + // {value: "testing" + Math.floor(Math.random() * 1000)} + // ).then(response => response.status) + // .then(data => console.log(data)) +} + +Main() \ No newline at end of file