mirror of
https://github.com/cpvalente/ontime.git
synced 2026-08-05 23:43:57 +00:00
wip: prepare endpoints
This commit is contained in:
@@ -110,13 +110,18 @@ export async function getOSC(): Promise<OSCSettings> {
|
||||
* @return {Promise}
|
||||
*/
|
||||
export async function getHTTP(): Promise<HTTPSettings> {
|
||||
console.log('getHTTP');
|
||||
const res = await axios.get(`${ontimeURL}/http`);
|
||||
console.log(res);
|
||||
|
||||
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
|
||||
* @return {Promise}
|
||||
@@ -133,14 +138,6 @@ export async function postOscSubscriptions(data: Subscription) {
|
||||
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
|
||||
*/
|
||||
|
||||
@@ -4,11 +4,11 @@ import { HTTPSettings } from 'ontime-types';
|
||||
import { queryRefetchIntervalSlow } from '../../ontimeConfig';
|
||||
import { HTTP_SETTINGS } from '../api/apiConstants';
|
||||
import { logAxiosError } from '../api/apiUtils';
|
||||
import { getHTTP, postHttpSubscriptions } from '../api/ontimeApi';
|
||||
import { getHTTP, postHTTP } from '../api/ontimeApi';
|
||||
import { httpPlaceholder } from '../models/Http';
|
||||
import { ontimeQueryClient } from '../queryClient';
|
||||
|
||||
export default function useHttpSettings() {
|
||||
export function useHttpSettings() {
|
||||
const { data, status, isFetching, isError, refetch } = useQuery({
|
||||
queryKey: HTTP_SETTINGS,
|
||||
queryFn: getHTTP,
|
||||
@@ -23,12 +23,11 @@ export default function useHttpSettings() {
|
||||
return { data: data! as unknown as HTTPSettings, status, isFetching, isError, refetch };
|
||||
}
|
||||
|
||||
|
||||
export function usePostHttpSubscriptions() {
|
||||
const { isLoading, mutateAsync } = useMutation({
|
||||
mutationFn: postHttpSubscriptions,
|
||||
export function usePostHttpSettings() {
|
||||
const { isPending, mutateAsync } = useMutation({
|
||||
mutationFn: postHTTP,
|
||||
onError: (error) => logAxiosError('Error saving HTTP settings', error),
|
||||
onSettled: () => ontimeQueryClient.invalidateQueries({ queryKey: HTTP_SETTINGS }),
|
||||
});
|
||||
return { isLoading, mutateAsync };
|
||||
return { isPending, mutateAsync };
|
||||
}
|
||||
|
||||
@@ -31,7 +31,7 @@ export default function IntegrationModal(props: IntegrationModalProps) {
|
||||
<TabList>
|
||||
<Tab>OSC</Tab>
|
||||
<Tab>OSC Integration</Tab>
|
||||
<Tab>HTML Integration</Tab>
|
||||
<Tab>HTTP Integration</Tab>
|
||||
</TabList>
|
||||
<TabPanels>
|
||||
<TabPanel>
|
||||
|
||||
@@ -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 fs from 'fs';
|
||||
@@ -284,11 +285,25 @@ export const getOSC = async (req, res) => {
|
||||
res.status(200).send(osc);
|
||||
};
|
||||
|
||||
// Create controller for GET request to '/ontime/http'
|
||||
// Returns -
|
||||
export const getHTTP = async (req, res) => {
|
||||
const http = DataProvider.getHttp();
|
||||
res.status(200).send(http);
|
||||
// Create controller for POST request to '/ontime/osc'
|
||||
// Returns ACK message
|
||||
export const postOSC = async (req, res) => {
|
||||
if (failEmptyObjects(req.body, res)) {
|
||||
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) => {
|
||||
@@ -312,42 +327,33 @@ export const postOscSubscriptions = async (req, res) => {
|
||||
}
|
||||
};
|
||||
|
||||
export const postHttpSubscriptions = async (req, res) => {
|
||||
if (failEmptyObjects(req.body, res)) {
|
||||
return;
|
||||
}
|
||||
|
||||
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 GET request to '/ontime/http'
|
||||
export const getHTTP = async (req, res: Response<HTTPSettings>) => {
|
||||
const http = DataProvider.getHttp();
|
||||
res.status(200).send(http);
|
||||
};
|
||||
|
||||
// Create controller for POST request to '/ontime/osc'
|
||||
// Returns ACK message
|
||||
export const postOSC = async (req, res) => {
|
||||
// Create controller for POST request to '/ontime/http'
|
||||
export const postHTTP = async (req: Request<any, any, HTTPSettings>, res: Response) => {
|
||||
if (failEmptyObjects(req.body, res)) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const oscSettings = req.body;
|
||||
await DataProvider.setOsc(oscSettings);
|
||||
const settings = req.body;
|
||||
const httpSettings = DataProvider.getHttp();
|
||||
const hasEnabledChanged = httpSettings.enabledOut !== settings.enabledOut;
|
||||
|
||||
// TODO: this update could be more granular, checking that relevant data was changed
|
||||
const { message } = oscIntegration.init(oscSettings);
|
||||
logger.info(LogOrigin.Tx, message);
|
||||
httpSettings.subscriptions = settings.subscriptions;
|
||||
httpSettings.enabledOut = settings.enabledOut;
|
||||
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) {
|
||||
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
|
||||
*/
|
||||
|
||||
@@ -16,11 +16,11 @@ import {
|
||||
postNew,
|
||||
postOSC,
|
||||
postOscSubscriptions,
|
||||
postHttpSubscriptions,
|
||||
postSettings,
|
||||
postUserFields,
|
||||
postViewSettings,
|
||||
previewExcel,
|
||||
postHTTP,
|
||||
} from '../controllers/ontimeController.js';
|
||||
|
||||
import {
|
||||
@@ -31,6 +31,7 @@ import {
|
||||
validateSettings,
|
||||
validateUserFields,
|
||||
viewValidator,
|
||||
validateHTTP,
|
||||
} from '../controllers/ontimeController.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
|
||||
router.get('/http', getHTTP);
|
||||
|
||||
// create route between controller and '/ontime/osc-subscriptions' endpoint
|
||||
router.post('/http-subscriptions', validateSubscription, postHttpSubscriptions);
|
||||
// create route between controller and '/ontime/http' endpoint
|
||||
router.post('/http', validateHTTP, postHTTP);
|
||||
|
||||
// create route between controller and '/ontime/new' endpoint
|
||||
router.post('/new', projectSanitiser, postNew);
|
||||
|
||||
@@ -115,7 +115,6 @@ export class HttpIntegration implements IIntegration {
|
||||
}
|
||||
|
||||
shutdown() {
|
||||
console.log('Shutting down HTTP integration');
|
||||
if (this.httpAgent) {
|
||||
this.httpAgent?.destroy();
|
||||
this.httpAgent = null;
|
||||
|
||||
@@ -1,6 +1,4 @@
|
||||
import { Subscription } from './Subscription.type.js';
|
||||
|
||||
|
||||
export interface HTTPSettings {
|
||||
enabledOut: boolean;
|
||||
subscriptions: Subscription;
|
||||
|
||||
Reference in New Issue
Block a user