Skip to content

Commit

Permalink
fix: add optional auth token to api-client and consortium-manual
Browse files Browse the repository at this point in the history
relationed with #1579

Signed-off-by: Elena Izaguirre <[email protected]>
  • Loading branch information
elenaizaguirre authored and petermetz committed Jan 17, 2022
1 parent 531956c commit c2feebf
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export class BambooHarvestListPage implements OnInit {
this._supplyChainApi = await this.baseClient.ofLedger(
this.quorumLedgerId,
SupplyChainApi,
{},
);
await this.loadData();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export class BookshelfDetailPage implements OnInit {
this._supplyChainApi = await this.baseClient.ofLedger(
this.quorumLedgerId,
SupplyChainApi,
{},
);

if (!this.bookshelf) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export class BookshelfListPage implements OnInit {
this._supplyChainApi = await this.baseClient.ofLedger(
this.ledgerId,
SupplyChainApi,
{},
);
await this.loadData();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export class ShipmentDetailPage implements OnInit {
this._supplyChainApi = await this.baseClient.ofLedger(
this.quorumLedgerId,
SupplyChainApi,
{},
);

if (!this.shipment) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export class ShipmentListPage implements OnInit {
this._supplyChainApi = await this.baseClient.ofLedger(
this.ledgerId,
SupplyChainApi,
{},
);
await this.loadData();
}
Expand Down
11 changes: 7 additions & 4 deletions packages/cactus-api-client/src/main/typescript/api-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ export class ApiClient extends BaseAPI {
public async ofLedger<T>(
ledgerOrId: string | Ledger,
ctor: new (configuration?: Configuration) => T,
ctorArgs: Record<string, unknown>,
): Promise<ApiClient & T>;
/**
* Constructs a new `ApiClient` object that is tied to whichever Cactus node
Expand All @@ -103,6 +104,7 @@ export class ApiClient extends BaseAPI {
public async ofLedger<T extends any>(
ledgerOrId: string | Ledger,
ctor: new (configuration?: Configuration) => T,
ctorArgs: Record<string, unknown>,
consortiumDbProvider?: IAsyncProvider<ConsortiumDatabase>,
): Promise<ApiClient & T> {
const fnTags = "ApiClient#forLedgerId()";
Expand All @@ -127,12 +129,13 @@ export class ApiClient extends BaseAPI {
// pick a random element from the array of nodes that have a connection to
// the target ledger (based on the ledger ID)
const randomIdx = Math.floor(Math.random() * nodes.length);

const randomNode = nodes[randomIdx];

const configuration = new Configuration({
basePath: randomNode.nodeApiHost,
});
// overwrite basePath with randomNode api host
ctorArgs.basePath = randomNode.nodeApiHost;

// create the ApiClient configuration object
const configuration = new Configuration(ctorArgs);

return new ApiClient(configuration).extendWith(ctor);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export interface IPluginConsortiumManualOptions extends ICactusPluginOptions {
prometheusExporter?: PrometheusExporter;
pluginRegistry?: PluginRegistry;
logLevel?: LogLevelDesc;
ctorArgs?: Record<string, unknown>;
}

export class PluginConsortiumManual
Expand Down Expand Up @@ -217,9 +218,16 @@ export class PluginConsortiumManual
public async getConsortiumJws(): Promise<JWSGeneral> {
const nodes = this.repo.allNodes;

const ctorArgs = this.options.ctorArgs || {};

const requests = nodes
.map((cnm) => cnm.nodeApiHost)
.map((host) => new Configuration({ basePath: host }))
.map(function (host) {
// overwrite basePath with node api host
ctorArgs.basePath = host;
// return the ApiClient configuration object
return new Configuration(ctorArgs);
})
.map((configuration) => new DefaultApi(configuration))
.map((apiClient) => apiClient.getNodeJwsV1());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ test(testCase, async (t: Test) => {
});

test("ApiClient #1 Routes based on Ledger ID #1", async (t2: Test) => {
const apiClient1 = await mainApiClient.ofLedger(ledger1.id, QuorumApi);
const apiClient1 = await mainApiClient.ofLedger(ledger1.id, QuorumApi, {});

// send money to the test account on ledger 1
const res = await apiClient1.runTransactionV1({
Expand All @@ -278,7 +278,7 @@ test(testCase, async (t: Test) => {
});

test("ApiClient #1 Routes based on Ledger ID #2", async (t2: Test) => {
const apiClient2 = await mainApiClient.ofLedger(ledger2.id, QuorumApi);
const apiClient2 = await mainApiClient.ofLedger(ledger2.id, QuorumApi, {});

// send money to the test account on ledger 1
const res = await apiClient2.runTransactionV1({
Expand Down

0 comments on commit c2feebf

Please sign in to comment.