forked from hyperledger-cacti/cacti
-
Notifications
You must be signed in to change notification settings - Fork 2
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): aggregate swagger.json endpoints
WORK IN PROGRESS Fixes hyperledger-cacti#431 Signed-off-by: Peter Somogyvari <peter.somogyvari@accenture.com>
- Loading branch information
Showing
19 changed files
with
780 additions
and
27 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
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
27 changes: 27 additions & 0 deletions
27
.../src/main/typescript/web-services/get-aggregate-openapi-json/collect-openapi-json-docs.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,27 @@ | ||
import type { OpenAPIV3 } from "express-openapi-validator/dist/framework/types"; | ||
|
||
import type { PluginRegistry } from "@hyperledger/cactus-core"; | ||
import type { ICactusPlugin } from "@hyperledger/cactus-core-api"; | ||
import type { IPluginWebService } from "@hyperledger/cactus-core-api"; | ||
import { isIPluginWebService } from "@hyperledger/cactus-core-api"; | ||
import { Checks } from "@hyperledger/cactus-common"; | ||
|
||
export async function collectOpenapiJsonDocs( | ||
pr: PluginRegistry, | ||
): Promise<OpenAPIV3.Document[]> { | ||
Checks.truthy(pr, `collectOpenapiJsonDocs() pr (PluginRegistry)`); | ||
|
||
const openApiJsonDocsPromises = pr | ||
.getPlugins() | ||
.filter((pluginInstance) => isIPluginWebService(pluginInstance)) | ||
.map(async (plugin: ICactusPlugin) => { | ||
const webSvc = plugin as IPluginWebService; | ||
const openApiJson = (await webSvc.getOpenApiSpec()) as OpenAPIV3.Document; | ||
return openApiJson; | ||
}); | ||
|
||
const openApiJsonDocs = await Promise.all(openApiJsonDocsPromises); | ||
|
||
// Filter out falsy results where the plugin did not return anything. | ||
return openApiJsonDocs.filter((d) => !!d); | ||
} |
107 changes: 107 additions & 0 deletions
107
...escript/web-services/get-aggregate-openapi-json/get-aggregate-openapi-json-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,107 @@ | ||
import { Express, Request, Response } from "express"; | ||
import HttpStatus from "http-status-codes"; | ||
|
||
import { | ||
Logger, | ||
Checks, | ||
LogLevelDesc, | ||
LoggerProvider, | ||
IAsyncProvider, | ||
} from "@hyperledger/cactus-common"; | ||
|
||
import { | ||
IWebServiceEndpoint, | ||
IExpressRequestHandler, | ||
IEndpointAuthzOptions, | ||
} from "@hyperledger/cactus-core-api"; | ||
|
||
import { | ||
PluginRegistry, | ||
registerWebServiceEndpoint, | ||
} from "@hyperledger/cactus-core"; | ||
|
||
import OAS from "../../../json/openapi.json"; | ||
import { collectOpenapiJsonDocs } from "./collect-openapi-json-docs"; | ||
|
||
export interface IGetAggregateOpenapiJsonEndpointV1Options { | ||
logLevel?: LogLevelDesc; | ||
pluginRegistry: PluginRegistry; | ||
} | ||
|
||
export class GetAggregateOpenapiJsonEndpointV1 implements IWebServiceEndpoint { | ||
public static readonly CLASS_NAME = "GetAggregateOpenapiJsonEndpointV1"; | ||
|
||
private readonly log: Logger; | ||
|
||
public get className(): string { | ||
return GetAggregateOpenapiJsonEndpointV1.CLASS_NAME; | ||
} | ||
|
||
constructor(public readonly opts: IGetAggregateOpenapiJsonEndpointV1Options) { | ||
const fnTag = `${this.className}#constructor()`; | ||
Checks.truthy(opts, `${fnTag} arg options`); | ||
Checks.truthy(opts.pluginRegistry, `${fnTag} arg options.pluginRegistry`); | ||
|
||
const level = this.opts.logLevel || "INFO"; | ||
const label = this.className; | ||
this.log = LoggerProvider.getOrCreate({ level, label }); | ||
} | ||
|
||
public getExpressRequestHandler(): IExpressRequestHandler { | ||
return this.handleRequest.bind(this); | ||
} | ||
|
||
public get oasPath(): typeof OAS.paths["/api/v1/api-server/get-aggregate-openapi-json"] { | ||
return OAS.paths["/api/v1/api-server/get-aggregate-openapi-json"]; | ||
} | ||
|
||
public getPath(): string { | ||
return this.oasPath.get["x-hyperledger-cactus"].http.path; | ||
} | ||
|
||
public getVerbLowerCase(): string { | ||
return this.oasPath.get["x-hyperledger-cactus"].http.verbLowerCase; | ||
} | ||
|
||
public getOperationId(): string { | ||
return this.oasPath.get.operationId; | ||
} | ||
|
||
public async registerExpress( | ||
expressApp: Express, | ||
): Promise<IWebServiceEndpoint> { | ||
await registerWebServiceEndpoint(expressApp, this); | ||
return this; | ||
} | ||
|
||
getAuthorizationOptionsProvider(): IAsyncProvider<IEndpointAuthzOptions> { | ||
// TODO: make this an injectable dependency in the constructor | ||
return { | ||
get: async () => ({ | ||
isProtected: true, | ||
requiredRoles: [], | ||
}), | ||
}; | ||
} | ||
|
||
async handleRequest(req: Request, res: Response): Promise<void> { | ||
const fnTag = `${this.className}#handleRequest()`; | ||
const verbUpper = this.getVerbLowerCase().toUpperCase(); | ||
this.log.debug(`${verbUpper} ${this.getPath()}`); | ||
|
||
try { | ||
const resBody = await collectOpenapiJsonDocs(this.opts.pluginRegistry); | ||
res.status(HttpStatus.OK); | ||
res.json(resBody); | ||
} catch (ex) { | ||
this.log.error(`${fnTag} failed to serve contract deploy request`, ex); | ||
res.status(HttpStatus.INTERNAL_SERVER_ERROR); | ||
res.statusMessage = ex.message; | ||
res.json({ error: ex.stack }); | ||
} | ||
} | ||
|
||
public async getAggregateOpenapiJson(): Promise<unknown> { | ||
return {}; | ||
} | ||
} |
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
Oops, something went wrong.