( payload: ApiCallPayload )
| 185 | } |
| 186 | |
| 187 | export async function performApiCall( |
| 188 | payload: ApiCallPayload |
| 189 | ): Promise<LogsData<ApiCallErrorResponse | PerformApiCallSuccess>> { |
| 190 | const options = buildOptions(payload); |
| 191 | // We also pass the timeout to adapter to gracefully abort the request after the timeout. |
| 192 | // timeout passed to adapter will cause axios socket to hang until the timeout is reached |
| 193 | // even if the totalTimeoutMs is reached and the 2nd attempt is made |
| 194 | const goAttempt1 = await go(() => adapter.buildAndExecuteRequest(options, { timeout: FIRST_API_CALL_TIMEOUT }), { |
| 195 | totalTimeoutMs: FIRST_API_CALL_TIMEOUT, |
| 196 | }); |
| 197 | if (goAttempt1.success) { |
| 198 | return [[], { ...goAttempt1.data }]; |
| 199 | } |
| 200 | const goAttempt2 = await go(() => adapter.buildAndExecuteRequest(options, { timeout: SECOND_API_CALL_TIMEOUT }), { |
| 201 | totalTimeoutMs: SECOND_API_CALL_TIMEOUT, |
| 202 | }); |
| 203 | if (goAttempt2.success) { |
| 204 | return [[], { ...goAttempt2.data }]; |
| 205 | } |
| 206 | const { aggregatedApiCall } = payload; |
| 207 | const log = logger.pend('ERROR', `Failed to call Endpoint:${aggregatedApiCall.endpointName}`, goAttempt2.error); |
| 208 | // eslint-disable-next-line import/no-named-as-default-member |
| 209 | const axiosErrorMsg = axios.isAxiosError(goAttempt2.error) ? errorMsgFromAxiosError(goAttempt2.error) : ''; |
| 210 | const errorMessage = compact([RequestErrorMessage.ApiCallFailed, axiosErrorMsg]).join(' '); |
| 211 | return [[log], { success: false, errorMessage: errorMessage }]; |
| 212 | } |
| 213 | |
| 214 | export async function processSuccessfulApiCall( |
| 215 | payload: ApiCallPayload, |
no test coverage detected