-
Notifications
You must be signed in to change notification settings - Fork 286
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 getBalance web service
Fixes #1066 Signed-off-by: Youngone Lee <[email protected]>
- Loading branch information
1 parent
40be742
commit 50107f6
Showing
6 changed files
with
386 additions
and
8 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
...tus-plugin-ledger-connector-besu/src/main/typescript/web-services/get-balance-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 IGetBalanceEndpointOptions { | ||
logLevel?: LogLevelDesc; | ||
connector: PluginLedgerConnectorBesu; | ||
} | ||
|
||
export class GetBalanceEndpoint implements IWebServiceEndpoint { | ||
public static readonly CLASS_NAME = "GetBalanceEndpoint"; | ||
|
||
private readonly log: Logger; | ||
|
||
public get className(): string { | ||
return GetBalanceEndpoint.CLASS_NAME; | ||
} | ||
|
||
constructor(public readonly options: IGetBalanceEndpointOptions) { | ||
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-balance" | ||
]; | ||
} | ||
|
||
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.getBalance(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, | ||
}); | ||
} | ||
} | ||
} |
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
Oops, something went wrong.