Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return CustomerEphemeralKey.Available directly rather than have consumers handle it. #9366

Merged
merged 1 commit into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,3 @@ internal sealed interface CachedCustomerEphemeralKey {
}
}
}

internal fun <T> Result<CachedCustomerEphemeralKey>.mapWithEphemeralKeyCatching(
mapper: (customerId: String, ephemeralKey: String) -> T
): Result<T> {
return mapCatching { cachedKey ->
when (cachedKey) {
is CachedCustomerEphemeralKey.Available -> mapper(cachedKey.customerId, cachedKey.ephemeralKey)
is CachedCustomerEphemeralKey.None -> throw IllegalStateException("No ephemeral key was available!")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import javax.inject.Singleton
import kotlin.coroutines.CoroutineContext

internal interface CustomerSessionElementsSessionManager {
suspend fun fetchCustomerSessionEphemeralKey(): Result<CachedCustomerEphemeralKey>
suspend fun fetchCustomerSessionEphemeralKey(): Result<CachedCustomerEphemeralKey.Available>

suspend fun fetchElementsSession(): Result<ElementsSession>
}
Expand All @@ -34,16 +34,23 @@ internal class DefaultCustomerSessionElementsSessionManager @Inject constructor(

private var intentConfiguration: CustomerSheet.IntentConfiguration? = null

override suspend fun fetchCustomerSessionEphemeralKey(): Result<CachedCustomerEphemeralKey> {
override suspend fun fetchCustomerSessionEphemeralKey(): Result<CachedCustomerEphemeralKey.Available> {
return withContext(workContext) {
cachedCustomerEphemeralKey.takeUnless { cachedCustomerEphemeralKey ->
cachedCustomerEphemeralKey.shouldRefresh(timeProvider())
}?.let {
Result.success(it)
} ?: kotlin.runCatching {
fetchElementsSession().getOrThrow()
runCatching {
val ephemeralKey = cachedCustomerEphemeralKey.takeUnless { cachedCustomerEphemeralKey ->
cachedCustomerEphemeralKey.shouldRefresh(timeProvider())
} ?: run {
fetchElementsSession().getOrThrow()

cachedCustomerEphemeralKey
}

cachedCustomerEphemeralKey
when (ephemeralKey) {
is CachedCustomerEphemeralKey.Available -> ephemeralKey
is CachedCustomerEphemeralKey.None -> throw IllegalStateException(
"No ephemeral key available!"
)
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ internal class CustomerSessionSavedSelectionDataSource @Inject constructor(
}

private suspend fun createPrefsRepository(): CustomerSheetDataResult<PrefsRepository> {
return elementsSessionManager.fetchCustomerSessionEphemeralKey().mapWithEphemeralKeyCatching { customerId, _ ->
prefsRepositoryFactory(customerId)
return elementsSessionManager.fetchCustomerSessionEphemeralKey().mapCatching { ephemeralKey ->
prefsRepositoryFactory(ephemeralKey.customerId)
}.toCustomerSheetDataResult()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ package com.stripe.android.customersheet.data
import com.stripe.android.model.ElementsSession

internal class FakeCustomerSessionElementsSessionManager(
private val ephemeralKey: Result<CachedCustomerEphemeralKey> = Result.success(
private val ephemeralKey: Result<CachedCustomerEphemeralKey.Available> = Result.success(
CachedCustomerEphemeralKey.Available(
customerId = "cus_1",
ephemeralKey = "ek_123",
expiresAt = 999999,
)
),
) : CustomerSessionElementsSessionManager {
override suspend fun fetchCustomerSessionEphemeralKey(): Result<CachedCustomerEphemeralKey> {
override suspend fun fetchCustomerSessionEphemeralKey(): Result<CachedCustomerEphemeralKey.Available> {
return ephemeralKey
}

Expand Down
Loading