wip: prepare endpoints

This commit is contained in:
Carlos Valente
2023-11-22 22:30:31 +01:00
parent f39c8b46bb
commit cdca5eaad3
8 changed files with 73 additions and 58 deletions
+8 -11
View File
@@ -110,13 +110,18 @@ export async function getOSC(): Promise<OSCSettings> {
* @return {Promise} * @return {Promise}
*/ */
export async function getHTTP(): Promise<HTTPSettings> { export async function getHTTP(): Promise<HTTPSettings> {
console.log('getHTTP');
const res = await axios.get(`${ontimeURL}/http`); const res = await axios.get(`${ontimeURL}/http`);
console.log(res);
return res.data; return res.data;
} }
/**
* @description HTTP request to mutate http settings
* @return {Promise}
*/
export async function postHTTP(data: HTTPSettings) {
return axios.post(`${ontimeURL}/http`, data);
}
/** /**
* @description HTTP request to mutate osc settings * @description HTTP request to mutate osc settings
* @return {Promise} * @return {Promise}
@@ -133,14 +138,6 @@ export async function postOscSubscriptions(data: Subscription) {
return axios.post(`${ontimeURL}/osc-subscriptions`, data); return axios.post(`${ontimeURL}/osc-subscriptions`, data);
} }
/**
* @description HTTP request to mutate osc subscriptions
* @return {Promise}
*/
export async function postHttpSubscriptions(data: Subscription) {
return axios.post(`${ontimeURL}/http-subscriptions`, data);
}
/** /**
* @description HTTP request to download db in CSV format * @description HTTP request to download db in CSV format
*/ */
@@ -4,11 +4,11 @@ import { HTTPSettings } from 'ontime-types';
import { queryRefetchIntervalSlow } from '../../ontimeConfig'; import { queryRefetchIntervalSlow } from '../../ontimeConfig';
import { HTTP_SETTINGS } from '../api/apiConstants'; import { HTTP_SETTINGS } from '../api/apiConstants';
import { logAxiosError } from '../api/apiUtils'; import { logAxiosError } from '../api/apiUtils';
import { getHTTP, postHttpSubscriptions } from '../api/ontimeApi'; import { getHTTP, postHTTP } from '../api/ontimeApi';
import { httpPlaceholder } from '../models/Http'; import { httpPlaceholder } from '../models/Http';
import { ontimeQueryClient } from '../queryClient'; import { ontimeQueryClient } from '../queryClient';
export default function useHttpSettings() { export function useHttpSettings() {
const { data, status, isFetching, isError, refetch } = useQuery({ const { data, status, isFetching, isError, refetch } = useQuery({
queryKey: HTTP_SETTINGS, queryKey: HTTP_SETTINGS,
queryFn: getHTTP, queryFn: getHTTP,
@@ -23,12 +23,11 @@ export default function useHttpSettings() {
return { data: data! as unknown as HTTPSettings, status, isFetching, isError, refetch }; return { data: data! as unknown as HTTPSettings, status, isFetching, isError, refetch };
} }
export function usePostHttpSettings() {
export function usePostHttpSubscriptions() { const { isPending, mutateAsync } = useMutation({
const { isLoading, mutateAsync } = useMutation({ mutationFn: postHTTP,
mutationFn: postHttpSubscriptions,
onError: (error) => logAxiosError('Error saving HTTP settings', error), onError: (error) => logAxiosError('Error saving HTTP settings', error),
onSettled: () => ontimeQueryClient.invalidateQueries({ queryKey: HTTP_SETTINGS }), onSettled: () => ontimeQueryClient.invalidateQueries({ queryKey: HTTP_SETTINGS }),
}); });
return { isLoading, mutateAsync }; return { isPending, mutateAsync };
} }
@@ -31,7 +31,7 @@ export default function IntegrationModal(props: IntegrationModalProps) {
<TabList> <TabList>
<Tab>OSC</Tab> <Tab>OSC</Tab>
<Tab>OSC Integration</Tab> <Tab>OSC Integration</Tab>
<Tab>HTML Integration</Tab> <Tab>HTTP Integration</Tab>
</TabList> </TabList>
<TabPanels> <TabPanels>
<TabPanel> <TabPanel>
+39 -33
View File
@@ -1,4 +1,5 @@
import { Alias, DatabaseModel, GetInfo, LogOrigin, ProjectData } from 'ontime-types'; import { LogOrigin } from 'ontime-types';
import type { Alias, DatabaseModel, GetInfo, HTTPSettings, ProjectData } from 'ontime-types';
import { RequestHandler, Request, Response } from 'express'; import { RequestHandler, Request, Response } from 'express';
import fs from 'fs'; import fs from 'fs';
@@ -284,11 +285,25 @@ export const getOSC = async (req, res) => {
res.status(200).send(osc); res.status(200).send(osc);
}; };
// Create controller for GET request to '/ontime/http' // Create controller for POST request to '/ontime/osc'
// Returns - // Returns ACK message
export const getHTTP = async (req, res) => { export const postOSC = async (req, res) => {
const http = DataProvider.getHttp(); if (failEmptyObjects(req.body, res)) {
res.status(200).send(http); return;
}
try {
const oscSettings = req.body;
await DataProvider.setOsc(oscSettings);
// TODO: this update could be more granular, checking that relevant data was changed
const { message } = oscIntegration.init(oscSettings);
logger.info(LogOrigin.Tx, message);
res.send(oscSettings).status(200);
} catch (error) {
res.status(400).send({ message: error.toString() });
}
}; };
export const postOscSubscriptions = async (req, res) => { export const postOscSubscriptions = async (req, res) => {
@@ -312,42 +327,33 @@ export const postOscSubscriptions = async (req, res) => {
} }
}; };
export const postHttpSubscriptions = async (req, res) => { // Create controller for GET request to '/ontime/http'
if (failEmptyObjects(req.body, res)) { export const getHTTP = async (req, res: Response<HTTPSettings>) => {
return; const http = DataProvider.getHttp();
} res.status(200).send(http);
try {
const subscriptions = req.body;
const httpSettings = DataProvider.getHttp();
httpSettings.subscriptions = subscriptions;
await DataProvider.setHttp(httpSettings);
const { message } = httpIntegration.init(httpSettings);
logger.info(LogOrigin.Tx, message);
res.send(httpSettings).status(200);
} catch (error) {
res.status(400).send(error);
}
}; };
// Create controller for POST request to '/ontime/osc' // Create controller for POST request to '/ontime/http'
// Returns ACK message export const postHTTP = async (req: Request<any, any, HTTPSettings>, res: Response) => {
export const postOSC = async (req, res) => {
if (failEmptyObjects(req.body, res)) { if (failEmptyObjects(req.body, res)) {
return; return;
} }
try { try {
const oscSettings = req.body; const settings = req.body;
await DataProvider.setOsc(oscSettings); const httpSettings = DataProvider.getHttp();
const hasEnabledChanged = httpSettings.enabledOut !== settings.enabledOut;
// TODO: this update could be more granular, checking that relevant data was changed httpSettings.subscriptions = settings.subscriptions;
const { message } = oscIntegration.init(oscSettings); httpSettings.enabledOut = settings.enabledOut;
logger.info(LogOrigin.Tx, message); await DataProvider.setHttp(httpSettings);
res.send(oscSettings).status(200); if (hasEnabledChanged) {
const { message } = httpIntegration.init(httpSettings);
logger.info(LogOrigin.Tx, message);
}
res.send(httpSettings).status(200);
} catch (error) { } catch (error) {
res.status(400).send({ message: error.toString() }); res.status(400).send({ message: error.toString() });
} }
@@ -90,6 +90,21 @@ export const validateOSC = [
}, },
]; ];
/**
* @description Validates object for POST /ontime/http
*/
export const validateHTTP = [
body('enabledOut').exists().isBoolean(),
body('subscriptions')
.isObject()
.custom((value) => validateOscObject(value)),
(req, res, next) => {
const errors = validationResult(req);
if (!errors.isEmpty()) return res.status(422).json({ errors: errors.array() });
next();
},
];
/** /**
* @description Validates object for POST /ontime/osc-subscriptions * @description Validates object for POST /ontime/osc-subscriptions
*/ */
+4 -3
View File
@@ -16,11 +16,11 @@ import {
postNew, postNew,
postOSC, postOSC,
postOscSubscriptions, postOscSubscriptions,
postHttpSubscriptions,
postSettings, postSettings,
postUserFields, postUserFields,
postViewSettings, postViewSettings,
previewExcel, previewExcel,
postHTTP,
} from '../controllers/ontimeController.js'; } from '../controllers/ontimeController.js';
import { import {
@@ -31,6 +31,7 @@ import {
validateSettings, validateSettings,
validateUserFields, validateUserFields,
viewValidator, viewValidator,
validateHTTP,
} from '../controllers/ontimeController.validate.js'; } from '../controllers/ontimeController.validate.js';
import { projectSanitiser } from '../controllers/projectController.validate.js'; import { projectSanitiser } from '../controllers/projectController.validate.js';
@@ -90,8 +91,8 @@ router.post('/osc-subscriptions', validateSubscription, postOscSubscriptions);
// create route between controller and '/ontime/http' endpoint // create route between controller and '/ontime/http' endpoint
router.get('/http', getHTTP); router.get('/http', getHTTP);
// create route between controller and '/ontime/osc-subscriptions' endpoint // create route between controller and '/ontime/http' endpoint
router.post('/http-subscriptions', validateSubscription, postHttpSubscriptions); router.post('/http', validateHTTP, postHTTP);
// create route between controller and '/ontime/new' endpoint // create route between controller and '/ontime/new' endpoint
router.post('/new', projectSanitiser, postNew); router.post('/new', projectSanitiser, postNew);
@@ -115,7 +115,6 @@ export class HttpIntegration implements IIntegration {
} }
shutdown() { shutdown() {
console.log('Shutting down HTTP integration');
if (this.httpAgent) { if (this.httpAgent) {
this.httpAgent?.destroy(); this.httpAgent?.destroy();
this.httpAgent = null; this.httpAgent = null;
@@ -1,6 +1,4 @@
import { Subscription } from './Subscription.type.js'; import { Subscription } from './Subscription.type.js';
export interface HTTPSettings { export interface HTTPSettings {
enabledOut: boolean; enabledOut: boolean;
subscriptions: Subscription; subscriptions: Subscription;