summaryrefslogtreecommitdiff
path: root/opendc-web/opendc-web-ui/src/api
diff options
context:
space:
mode:
Diffstat (limited to 'opendc-web/opendc-web-ui/src/api')
-rw-r--r--opendc-web/opendc-web-ui/src/api/index.js13
-rw-r--r--opendc-web/opendc-web-ui/src/api/routes/portfolios.js42
-rw-r--r--opendc-web/opendc-web-ui/src/api/routes/prefabs.js40
-rw-r--r--opendc-web/opendc-web-ui/src/api/routes/projects.js40
-rw-r--r--opendc-web/opendc-web-ui/src/api/routes/scenarios.js42
-rw-r--r--opendc-web/opendc-web-ui/src/api/routes/schedulers.js5
-rw-r--r--opendc-web/opendc-web-ui/src/api/routes/token-signin.js10
-rw-r--r--opendc-web/opendc-web-ui/src/api/routes/topologies.js42
-rw-r--r--opendc-web/opendc-web-ui/src/api/routes/traces.js5
-rw-r--r--opendc-web/opendc-web-ui/src/api/routes/users.js48
-rw-r--r--opendc-web/opendc-web-ui/src/api/routes/util.js37
-rw-r--r--opendc-web/opendc-web-ui/src/api/socket.js50
12 files changed, 374 insertions, 0 deletions
diff --git a/opendc-web/opendc-web-ui/src/api/index.js b/opendc-web/opendc-web-ui/src/api/index.js
new file mode 100644
index 00000000..cefcb2c5
--- /dev/null
+++ b/opendc-web/opendc-web-ui/src/api/index.js
@@ -0,0 +1,13 @@
+import { sendSocketRequest } from './socket'
+
+export function sendRequest(request) {
+ return new Promise((resolve, reject) => {
+ sendSocketRequest(request, (response) => {
+ if (response.status.code === 200) {
+ resolve(response.content)
+ } else {
+ reject(response)
+ }
+ })
+ })
+}
diff --git a/opendc-web/opendc-web-ui/src/api/routes/portfolios.js b/opendc-web/opendc-web-ui/src/api/routes/portfolios.js
new file mode 100644
index 00000000..7c9ea02a
--- /dev/null
+++ b/opendc-web/opendc-web-ui/src/api/routes/portfolios.js
@@ -0,0 +1,42 @@
+import { deleteById, getById } from './util'
+import { sendRequest } from '../index'
+
+export function addPortfolio(projectId, portfolio) {
+ return sendRequest({
+ path: '/projects/{projectId}/portfolios',
+ method: 'POST',
+ parameters: {
+ body: {
+ portfolio,
+ },
+ path: {
+ projectId,
+ },
+ query: {},
+ },
+ })
+}
+
+export function getPortfolio(portfolioId) {
+ return getById('/portfolios/{portfolioId}', { portfolioId })
+}
+
+export function updatePortfolio(portfolioId, portfolio) {
+ return sendRequest({
+ path: '/portfolios/{projectId}',
+ method: 'POST',
+ parameters: {
+ body: {
+ portfolio,
+ },
+ path: {
+ portfolioId,
+ },
+ query: {},
+ },
+ })
+}
+
+export function deletePortfolio(portfolioId) {
+ return deleteById('/portfolios/{portfolioId}', { portfolioId })
+}
diff --git a/opendc-web/opendc-web-ui/src/api/routes/prefabs.js b/opendc-web/opendc-web-ui/src/api/routes/prefabs.js
new file mode 100644
index 00000000..8a1debfa
--- /dev/null
+++ b/opendc-web/opendc-web-ui/src/api/routes/prefabs.js
@@ -0,0 +1,40 @@
+import { sendRequest } from '../index'
+import { deleteById, getById } from './util'
+
+export function getPrefab(prefabId) {
+ return getById('/prefabs/{prefabId}', { prefabId })
+}
+
+export function addPrefab(prefab) {
+ return sendRequest({
+ path: '/prefabs',
+ method: 'POST',
+ parameters: {
+ body: {
+ prefab,
+ },
+ path: {},
+ query: {},
+ },
+ })
+}
+
+export function updatePrefab(prefab) {
+ return sendRequest({
+ path: '/prefabs/{prefabId}',
+ method: 'PUT',
+ parameters: {
+ body: {
+ prefab,
+ },
+ path: {
+ prefabId: prefab._id,
+ },
+ query: {},
+ },
+ })
+}
+
+export function deletePrefab(prefabId) {
+ return deleteById('/prefabs/{prefabId}', { prefabId })
+}
diff --git a/opendc-web/opendc-web-ui/src/api/routes/projects.js b/opendc-web/opendc-web-ui/src/api/routes/projects.js
new file mode 100644
index 00000000..4109079c
--- /dev/null
+++ b/opendc-web/opendc-web-ui/src/api/routes/projects.js
@@ -0,0 +1,40 @@
+import { sendRequest } from '../index'
+import { deleteById, getById } from './util'
+
+export function getProject(projectId) {
+ return getById('/projects/{projectId}', { projectId })
+}
+
+export function addProject(project) {
+ return sendRequest({
+ path: '/projects',
+ method: 'POST',
+ parameters: {
+ body: {
+ project,
+ },
+ path: {},
+ query: {},
+ },
+ })
+}
+
+export function updateProject(project) {
+ return sendRequest({
+ path: '/projects/{projectId}',
+ method: 'PUT',
+ parameters: {
+ body: {
+ project,
+ },
+ path: {
+ projectId: project._id,
+ },
+ query: {},
+ },
+ })
+}
+
+export function deleteProject(projectId) {
+ return deleteById('/projects/{projectId}', { projectId })
+}
diff --git a/opendc-web/opendc-web-ui/src/api/routes/scenarios.js b/opendc-web/opendc-web-ui/src/api/routes/scenarios.js
new file mode 100644
index 00000000..ab2e8b86
--- /dev/null
+++ b/opendc-web/opendc-web-ui/src/api/routes/scenarios.js
@@ -0,0 +1,42 @@
+import { deleteById, getById } from './util'
+import { sendRequest } from '../index'
+
+export function addScenario(portfolioId, scenario) {
+ return sendRequest({
+ path: '/portfolios/{portfolioId}/scenarios',
+ method: 'POST',
+ parameters: {
+ body: {
+ scenario,
+ },
+ path: {
+ portfolioId,
+ },
+ query: {},
+ },
+ })
+}
+
+export function getScenario(scenarioId) {
+ return getById('/scenarios/{scenarioId}', { scenarioId })
+}
+
+export function updateScenario(scenarioId, scenario) {
+ return sendRequest({
+ path: '/scenarios/{projectId}',
+ method: 'POST',
+ parameters: {
+ body: {
+ scenario,
+ },
+ path: {
+ scenarioId,
+ },
+ query: {},
+ },
+ })
+}
+
+export function deleteScenario(scenarioId) {
+ return deleteById('/scenarios/{scenarioId}', { scenarioId })
+}
diff --git a/opendc-web/opendc-web-ui/src/api/routes/schedulers.js b/opendc-web/opendc-web-ui/src/api/routes/schedulers.js
new file mode 100644
index 00000000..4481fb2a
--- /dev/null
+++ b/opendc-web/opendc-web-ui/src/api/routes/schedulers.js
@@ -0,0 +1,5 @@
+import { getAll } from './util'
+
+export function getAllSchedulers() {
+ return getAll('/schedulers')
+}
diff --git a/opendc-web/opendc-web-ui/src/api/routes/token-signin.js b/opendc-web/opendc-web-ui/src/api/routes/token-signin.js
new file mode 100644
index 00000000..d6cff570
--- /dev/null
+++ b/opendc-web/opendc-web-ui/src/api/routes/token-signin.js
@@ -0,0 +1,10 @@
+export function performTokenSignIn(token) {
+ const apiUrl = process.env.REACT_APP_API_BASE_URL || ''
+
+ return fetch(`${apiUrl}/tokensignin`, {
+ method: 'POST',
+ body: new URLSearchParams({
+ idtoken: token,
+ }),
+ }).then((res) => res.json())
+}
diff --git a/opendc-web/opendc-web-ui/src/api/routes/topologies.js b/opendc-web/opendc-web-ui/src/api/routes/topologies.js
new file mode 100644
index 00000000..a8f0d6b1
--- /dev/null
+++ b/opendc-web/opendc-web-ui/src/api/routes/topologies.js
@@ -0,0 +1,42 @@
+import { deleteById, getById } from './util'
+import { sendRequest } from '../index'
+
+export function addTopology(topology) {
+ return sendRequest({
+ path: '/projects/{projectId}/topologies',
+ method: 'POST',
+ parameters: {
+ body: {
+ topology,
+ },
+ path: {
+ projectId: topology.projectId,
+ },
+ query: {},
+ },
+ })
+}
+
+export function getTopology(topologyId) {
+ return getById('/topologies/{topologyId}', { topologyId })
+}
+
+export function updateTopology(topology) {
+ return sendRequest({
+ path: '/topologies/{topologyId}',
+ method: 'PUT',
+ parameters: {
+ body: {
+ topology,
+ },
+ path: {
+ topologyId: topology._id,
+ },
+ query: {},
+ },
+ })
+}
+
+export function deleteTopology(topologyId) {
+ return deleteById('/topologies/{topologyId}', { topologyId })
+}
diff --git a/opendc-web/opendc-web-ui/src/api/routes/traces.js b/opendc-web/opendc-web-ui/src/api/routes/traces.js
new file mode 100644
index 00000000..67895a87
--- /dev/null
+++ b/opendc-web/opendc-web-ui/src/api/routes/traces.js
@@ -0,0 +1,5 @@
+import { getAll } from './util'
+
+export function getAllTraces() {
+ return getAll('/traces')
+}
diff --git a/opendc-web/opendc-web-ui/src/api/routes/users.js b/opendc-web/opendc-web-ui/src/api/routes/users.js
new file mode 100644
index 00000000..3028f3f7
--- /dev/null
+++ b/opendc-web/opendc-web-ui/src/api/routes/users.js
@@ -0,0 +1,48 @@
+import { sendRequest } from '../index'
+import { deleteById } from './util'
+
+export function getUserByEmail(email) {
+ return sendRequest({
+ path: '/users',
+ method: 'GET',
+ parameters: {
+ body: {},
+ path: {},
+ query: {
+ email,
+ },
+ },
+ })
+}
+
+export function addUser(user) {
+ return sendRequest({
+ path: '/users',
+ method: 'POST',
+ parameters: {
+ body: {
+ user,
+ },
+ path: {},
+ query: {},
+ },
+ })
+}
+
+export function getUser(userId) {
+ return sendRequest({
+ path: '/users/{userId}',
+ method: 'GET',
+ parameters: {
+ body: {},
+ path: {
+ userId,
+ },
+ query: {},
+ },
+ })
+}
+
+export function deleteUser(userId) {
+ return deleteById('/users/{userId}', { userId })
+}
diff --git a/opendc-web/opendc-web-ui/src/api/routes/util.js b/opendc-web/opendc-web-ui/src/api/routes/util.js
new file mode 100644
index 00000000..67e7173b
--- /dev/null
+++ b/opendc-web/opendc-web-ui/src/api/routes/util.js
@@ -0,0 +1,37 @@
+import { sendRequest } from '../index'
+
+export function getAll(path) {
+ return sendRequest({
+ path,
+ method: 'GET',
+ parameters: {
+ body: {},
+ path: {},
+ query: {},
+ },
+ })
+}
+
+export function getById(path, pathObject) {
+ return sendRequest({
+ path,
+ method: 'GET',
+ parameters: {
+ body: {},
+ path: pathObject,
+ query: {},
+ },
+ })
+}
+
+export function deleteById(path, pathObject) {
+ return sendRequest({
+ path,
+ method: 'DELETE',
+ parameters: {
+ body: {},
+ path: pathObject,
+ query: {},
+ },
+ })
+}
diff --git a/opendc-web/opendc-web-ui/src/api/socket.js b/opendc-web/opendc-web-ui/src/api/socket.js
new file mode 100644
index 00000000..1c432167
--- /dev/null
+++ b/opendc-web/opendc-web-ui/src/api/socket.js
@@ -0,0 +1,50 @@
+import io from 'socket.io-client'
+import { getAuthToken } from '../auth/index'
+
+let socket
+let requestIdCounter = 0
+const callbacks = {}
+
+export function setupSocketConnection(onConnect) {
+ const apiUrl =
+ process.env.REACT_APP_API_BASE_URL ||
+ `${window.location.protocol}//${window.location.hostname}:${window.location.port}`
+
+ socket = io.connect(apiUrl)
+ socket.on('connect', onConnect)
+ socket.on('response', onSocketResponse)
+}
+
+export function sendSocketRequest(request, callback) {
+ if (!socket.connected) {
+ console.error('Attempted to send request over unconnected socket')
+ return
+ }
+
+ const newId = requestIdCounter++
+ callbacks[newId] = callback
+
+ request.id = newId
+ request.token = getAuthToken()
+
+ if (!request.isRootRoute) {
+ request.path = '/v2' + request.path
+ }
+
+ socket.emit('request', request)
+
+ if (process.env.NODE_ENV !== 'production') {
+ console.log('Sent socket request:', request)
+ }
+}
+
+function onSocketResponse(json) {
+ const response = JSON.parse(json)
+
+ if (process.env.NODE_ENV !== 'production') {
+ console.log('Received socket response:', response)
+ }
+
+ callbacks[response.id](response)
+ delete callbacks[response.id]
+}