Skip to content

Commit

Permalink
PorterClient: multiple porter uris with check
Browse files Browse the repository at this point in the history
  • Loading branch information
vzotova committed Jul 18, 2024
1 parent b689493 commit 25b9bc9
Showing 1 changed file with 47 additions and 12 deletions.
59 changes: 47 additions & 12 deletions packages/shared/src/porter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ import {
RetrievalKit,
TreasureMap,
} from '@nucypher/nucypher-core';
import axios, { AxiosResponse } from 'axios';
import axios, {
AxiosRequestConfig,
AxiosResponse,
HttpStatusCode,
} from 'axios';
import qs from 'qs';

import { Base64EncodedBytes, ChecksumAddress, HexEncodedBytes } from './types';
Expand Down Expand Up @@ -120,10 +124,43 @@ export type TacoDecryptResult = {
};

export class PorterClient {
readonly porterUrl: URL;
readonly porterUrls: URL[];

constructor(porterUri: string) {
this.porterUrl = new URL(porterUri);
constructor(...porterUris: [string, ...[string]]) {
this.porterUrls = porterUris.map((uri) => new URL(uri));
}

protected async tryAndGet<T, D>(
path: string,
config?: AxiosRequestConfig<D>,
): Promise<AxiosResponse<T>> {
let resp!: AxiosResponse<T>;
for (const porterUrl of this.porterUrls) {
resp = await axios.get(new URL(path, porterUrl).toString(), config);
if (resp.status === HttpStatusCode.Ok) {
return resp;
}
}
return resp;
}

protected async tryAndPost<T, D>(
path: string,
data?: D,
config?: AxiosRequestConfig<D>,
): Promise<AxiosResponse<T>> {
let resp!: AxiosResponse<T>;
for (const porterUrl of this.porterUrls) {
resp = await axios.post(
new URL(path, porterUrl).toString(),
data,
config,
);
if (resp.status === HttpStatusCode.Ok) {
return resp;
}
}
return resp;
}

public async getUrsulas(
Expand All @@ -136,8 +173,8 @@ export class PorterClient {
exclude_ursulas: excludeUrsulas,
include_ursulas: includeUrsulas,
};
const resp: AxiosResponse<GetUrsulasResult> = await axios.get(
new URL('/get_ursulas', this.porterUrl).toString(),
const resp: AxiosResponse<GetUrsulasResult> = await this.tryAndGet(
'/get_ursulas',
{
params,
paramsSerializer: (params) => {
Expand Down Expand Up @@ -170,10 +207,8 @@ export class PorterClient {
bob_verifying_key: toHexString(bobVerifyingKey.toCompressedBytes()),
context: conditionContextJSON,
};
const resp: AxiosResponse<PostRetrieveCFragsResponse> = await axios.post(
new URL('/retrieve_cfrags', this.porterUrl).toString(),
data,
);
const resp: AxiosResponse<PostRetrieveCFragsResponse> =
await this.tryAndPost('/retrieve_cfrags', data);

return resp.data.result.retrieval_results.map(({ cfrags, errors }) => {
const parsed = Object.keys(cfrags).map((address) => [
Expand All @@ -198,8 +233,8 @@ export class PorterClient {
),
threshold,
};
const resp: AxiosResponse<PostTacoDecryptResponse> = await axios.post(
new URL('/decrypt', this.porterUrl).toString(),
const resp: AxiosResponse<PostTacoDecryptResponse> = await this.tryAndPost(
'/decrypt',
data,
);

Expand Down

0 comments on commit 25b9bc9

Please sign in to comment.