diff --git a/packages/shared/src/porter.ts b/packages/shared/src/porter.ts index f1b0cf3ad..c9432d0db 100644 --- a/packages/shared/src/porter.ts +++ b/packages/shared/src/porter.ts @@ -143,32 +143,11 @@ export class PorterClient { } } - protected async tryAndGet( - path: string, - config?: AxiosRequestConfig, - ): Promise> { + protected async tryAndCall(config: AxiosRequestConfig): Promise> { let resp!: AxiosResponse; 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( - path: string, - data?: D, - config?: AxiosRequestConfig, - ): Promise> { - let resp!: AxiosResponse; - 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; } @@ -186,14 +165,14 @@ export class PorterClient { exclude_ursulas: excludeUrsulas, include_ursulas: includeUrsulas, }; - const resp: AxiosResponse = await this.tryAndGet( - '/get_ursulas', - { - params, - paramsSerializer: (params) => { - return qs.stringify(params, { arrayFormat: 'comma' }); - }, + const resp: AxiosResponse = 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, @@ -221,7 +200,11 @@ export class PorterClient { context: conditionContextJSON, }; const resp: AxiosResponse = - 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) => [ @@ -246,10 +229,12 @@ export class PorterClient { ), threshold, }; - const resp: AxiosResponse = await this.tryAndPost( - '/decrypt', - data, - ); + const resp: AxiosResponse = + await this.tryAndCall({ + url: '/decrypt', + method: 'post', + data: data + }); const { encrypted_decryption_responses, errors } = resp.data.result.decryption_results; diff --git a/packages/test-utils/src/utils.ts b/packages/test-utils/src/utils.ts index 9a52848eb..06095b125 100644 --- a/packages/test-utils/src/utils.ts +++ b/packages/test-utils/src/utils.ts @@ -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'; @@ -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); }); };