-
Notifications
You must be signed in to change notification settings - Fork 283
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cmd-api-server): swagger.json endpoints
fixes: #1877 Signed-off-by: charellesandig <[email protected]>
- Loading branch information
1 parent
10288c0
commit decae2b
Showing
5 changed files
with
3,391 additions
and
3,055 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
...ages/cactus-plugin-htlc-eth-besu/src/main/typescript/web-services/get-swagger-endpoint.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
import { Express, Request, Response } from "express"; | ||
import { | ||
Checks, | ||
IAsyncProvider, | ||
Logger, | ||
LoggerProvider, | ||
LogLevelDesc, | ||
} from "@hyperledger/cactus-common"; | ||
import { | ||
IEndpointAuthzOptions, | ||
IExpressRequestHandler, | ||
IWebServiceEndpoint, | ||
} from "@hyperledger/cactus-core-api"; | ||
import { registerWebServiceEndpoint } from "@hyperledger/cactus-core"; | ||
import OAS from "../../json/openapi.json"; | ||
|
||
//import { InitializeRequest } from "../generated/openapi/typescript-axios"; | ||
import { PluginHtlcEthBesu } from "../plugin-htlc-eth-besu"; | ||
import { InitializeRequest } from "../public-api"; | ||
|
||
export interface IGetSwaggerEndpoints { | ||
logLevel?: LogLevelDesc; | ||
plugin: PluginHtlcEthBesu; | ||
} | ||
|
||
export class IGetSwaggerEndpoints implements IWebServiceEndpoint { | ||
public static readonly CLASS_NAME = "GetSwaggerEndpoints"; | ||
private readonly log: Logger; | ||
|
||
constructor(public readonly options: IGetSwaggerEndpoints) { | ||
const fnTag = `${this.className}#constructor()`; | ||
Checks.truthy(options, `${fnTag} arg options`); | ||
const level = this.options.logLevel || "INFO"; | ||
const label = this.className; | ||
this.log = LoggerProvider.getOrCreate({ level, label }); | ||
} | ||
|
||
public get className(): string { | ||
return IGetSwaggerEndpoints.CLASS_NAME; | ||
} | ||
|
||
public get oasPath(): typeof OAS.paths["/api/v1/plugins/@hyperledger/cactus-plugin-htlc-eth-besu/get-swagger-endpoint"] { | ||
return OAS.paths[ | ||
"/api/v1/plugins/@hyperledger/cactus-plugin-htlc-eth-besu/get-swagger-endpoint" | ||
]; | ||
} | ||
|
||
public getVerbLowerCase(): string { | ||
return this.oasPath.post["x-hyperledger-cactus"].http.verbLowerCase; | ||
} | ||
|
||
public getPath(): string { | ||
return this.oasPath.post["x-hyperledger-cactus"].http.path; | ||
} | ||
|
||
public getOperationId(): string { | ||
return this.oasPath.post.operationId; | ||
} | ||
|
||
getAuthorizationOptionsProvider(): IAsyncProvider<IEndpointAuthzOptions> { | ||
// TODO: make this an injectable dependency in the constructor | ||
return { | ||
get: async () => ({ | ||
isProtected: true, | ||
requiredRoles: [], | ||
}), | ||
}; | ||
} | ||
|
||
public async registerExpress( | ||
expressApp: Express, | ||
): Promise<IWebServiceEndpoint> { | ||
await registerWebServiceEndpoint(expressApp, this); | ||
return this; | ||
} | ||
|
||
public getExpressRequestHandler(): IExpressRequestHandler { | ||
return this.handleRequest.bind(this); | ||
} | ||
|
||
public async handleRequest(req: Request, res: Response): Promise<void> { | ||
const fnTag = "InitializeEndpoint#handleRequest()"; | ||
this.log.debug(`POST ${this.getPath()}`); | ||
try { | ||
const request: InitializeRequest = req.body as InitializeRequest; | ||
const result = await this.options.plugin.initialize(request); | ||
if (result.transactionReceipt?.status === false) { | ||
res.status(400).json({ | ||
message: "Bad request", | ||
error: result.transactionReceipt, | ||
}); | ||
} else { | ||
res.status(200); | ||
res.send(result); | ||
} | ||
} catch (ex) { | ||
this.log.error(`${fnTag} failed to serve request`, ex); | ||
res.status(500).json({ | ||
message: "Internal Server Error", | ||
error: ex?.stack || ex?.message, | ||
}); | ||
} | ||
} | ||
} |
Oops, something went wrong.