X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2Fui-server%2FUIHttpServer.ts;h=ccafb0845df0307a2577e34b68f5d4e8742ff18f;hb=b9da1bc288a2a6ffccfbca6cd19aa195bc6ccfbf;hp=08ea7ff080137f5cce8e664a90cb00be272abfb1;hpb=17bc43d765c22c8d8c132484f8dc9c3edd370d91;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/ui-server/UIHttpServer.ts b/src/charging-station/ui-server/UIHttpServer.ts index 08ea7ff0..ccafb084 100644 --- a/src/charging-station/ui-server/UIHttpServer.ts +++ b/src/charging-station/ui-server/UIHttpServer.ts @@ -1,7 +1,8 @@ -import type { IncomingMessage, RequestListener, ServerResponse } from 'http'; +import type { IncomingMessage, RequestListener, ServerResponse } from 'node:http'; import { StatusCodes } from 'http-status-codes'; +import { AbstractUIServer } from './AbstractUIServer'; import { UIServerUtils } from './UIServerUtils'; import { BaseError } from '../../exception'; import { @@ -14,12 +15,17 @@ import { ResponseStatus, type UIServerConfiguration, } from '../../types'; -import { logger } from '../../utils/Logger'; -import { Utils } from '../../utils/Utils'; -import { AbstractUIServer } from '../internal'; +import { Constants, Utils, logger } from '../../utils'; const moduleName = 'UIHttpServer'; +enum HttpMethods { + GET = 'GET', + PUT = 'PUT', + POST = 'POST', + PATCH = 'PATCH', +} + export class UIHttpServer extends AbstractUIServer { public constructor(protected readonly uiServerConfiguration: UIServerConfiguration) { super(uiServerConfiguration); @@ -102,7 +108,7 @@ export class UIHttpServer extends AbstractUIServer { error ); }); - if (req.method === 'POST') { + if (req.method === HttpMethods.POST) { const bodyBuffer = []; req .on('data', (chunk) => { @@ -112,10 +118,19 @@ export class UIHttpServer extends AbstractUIServer { const body = JSON.parse(Buffer.concat(bodyBuffer).toString()) as RequestPayload; this.uiServices .get(version) - ?.requestHandler(this.buildProtocolRequest(uuid, procedureName, body ?? {})) - .catch(() => { - /* Error caught by AbstractUIService */ - }); + ?.requestHandler( + this.buildProtocolRequest( + uuid, + procedureName, + body ?? Constants.EMPTY_FREEZED_OBJECT + ) + ) + .then((protocolResponse: ProtocolResponse) => { + if (!Utils.isNullOrUndefined(protocolResponse)) { + this.sendResponse(protocolResponse); + } + }) + .catch(Constants.EMPTY_FUNCTION); }); } else { throw new BaseError(`Unsupported HTTP method: '${req.method}'`);