|
| 1 | +package com.stripe.android.financialconnections.lite.network |
| 2 | + |
| 3 | +import com.stripe.android.core.Logger |
| 4 | +import com.stripe.android.core.exception.APIConnectionException |
| 5 | +import com.stripe.android.core.exception.APIException |
| 6 | +import com.stripe.android.core.exception.AuthenticationException |
| 7 | +import com.stripe.android.core.exception.InvalidRequestException |
| 8 | +import com.stripe.android.core.exception.PermissionException |
| 9 | +import com.stripe.android.core.exception.RateLimitException |
| 10 | +import com.stripe.android.core.model.parsers.StripeErrorJsonParser |
| 11 | +import com.stripe.android.core.networking.HTTP_TOO_MANY_REQUESTS |
| 12 | +import com.stripe.android.core.networking.StripeNetworkClient |
| 13 | +import com.stripe.android.core.networking.StripeRequest |
| 14 | +import com.stripe.android.core.networking.StripeResponse |
| 15 | +import com.stripe.android.core.networking.responseJson |
| 16 | +import kotlinx.serialization.KSerializer |
| 17 | +import kotlinx.serialization.json.Json |
| 18 | +import java.net.HttpURLConnection |
| 19 | +import javax.inject.Inject |
| 20 | + |
| 21 | +internal class FinancialConnectionsLiteRequestExecutor @Inject constructor( |
| 22 | + private val stripeNetworkClient: StripeNetworkClient, |
| 23 | + private val json: Json, |
| 24 | + private val logger: Logger, |
| 25 | +) { |
| 26 | + suspend fun <Response> execute( |
| 27 | + request: StripeRequest, |
| 28 | + responseSerializer: KSerializer<Response> |
| 29 | + ): Result<Response> { |
| 30 | + return executeInternal(request) { body -> |
| 31 | + runCatching { |
| 32 | + json.decodeFromString(responseSerializer, body) |
| 33 | + } |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + private suspend fun <Response> executeInternal( |
| 38 | + request: StripeRequest, |
| 39 | + decodeResponse: (String) -> Result<Response>, |
| 40 | + ): Result<Response> { |
| 41 | + logger.debug("Executing ${request.method.code} request to ${request.url}") |
| 42 | + return runCatching { |
| 43 | + stripeNetworkClient.executeRequest(request) |
| 44 | + }.mapCatching { response -> |
| 45 | + if (response.isError) { |
| 46 | + throw handleApiError(response) |
| 47 | + } else { |
| 48 | + decodeResponse(requireNotNull(response.body)).getOrThrow() |
| 49 | + } |
| 50 | + }.recoverCatching { |
| 51 | + throw APIConnectionException("Failed to execute $request", cause = it) |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + private fun handleApiError(response: StripeResponse<String>): Exception { |
| 56 | + val requestId = response.requestId?.value |
| 57 | + val responseCode = response.code |
| 58 | + val stripeError = StripeErrorJsonParser().parse(response.responseJson()) |
| 59 | + |
| 60 | + return when (responseCode) { |
| 61 | + HttpURLConnection.HTTP_ACCEPTED, |
| 62 | + HttpURLConnection.HTTP_BAD_REQUEST, |
| 63 | + HttpURLConnection.HTTP_NOT_FOUND -> InvalidRequestException( |
| 64 | + stripeError, |
| 65 | + requestId, |
| 66 | + responseCode |
| 67 | + ) |
| 68 | + HttpURLConnection.HTTP_UNAUTHORIZED -> AuthenticationException(stripeError, requestId) |
| 69 | + HttpURLConnection.HTTP_FORBIDDEN -> PermissionException(stripeError, requestId) |
| 70 | + HTTP_TOO_MANY_REQUESTS -> RateLimitException(stripeError, requestId) |
| 71 | + else -> APIException(stripeError, requestId, responseCode) |
| 72 | + } |
| 73 | + } |
| 74 | +} |
0 commit comments