forked from hyperledger-cacti/cacti
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(connector-besu): add getTransaction web service
Fixes hyperledger-cacti#1062 Depends on hyperledger-cacti#1061 Signed-off-by: Jeffrey Ushry <[email protected]>
- Loading branch information
1 parent
f97f264
commit 584bfdd
Showing
5 changed files
with
387 additions
and
0 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
101 changes: 101 additions & 0 deletions
101
...plugin-ledger-connector-besu/src/main/typescript/web-services/get-transaction-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,101 @@ | ||
import { Express, Request, Response } from "express"; | ||
|
||
import { | ||
Logger, | ||
Checks, | ||
LogLevelDesc, | ||
LoggerProvider, | ||
IAsyncProvider, | ||
} from "@hyperledger/cactus-common"; | ||
import { | ||
IEndpointAuthzOptions, | ||
IExpressRequestHandler, | ||
IWebServiceEndpoint, | ||
} from "@hyperledger/cactus-core-api"; | ||
import { registerWebServiceEndpoint } from "@hyperledger/cactus-core"; | ||
|
||
import { PluginLedgerConnectorBesu } from "../plugin-ledger-connector-besu"; | ||
|
||
import OAS from "../../json/openapi.json"; | ||
|
||
export interface IGetTransactionEndpointOptions { | ||
logLevel?: LogLevelDesc; | ||
connector: PluginLedgerConnectorBesu; | ||
} | ||
|
||
export class GetTransactionEndpoint implements IWebServiceEndpoint { | ||
public static readonly CLASS_NAME = "GetTransactionEndpoint"; | ||
|
||
private readonly log: Logger; | ||
|
||
public get className(): string { | ||
return GetTransactionEndpoint.CLASS_NAME; | ||
} | ||
|
||
constructor(public readonly options: IGetTransactionEndpointOptions) { | ||
const fnTag = `${this.className}#constructor()`; | ||
Checks.truthy(options, `${fnTag} arg options`); | ||
Checks.truthy(options.connector, `${fnTag} arg options.connector`); | ||
|
||
const level = this.options.logLevel || "INFO"; | ||
const label = this.className; | ||
this.log = LoggerProvider.getOrCreate({ level, label }); | ||
} | ||
|
||
public getOasPath() { | ||
return OAS.paths[ | ||
"/api/v1/plugins/@hyperledger/cactus-plugin-ledger-connector-besu/get-transaction" | ||
]; | ||
} | ||
|
||
public getPath(): string { | ||
const apiPath = this.getOasPath(); | ||
return apiPath.post["x-hyperledger-cactus"].http.path; | ||
} | ||
|
||
public getVerbLowerCase(): string { | ||
const apiPath = this.getOasPath(); | ||
return apiPath.post["x-hyperledger-cactus"].http.verbLowerCase; | ||
} | ||
|
||
public getOperationId(): string { | ||
return this.getOasPath().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 reqTag = `${this.getVerbLowerCase()} - ${this.getPath()}`; | ||
this.log.debug(reqTag); | ||
const reqBody = req.body; | ||
try { | ||
const resBody = await this.options.connector.getTransaction(reqBody); | ||
res.json(resBody); | ||
} catch (ex) { | ||
this.log.error(`Crash while serving ${reqTag}`, ex); | ||
res.status(500).json({ | ||
message: "Internal Server Error", | ||
error: ex?.stack || ex?.message, | ||
}); | ||
} | ||
} | ||
} |
Oops, something went wrong.