-
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 GetOpenApiSpecV1Endpoint (HTTP GET)
The new endpoint can serve up the OpenAPI specification file of the plugin via a get request without any parameters needed. Under the hood it uses the re-usable common base endpoint class that we've recently added to the core package. The test case at this file path demonstrates how to use it via the API client: `packages/cactus-plugin-ledger-connector-besu/src/test/typescript/ unit/get-open-api-spec-v1-connector-besu.test.ts` Signed-off-by: Peter Somogyvari <[email protected]>
- Loading branch information
Showing
5 changed files
with
256 additions
and
26 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
39 changes: 39 additions & 0 deletions
39
...n-ledger-connector-besu/src/main/typescript/web-services/get-open-api-spec-v1-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,39 @@ | ||
import { | ||
GetOpenApiSpecV1EndpointBase, | ||
IGetOpenApiSpecV1EndpointBaseOptions, | ||
} from "@hyperledger/cactus-core"; | ||
|
||
import { Checks, LogLevelDesc } from "@hyperledger/cactus-common"; | ||
import { IWebServiceEndpoint } from "@hyperledger/cactus-core-api"; | ||
|
||
import OAS from "../../json/openapi.json"; | ||
|
||
export const OasPathGetOpenApiSpecV1 = | ||
OAS.paths[ | ||
"/api/v1/plugins/@hyperledger/cactus-plugin-ledger-connector-besu/get-open-api-spec" | ||
]; | ||
|
||
export type OasPathTypeGetOpenApiSpecV1 = typeof OasPathGetOpenApiSpecV1; | ||
|
||
export interface IGetOpenApiSpecV1EndpointOptions | ||
extends IGetOpenApiSpecV1EndpointBaseOptions< | ||
typeof OAS, | ||
OasPathTypeGetOpenApiSpecV1 | ||
> { | ||
readonly logLevel?: LogLevelDesc; | ||
} | ||
|
||
export class GetOpenApiSpecV1Endpoint | ||
extends GetOpenApiSpecV1EndpointBase<typeof OAS, OasPathTypeGetOpenApiSpecV1> | ||
implements IWebServiceEndpoint | ||
{ | ||
public get className(): string { | ||
return GetOpenApiSpecV1Endpoint.CLASS_NAME; | ||
} | ||
|
||
constructor(public readonly options: IGetOpenApiSpecV1EndpointOptions) { | ||
super(options); | ||
const fnTag = `${this.className}#constructor()`; | ||
Checks.truthy(options, `${fnTag} arg options`); | ||
} | ||
} |
89 changes: 89 additions & 0 deletions
89
...edger-connector-besu/src/test/typescript/unit/get-open-api-spec-v1-connector-besu.test.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,89 @@ | ||
import { | ||
IListenOptions, | ||
LogLevelDesc, | ||
LoggerProvider, | ||
Servers, | ||
} from "@hyperledger/cactus-common"; | ||
import { PluginRegistry } from "@hyperledger/cactus-core"; | ||
import { Constants, PluginImportType } from "@hyperledger/cactus-core-api"; | ||
import bodyParser from "body-parser"; | ||
import express from "express"; | ||
import http from "http"; | ||
import "jest-extended"; | ||
import { AddressInfo } from "net"; | ||
import { Server as SocketIoServer } from "socket.io"; | ||
import { v4 as uuidv4 } from "uuid"; | ||
import { | ||
BesuApiClient, | ||
BesuApiClientOptions, | ||
PluginFactoryLedgerConnector, | ||
PluginLedgerConnectorBesu, | ||
} from "../../../main/typescript/public-api"; | ||
|
||
describe(__filename, () => { | ||
const logLevel: LogLevelDesc = "TRACE"; | ||
|
||
const log = LoggerProvider.getOrCreate({ | ||
label: __filename, | ||
level: logLevel, | ||
}); | ||
|
||
const rpcApiHttpHost = "http://127.0.0.1:8000"; | ||
const rpcApiWsHost = "ws://127.0.0.1:9000"; | ||
|
||
const expressApp = express(); | ||
expressApp.use(bodyParser.json({ limit: "250mb" })); | ||
const server = http.createServer(expressApp); | ||
let apiClient: BesuApiClient; | ||
|
||
afterAll(async () => { | ||
await Servers.shutdown(server); | ||
}); | ||
|
||
beforeAll(async () => { | ||
const factory = new PluginFactoryLedgerConnector({ | ||
pluginImportType: PluginImportType.Local, | ||
}); | ||
|
||
const connector: PluginLedgerConnectorBesu = await factory.create({ | ||
rpcApiHttpHost, | ||
rpcApiWsHost, | ||
logLevel, | ||
instanceId: uuidv4(), | ||
pluginRegistry: new PluginRegistry({ plugins: [] }), | ||
}); | ||
|
||
const wsApi = new SocketIoServer(server, { | ||
path: Constants.SocketIoConnectionPathV1, | ||
}); | ||
|
||
await connector.registerWebServices(expressApp, wsApi); | ||
|
||
const listenOptions: IListenOptions = { | ||
hostname: "localhost", | ||
port: 0, | ||
server, | ||
}; | ||
const addressInfo = (await Servers.listen(listenOptions)) as AddressInfo; | ||
const { address, port } = addressInfo; | ||
const apiHost = `http://${address}:${port}`; | ||
|
||
const besuApiClientOptions = new BesuApiClientOptions({ | ||
basePath: apiHost, | ||
}); | ||
apiClient = new BesuApiClient(besuApiClientOptions); | ||
log.debug("Instantiated BesuApiClient OK"); | ||
}); | ||
|
||
it("Returns a JSON document containing the Open API specification of the plugin.", async () => { | ||
const res1Promise = apiClient.getOpenApiSpecV1(); | ||
await expect(res1Promise).resolves.not.toThrow(); | ||
const res1 = await res1Promise; | ||
expect(res1.status).toEqual(200); | ||
expect(res1.data).toBeTruthy(); | ||
expect(res1.config).toBeTruthy(); | ||
expect(res1.config.url).toBeString(); | ||
log.debug("Fetched URL OK=%s", res1.config.url); | ||
expect(res1.data).toBeObject(); | ||
}); | ||
}); |