Skip to content

Commit

Permalink
feat(connector-besu): add getTransaction web service
Browse files Browse the repository at this point in the history
Fixes hyperledger#1062

Depends on hyperledger#1061

Signed-off-by: Jeffrey Ushry <jeffrey.ushry@accenture.com>
  • Loading branch information
Jeff-Ushry authored and petermetz committed Jul 2, 2021
1 parent c4900e9 commit 0ca0769
Show file tree
Hide file tree
Showing 5 changed files with 387 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -990,6 +990,41 @@
}
}
},

"/api/v1/plugins/@hyperledger/cactus-plugin-ledger-connector-besu/get-transaction": {
"post": {
"x-hyperledger-cactus": {
"http": {
"verbLowerCase": "post",
"path": "/api/v1/plugins/@hyperledger/cactus-plugin-ledger-connector-besu/get-transaction"
}
},
"operationId": "getTransaction",
"summary": "Executes a transaction on a besu ledger",
"parameters": [],
"requestBody": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/GetTransactionV1Request"
}
}
}
},
"responses": {
"200": {
"description": "OK",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/GetTransactionV1Response"
}
}
}
}
}
}
},
"/api/v1/plugins/@hyperledger/cactus-plugin-ledger-connector-besu/invoke-contract": {
"post": {
"x-hyperledger-cactus": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1086,6 +1086,40 @@ export const DefaultApiAxiosParamCreator = function (configuration?: Configurati
options: localVarRequestOptions,
};
},
/**
*
* @summary Executes a transaction on a besu ledger
* @param {GetTransactionV1Request} [getTransactionV1Request]
* @param {*} [options] Override http request option.
* @throws {RequiredError}
*/
getTransaction: async (getTransactionV1Request?: GetTransactionV1Request, options: any = {}): Promise<RequestArgs> => {
const localVarPath = `/api/v1/plugins/@hyperledger/cactus-plugin-ledger-connector-besu/get-transaction`;
// 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(getTransactionV1Request, localVarRequestOptions, configuration)

return {
url: toPathString(localVarUrlObj),
options: localVarRequestOptions,
};
},
/**
*
* @summary Return balance of an address of a given block
Expand Down Expand Up @@ -1209,6 +1243,17 @@ export const DefaultApiFp = function(configuration?: Configuration) {
const localVarAxiosArgs = await localVarAxiosParamCreator.getPrometheusExporterMetricsV1(options);
return createRequestFunction(localVarAxiosArgs, globalAxios, BASE_PATH, configuration);
},
/**
*
* @summary Executes a transaction on a besu ledger
* @param {GetTransactionV1Request} [getTransactionV1Request]
* @param {*} [options] Override http request option.
* @throws {RequiredError}
*/
async getTransaction(getTransactionV1Request?: GetTransactionV1Request, options?: any): Promise<(axios?: AxiosInstance, basePath?: string) => AxiosPromise<GetTransactionV1Response>> {
const localVarAxiosArgs = await localVarAxiosParamCreator.getTransaction(getTransactionV1Request, options);
return createRequestFunction(localVarAxiosArgs, globalAxios, BASE_PATH, configuration);
},
/**
*
* @summary Return balance of an address of a given block
Expand Down Expand Up @@ -1280,6 +1325,16 @@ export const DefaultApiFactory = function (configuration?: Configuration, basePa
getPrometheusExporterMetricsV1(options?: any): AxiosPromise<string> {
return localVarFp.getPrometheusExporterMetricsV1(options).then((request) => request(axios, basePath));
},
/**
*
* @summary Executes a transaction on a besu ledger
* @param {GetTransactionV1Request} [getTransactionV1Request]
* @param {*} [options] Override http request option.
* @throws {RequiredError}
*/
getTransaction(getTransactionV1Request?: GetTransactionV1Request, options?: any): AxiosPromise<GetTransactionV1Response> {
return localVarFp.getTransaction(getTransactionV1Request, options).then((request) => request(axios, basePath));
},
/**
*
* @summary Return balance of an address of a given block
Expand Down Expand Up @@ -1357,6 +1412,18 @@ export class DefaultApi extends BaseAPI {
return DefaultApiFp(this.configuration).getPrometheusExporterMetricsV1(options).then((request) => request(this.axios, this.basePath));
}

/**
*
* @summary Executes a transaction on a besu ledger
* @param {GetTransactionV1Request} [getTransactionV1Request]
* @param {*} [options] Override http request option.
* @throws {RequiredError}
* @memberof DefaultApi
*/
public getTransaction(getTransactionV1Request?: GetTransactionV1Request, options?: any) {
return DefaultApiFp(this.configuration).getTransaction(getTransactionV1Request, options).then((request) => request(this.axios, this.basePath));
}

/**
*
* @summary Return balance of an address of a given block
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ import {
} from "./web-services/get-prometheus-exporter-metrics-endpoint-v1";
import { WatchBlocksV1Endpoint } from "./web-services/watch-blocks-v1-endpoint";
import { GetBalanceEndpoint } from "./web-services/get-balance-endpoint";
import { GetTransactionEndpoint } from "./web-services/get-transaction-endpoint";

export const E_KEYCHAIN_NOT_FOUND = "cactus.connector.besu.keychain_not_found";

Expand Down Expand Up @@ -217,6 +218,13 @@ export class PluginLedgerConnectorBesu
});
endpoints.push(endpoint);
}
{
const endpoint = new GetTransactionEndpoint({
connector: this,
logLevel: this.options.logLevel,
});
endpoints.push(endpoint);
}
{
const endpoint = new RunTransactionEndpoint({
connector: this,
Expand Down
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 IGetTransactionEndpointOptions {
logLevel?: LogLevelDesc;
connector: PluginLedgerConnectorBesu;
}

export class GetTransactionEndpoint implements IWebServiceEndpoint {
public static readonly CLASS_NAME = "GetTransactionEndpoint";

private readonly log: Logger;

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

constructor(public readonly options: IGetTransactionEndpointOptions) {
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-transaction"
];
}

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.getTransaction(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,
});
}
}
}
Loading

0 comments on commit 0ca0769

Please sign in to comment.