Skip to content

Commit

Permalink
feat(cmd-api-server): swagger.json endpoints
Browse files Browse the repository at this point in the history
fixes: #1877
Signed-off-by: charellesandig <[email protected]>
  • Loading branch information
charellesandig committed May 23, 2022
1 parent 10288c0 commit decae2b
Show file tree
Hide file tree
Showing 5 changed files with 3,391 additions and 3,055 deletions.
2 changes: 2 additions & 0 deletions packages/cactus-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,12 @@
"express": "4.17.1",
"express-jwt-authz": "2.4.1",
"express-openapi-validator": "4.12.12",
"swagger-ui-express": "4.3.0",
"typescript-optional": "2.0.1"
},
"devDependencies": {
"@types/express": "4.17.13",
"@types/swagger-ui-express": "4.1.3",
"uuid": "8.3.2"
},
"engines": {
Expand Down
31 changes: 31 additions & 0 deletions packages/cactus-plugin-htlc-eth-besu/src/main/json/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,37 @@
}
}
}
},
"/api/v1/plugins/@hyperledger/cactus-plugin-htlc-eth-besu/get-swagger-endpoint":{
"post": {
"x-hyperledger-cactus": {
"http": {
"verbLowerCase": "post",
"path": "/api/v1/plugins/@hyperledger/cactus-plugin-htlc-eth-besu/get-swagger-endpoint"
}
},
"operationId": "getSwaggerEndpoint",
"requestBody": {
"content": {
"application/json": {
"schema": {
}
}
}
},
"responses": {
"200": {
"description": "OK",
"content": {
"application/json": {
"schema": {
"$ref": "https://raw.githubusercontent.com/hyperledger/cactus/v0.8.0/packages/cactus-plugin-ledger-connector-besu/src/main/json/openapi.json#"
}
}
}
}
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,39 @@ export const DefaultApiAxiosParamCreator = function (configuration?: Configurati
options: localVarRequestOptions,
};
},
/**
*
* @param {any} [body]
* @param {*} [options] Override http request option.
* @throws {RequiredError}
*/
getSwaggerEndpoint: async (body?: any, options: any = {}): Promise<RequestArgs> => {
const localVarPath = `/api/v1/plugins/@hyperledger/cactus-plugin-htlc-eth-besu/get-swagger-endpoint`;
// use dummy base URL string because the URL constructor only accepts absolute URLs.
const localVarUrlObj = new URL(localVarPath, DUMMY_BASE_URL);
let baseOptions;
if (configuration) {
baseOptions = configuration.baseOptions;
}

const localVarRequestOptions = { method: 'POST', ...baseOptions, ...options};
const localVarHeaderParameter = {} as any;
const localVarQueryParameter = {} as any;



localVarHeaderParameter['Content-Type'] = 'application/json';

setSearchParams(localVarUrlObj, localVarQueryParameter, options.query);
let headersFromBaseOptions = baseOptions && baseOptions.headers ? baseOptions.headers : {};
localVarRequestOptions.headers = {...localVarHeaderParameter, ...headersFromBaseOptions, ...options.headers};
localVarRequestOptions.data = serializeDataIfNeeded(body, localVarRequestOptions, configuration)

return {
url: toPathString(localVarUrlObj),
options: localVarRequestOptions,
};
},
/**
*
* @param {InitializeRequest} [initializeRequest]
Expand Down Expand Up @@ -703,6 +736,16 @@ export const DefaultApiFp = function(configuration?: Configuration) {
const localVarAxiosArgs = await localVarAxiosParamCreator.getStatusV1(getStatusRequest, options);
return createRequestFunction(localVarAxiosArgs, globalAxios, BASE_PATH, configuration);
},
/**
*
* @param {any} [body]
* @param {*} [options] Override http request option.
* @throws {RequiredError}
*/
async getSwaggerEndpoint(body?: any, options?: any): Promise<(axios?: AxiosInstance, basePath?: string) => AxiosPromise<object>> {
const localVarAxiosArgs = await localVarAxiosParamCreator.getSwaggerEndpoint(body, options);
return createRequestFunction(localVarAxiosArgs, globalAxios, BASE_PATH, configuration);
},
/**
*
* @param {InitializeRequest} [initializeRequest]
Expand Down Expand Up @@ -771,6 +814,15 @@ export const DefaultApiFactory = function (configuration?: Configuration, basePa
getStatusV1(getStatusRequest?: GetStatusRequest, options?: any): AxiosPromise<Array<number>> {
return localVarFp.getStatusV1(getStatusRequest, options).then((request) => request(axios, basePath));
},
/**
*
* @param {any} [body]
* @param {*} [options] Override http request option.
* @throws {RequiredError}
*/
getSwaggerEndpoint(body?: any, options?: any): AxiosPromise<object> {
return localVarFp.getSwaggerEndpoint(body, options).then((request) => request(axios, basePath));
},
/**
*
* @param {InitializeRequest} [initializeRequest]
Expand Down Expand Up @@ -839,6 +891,17 @@ export class DefaultApi extends BaseAPI {
return DefaultApiFp(this.configuration).getStatusV1(getStatusRequest, options).then((request) => request(this.axios, this.basePath));
}

/**
*
* @param {any} [body]
* @param {*} [options] Override http request option.
* @throws {RequiredError}
* @memberof DefaultApi
*/
public getSwaggerEndpoint(body?: any, options?: any) {
return DefaultApiFp(this.configuration).getSwaggerEndpoint(body, options).then((request) => request(this.axios, this.basePath));
}

/**
*
* @param {InitializeRequest} [initializeRequest]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import { Express, Request, Response } from "express";
import {
Checks,
IAsyncProvider,
Logger,
LoggerProvider,
LogLevelDesc,
} from "@hyperledger/cactus-common";
import {
IEndpointAuthzOptions,
IExpressRequestHandler,
IWebServiceEndpoint,
} from "@hyperledger/cactus-core-api";
import { registerWebServiceEndpoint } from "@hyperledger/cactus-core";
import OAS from "../../json/openapi.json";

//import { InitializeRequest } from "../generated/openapi/typescript-axios";
import { PluginHtlcEthBesu } from "../plugin-htlc-eth-besu";
import { InitializeRequest } from "../public-api";

export interface IGetSwaggerEndpoints {
logLevel?: LogLevelDesc;
plugin: PluginHtlcEthBesu;
}

export class IGetSwaggerEndpoints implements IWebServiceEndpoint {
public static readonly CLASS_NAME = "GetSwaggerEndpoints";
private readonly log: Logger;

constructor(public readonly options: IGetSwaggerEndpoints) {
const fnTag = `${this.className}#constructor()`;
Checks.truthy(options, `${fnTag} arg options`);
const level = this.options.logLevel || "INFO";
const label = this.className;
this.log = LoggerProvider.getOrCreate({ level, label });
}

public get className(): string {
return IGetSwaggerEndpoints.CLASS_NAME;
}

public get oasPath(): typeof OAS.paths["/api/v1/plugins/@hyperledger/cactus-plugin-htlc-eth-besu/get-swagger-endpoint"] {
return OAS.paths[
"/api/v1/plugins/@hyperledger/cactus-plugin-htlc-eth-besu/get-swagger-endpoint"
];
}

public getVerbLowerCase(): string {
return this.oasPath.post["x-hyperledger-cactus"].http.verbLowerCase;
}

public getPath(): string {
return this.oasPath.post["x-hyperledger-cactus"].http.path;
}

public getOperationId(): string {
return this.oasPath.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 fnTag = "InitializeEndpoint#handleRequest()";
this.log.debug(`POST ${this.getPath()}`);
try {
const request: InitializeRequest = req.body as InitializeRequest;
const result = await this.options.plugin.initialize(request);
if (result.transactionReceipt?.status === false) {
res.status(400).json({
message: "Bad request",
error: result.transactionReceipt,
});
} else {
res.status(200);
res.send(result);
}
} catch (ex) {
this.log.error(`${fnTag} failed to serve request`, ex);
res.status(500).json({
message: "Internal Server Error",
error: ex?.stack || ex?.message,
});
}
}
}
Loading

0 comments on commit decae2b

Please sign in to comment.