-
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 getPastLogs web service
Fixes #1067 Signed-off-by: Hana Awad <awadhana0825@gmail.com>
- Loading branch information
Showing
5 changed files
with
382 additions
and
12 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
...s-plugin-ledger-connector-besu/src/main/typescript/web-services/get-past-logs-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 IGetPastLogsEndpointOptions { | ||
logLevel?: LogLevelDesc; | ||
connector: PluginLedgerConnectorBesu; | ||
} | ||
|
||
export class GetPastLogsEndpoint implements IWebServiceEndpoint { | ||
public static readonly CLASS_NAME = "GetPastLogsEndpoint"; | ||
|
||
private readonly log: Logger; | ||
|
||
public get className(): string { | ||
return GetPastLogsEndpoint.CLASS_NAME; | ||
} | ||
|
||
constructor(public readonly options: IGetPastLogsEndpointOptions) { | ||
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-past-logs" | ||
]; | ||
} | ||
|
||
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.getPastLogs(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.