Skip to content

Commit

Permalink
feat: new api structure (#86)
Browse files Browse the repository at this point in the history
* feat: new api structure

* chore: camelCase

* fix: remove redirect on session

* fix: patch response fetch type and cleanup

* fix: accidental search replace
  • Loading branch information
oshinongit authored May 3, 2024
1 parent 1c0e85f commit 30e0ef9
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 84 deletions.
1 change: 1 addition & 0 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ export default async (opts: ApiOptions) => {
api.register(healthcheck, { title: opts.title });
// register other API routes here
api.register(await getApiProductions(), {
prefix: 'api/v1',
smbServerBaseUrl: opts.smbServerBaseUrl,
endpointIdleTimeout: opts.endpointIdleTimeout
});
Expand Down
133 changes: 71 additions & 62 deletions src/api_productions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ import {
UserResponse,
ProductionResponse,
DetailedProductionResponse,
UserSession
UserSession,
NewSession,
SessionResponse,
SdpAnswer
} from './models';
import { SmbProtocol } from './smb';
import { ProductionManager } from './production_manager';
Expand Down Expand Up @@ -64,7 +67,7 @@ const apiProductions: FastifyPluginCallback<ApiProductionsOptions> = (
if (production) {
const productionResponse: ProductionResponse = {
name: production.name,
productionid: production._id.toString()
productionId: production._id.toString()
};
reply.code(200).send(productionResponse);
} else {
Expand All @@ -81,7 +84,7 @@ const apiProductions: FastifyPluginCallback<ApiProductionsOptions> = (
fastify.get<{
Reply: ProductionResponse[] | string;
}>(
'/productions',
'/production',
{
schema: {
description: 'Retrieves all Productions.',
Expand All @@ -96,7 +99,7 @@ const apiProductions: FastifyPluginCallback<ApiProductionsOptions> = (
reply.code(200).send(
productions.map(({ _id, name }) => ({
name,
productionid: _id.toString()
productionId: _id.toString()
}))
);
} catch (err) {
Expand All @@ -108,10 +111,10 @@ const apiProductions: FastifyPluginCallback<ApiProductionsOptions> = (
);

fastify.get<{
Params: { productionid: string };
Params: { productionId: string };
Reply: DetailedProductionResponse | string;
}>(
'/productions/:productionid',
'/production/:productionId',
{
schema: {
description: 'Retrieves a Production.',
Expand All @@ -123,13 +126,13 @@ const apiProductions: FastifyPluginCallback<ApiProductionsOptions> = (
async (request, reply) => {
try {
const production = await productionManager.requireProduction(
parseInt(request.params.productionid, 10)
parseInt(request.params.productionId, 10)
);
const allLinesResponse: LineResponse[] =
coreFunctions.getAllLinesResponse(production);
const productionResponse: DetailedProductionResponse = {
name: production.name,
productionid: production._id.toString(),
productionId: production._id.toString(),
lines: allLinesResponse
};
reply.code(200).send(productionResponse);
Expand All @@ -142,10 +145,10 @@ const apiProductions: FastifyPluginCallback<ApiProductionsOptions> = (
);

fastify.get<{
Params: { productionid: string };
Params: { productionId: string };
Reply: LineResponse[] | string;
}>(
'/productions/:productionid/lines',
'/production/:productionId/line',
{
schema: {
description: 'Retrieves all lines for a Production.',
Expand All @@ -157,7 +160,7 @@ const apiProductions: FastifyPluginCallback<ApiProductionsOptions> = (
async (request, reply) => {
try {
const production = await productionManager.requireProduction(
parseInt(request.params.productionid, 10)
parseInt(request.params.productionId, 10)
);
const allLinesResponse: LineResponse[] =
coreFunctions.getAllLinesResponse(production);
Expand All @@ -171,10 +174,10 @@ const apiProductions: FastifyPluginCallback<ApiProductionsOptions> = (
);

fastify.get<{
Params: { productionid: string; lineid: string };
Params: { productionId: string; lineId: string };
Reply: LineResponse | string;
}>(
'/productions/:productionid/lines/:lineid',
'/production/:productionId/line/:lineId',
{
schema: {
description: 'Retrieves an active Production line.',
Expand All @@ -185,20 +188,20 @@ const apiProductions: FastifyPluginCallback<ApiProductionsOptions> = (
},
async (request, reply) => {
try {
const { productionid, lineid } = request.params;
const { productionId, lineId } = request.params;
const production = await productionManager.requireProduction(
parseInt(productionid, 10)
parseInt(productionId, 10)
);
const line = productionManager.requireLine(production.lines, lineid);
const line = productionManager.requireLine(production.lines, lineId);

const participantlist = productionManager.getUsersForLine(
productionid,
productionId,
line.id
);
const lineResponse: LineResponse = {
name: line.name,
id: line.id,
smbconferenceid: line.smbconferenceid,
smbConferenceId: line.smbConferenceId,
participants: participantlist
};
reply.code(200).send(lineResponse);
Expand All @@ -211,42 +214,40 @@ const apiProductions: FastifyPluginCallback<ApiProductionsOptions> = (
);

fastify.post<{
Params: { productionid: string; lineid: string; username: string };
Reply: { [key: string]: string | string[] } | string;
Body: NewSession;
Reply: SessionResponse | string;
}>(
'/productions/:productionid/lines/:lineid/users/:username',
'/session',
{
schema: {
description:
'Initiate connection protocol. Generates sdp offer describing remote SMB instance.',
body: NewSession,
response: {
200: Type.Object({
sdp: Type.String(),
sessionid: Type.String()
})
201: SessionResponse
}
}
},
async (request, reply) => {
try {
const { lineid, productionid, username } = request.params;
const { lineId, productionId, username } = request.body;
const sessionId: string = uuidv4();
const production = await productionManager.requireProduction(
parseInt(productionid, 10)
parseInt(productionId, 10)
);

const smbconferenceid = await coreFunctions.createConferenceForLine(
const smbConferenceId = await coreFunctions.createConferenceForLine(
smb,
smbServerUrl,
production,
lineid
lineId
);

const endpointId: string = uuidv4();
const endpoint = await coreFunctions.createEndpoint(
smb,
smbServerUrl,
smbconferenceid,
smbConferenceId,
endpointId,
true,
true,
Expand All @@ -260,16 +261,19 @@ const apiProductions: FastifyPluginCallback<ApiProductionsOptions> = (
}

const sdpOffer = await coreFunctions.createConnection(
productionid,
lineid,
productionId,
lineId,
endpoint,
username,
endpointId,
sessionId
);

if (sdpOffer) {
reply.code(200).send({ sdp: sdpOffer, sessionid: sessionId });
reply
.code(201)
.type('application/json')
.send({ sessionId: sessionId, sdp: sdpOffer });
} else {
reply.code(500).send('Failed to generate sdp offer for endpoint');
}
Expand All @@ -282,10 +286,10 @@ const apiProductions: FastifyPluginCallback<ApiProductionsOptions> = (
);

fastify.patch<{
Params: { productionid: string; lineid: string; sessionid: string };
Body: string;
Params: { sessionId: string };
Body: SdpAnswer;
}>(
'/productions/:productionid/lines/:lineid/session/:sessionid',
'/session/:sessionId',
{
schema: {
description:
Expand All @@ -297,19 +301,24 @@ const apiProductions: FastifyPluginCallback<ApiProductionsOptions> = (
},
async (request, reply) => {
try {
const { productionid, lineid, sessionid } = request.params;
const production = await productionManager.requireProduction(
parseInt(productionid, 10)
);
const line = productionManager.requireLine(production.lines, lineid);
const { sessionId } = request.params;

const userSession: UserSession | undefined =
productionManager.getUser(sessionid);
productionManager.getUser(sessionId);
if (!userSession) {
throw new Error(
'Could not get user session or session does not exist'
);
}

const production = await productionManager.requireProduction(
parseInt(userSession.productionId, 10)
);
const line = productionManager.requireLine(
production.lines,
userSession.lineId
);

const connectionEndpointDescription:
| SmbEndpointDescription
| undefined = userSession.sessionDescription;
Expand All @@ -324,12 +333,12 @@ const apiProductions: FastifyPluginCallback<ApiProductionsOptions> = (
await coreFunctions.handleAnswerRequest(
smb,
smbServerUrl,
line.smbconferenceid,
line.smbConferenceId,
endpointId,
connectionEndpointDescription,
request.body
request.body.sdpAnswer
);
reply.code(200);
reply.code(204);
} catch (err) {
reply
.code(500)
Expand All @@ -339,10 +348,10 @@ const apiProductions: FastifyPluginCallback<ApiProductionsOptions> = (
);

fastify.delete<{
Params: { productionid: string };
Params: { productionId: string };
Reply: string;
}>(
'/productions/:productionid',
'/production/:productionId',
{
schema: {
description: 'Deletes a Production.',
Expand All @@ -352,16 +361,16 @@ const apiProductions: FastifyPluginCallback<ApiProductionsOptions> = (
}
},
async (request, reply) => {
const { productionid } = request.params;
const { productionId } = request.params;
try {
if (
!(await productionManager.deleteProduction(
parseInt(productionid, 10)
parseInt(productionId, 10)
))
) {
throw new Error('Could not delete production');
}
reply.code(204).send(`Deleted production ${productionid}`);
reply.code(204).send(`Deleted production ${productionId}`);
} catch (err) {
reply
.code(500)
Expand All @@ -371,10 +380,10 @@ const apiProductions: FastifyPluginCallback<ApiProductionsOptions> = (
);

fastify.delete<{
Params: { productionid: string; lineid: string; sessionid: string };
Params: { sessionId: string };
Reply: string;
}>(
'/productions/:productionid/lines/:lineid/session/:sessionid',
'/session/:sessionId',
{
schema: {
description: 'Deletes a Connection from ProductionManager.',
Expand All @@ -384,7 +393,7 @@ const apiProductions: FastifyPluginCallback<ApiProductionsOptions> = (
}
},
async (request, reply) => {
const sessionId = request.params.sessionid;
const sessionId = request.params.sessionId;
try {
const deletedSessionId = productionManager.removeUserSession(sessionId);
if (!deletedSessionId) {
Expand All @@ -401,10 +410,10 @@ const apiProductions: FastifyPluginCallback<ApiProductionsOptions> = (

//Long poll endpoint
fastify.post<{
Params: { productionid: string; lineid: string; sessionid: string };
Params: { productionId: string; lineId: string };
Reply: UserResponse[] | string;
}>(
'/productions/:productionid/lines/:lineid/participants',
'/production/:productionId/line/:lineId/participants',
{
schema: {
description: 'Long Poll Endpoint to get participant list.',
Expand All @@ -416,8 +425,8 @@ const apiProductions: FastifyPluginCallback<ApiProductionsOptions> = (
async (request, reply) => {
try {
const participants = productionManager.getUsersForLine(
request.params.productionid,
request.params.lineid
request.params.productionId,
request.params.lineId
);

const waitForChange = new Promise<void>((resolve) => {
Expand All @@ -439,9 +448,9 @@ const apiProductions: FastifyPluginCallback<ApiProductionsOptions> = (
);

fastify.get<{
Params: { sessionid: string };
Params: { sessionId: string };
}>(
'/heartbeat/:sessionid',
'/heartbeat/:sessionId',
{
schema: {
description: 'Update user session lastSeen',
Expand All @@ -452,12 +461,12 @@ const apiProductions: FastifyPluginCallback<ApiProductionsOptions> = (
}
},
async (request, reply) => {
const { sessionid } = request.params;
const status = productionManager.updateUserLastSeen(sessionid);
const { sessionId } = request.params;
const status = productionManager.updateUserLastSeen(sessionId);
if (status) {
reply.code(200).send('ok');
} else {
reply.code(410).send(`User session id "${sessionid}" not found.`);
reply.code(410).send(`User session id "${sessionId}" not found.`);
}
}
);
Expand Down
Loading

0 comments on commit 30e0ef9

Please sign in to comment.