Skip to content

Commit

Permalink
PorterClient: more generic way to use axios
Browse files Browse the repository at this point in the history
  • Loading branch information
vzotova committed Jul 24, 2024
1 parent 7134edb commit 3552e03
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 54 deletions.
57 changes: 21 additions & 36 deletions packages/shared/src/porter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,32 +143,11 @@ export class PorterClient {
}
}

protected async tryAndGet<T, D>(
path: string,
config?: AxiosRequestConfig<D>,
): Promise<AxiosResponse<T>> {
protected async tryAndCall<T, D>(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,
);
config.baseURL = porterUrl.toString();
resp = await axios.request(config);
if (resp.status === HttpStatusCode.Ok) {
return resp;
}
Expand All @@ -186,14 +165,14 @@ export class PorterClient {
exclude_ursulas: excludeUrsulas,
include_ursulas: includeUrsulas,
};
const resp: AxiosResponse<GetUrsulasResult> = await this.tryAndGet(
'/get_ursulas',
{
params,
paramsSerializer: (params) => {
return qs.stringify(params, { arrayFormat: 'comma' });
},
const resp: AxiosResponse<GetUrsulasResult> = await this.tryAndCall({
url: '/get_ursulas',
method: 'get',
params: params,
paramsSerializer: (params) => {
return qs.stringify(params, { arrayFormat: 'comma' });
},
},
);
return resp.data.result.ursulas.map((u: UrsulaResponse) => ({
checksumAddress: u.checksum_address,
Expand Down Expand Up @@ -221,7 +200,11 @@ export class PorterClient {
context: conditionContextJSON,
};
const resp: AxiosResponse<PostRetrieveCFragsResponse> =
await this.tryAndPost('/retrieve_cfrags', data);
await this.tryAndCall({
url: '/retrieve_cfrags',
method: 'post',
data: data
});

return resp.data.result.retrieval_results.map(({ cfrags, errors }) => {
const parsed = Object.keys(cfrags).map((address) => [
Expand All @@ -246,10 +229,12 @@ export class PorterClient {
),
threshold,
};
const resp: AxiosResponse<PostTacoDecryptResponse> = await this.tryAndPost(
'/decrypt',
data,
);
const resp: AxiosResponse<PostTacoDecryptResponse> =
await this.tryAndCall({
url: '/decrypt',
method: 'post',
data: data
});

const { encrypted_decryption_responses, errors } =
resp.data.result.decryption_results;
Expand Down
35 changes: 17 additions & 18 deletions packages/test-utils/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ import {
zip,
} from '@nucypher/shared';
import { EIP4361_AUTH_METHOD, EIP4361AuthProvider } from '@nucypher/taco-auth';
import axios from 'axios';
import { ethers, providers, Wallet } from 'ethers';
import { expect, SpyInstance, vi } from 'vitest';

Expand Down Expand Up @@ -133,23 +132,23 @@ export const fakeUrsulas = (n = 4): Ursula[] =>
export const mockGetUrsulas = (
ursulas: Ursula[] = fakeUrsulas(),
): SpyInstance => {
const fakePorterUrsulas = (
mockUrsulas: readonly Ursula[],
): GetUrsulasResult => {
return {
result: {
ursulas: mockUrsulas.map(({ encryptingKey, uri, checksumAddress }) => ({
encrypting_key: toHexString(encryptingKey.toCompressedBytes()),
uri: uri,
checksum_address: checksumAddress,
})),
},
version: '5.2.0',
};
};

return vi.spyOn(axios, 'get').mockImplementation(async () => {
return Promise.resolve({ data: fakePorterUrsulas(ursulas) });
// const fakePorterUrsulas = (
// mockUrsulas: readonly Ursula[],
// ): GetUrsulasResult => {
// return {
// result: {
// ursulas: mockUrsulas.map(({ encryptingKey, uri, checksumAddress }) => ({
// encrypting_key: toHexString(encryptingKey.toCompressedBytes()),
// uri: uri,
// checksum_address: checksumAddress,
// })),
// },
// version: '5.2.0',
// };
// };

return vi.spyOn(PorterClient.prototype, 'getUrsulas').mockImplementation(async () => {
return Promise.resolve(ursulas);
});
};

Expand Down

0 comments on commit 3552e03

Please sign in to comment.