globalesim: read the rate card from the static R2 copy first (#static-r2)
- Commit
50cc670ab9016a13b2b2bd401efcceaa4e1ecd27- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-13 22:38 UTC (22 days ago)
- Parents
245a187
What changed: EsimApi.rates() reads https://static.maproj.org/rates/v1/esim.json first and falls back to /esim/v1/rates on api.maproj.org when the copy is missing, failing, undecodable, or older than two days (Last-Modified). Why: the rate card is the same for every account (already scaled to the wallet's face value server-side), so the server (location_share_server feat/static-r2, src/static_r2/snapshots.rs) renders /esim/v1/rates hourly through its own handler and publishes the identical JSON to R2. The Pricing tab then no longer costs a server request per user (vendor fetch mean 209 ms, 29% cache hit on the server today). Details: - Still wrapped in wrapApiErrors, so a failing fallback surfaces as the same ApiError(statusCode) the ViewModel already handles. - With baseUrlOverride set (the hermetic offline tests) the static copy is looked up under the override as well, so tests never reach the real network. - Account-specific calls (status, top-ups, transfers) are untouched and stay on the server. User-visible effect: none. Verified: StaticMirrorTest covers the fallback policy; existing ApiErrorTest / EsimViewModelTest run with :globalesim:testDevUnitTest in the container build.
Diff
globalesim/src/main/java/com/vayunmathur/globalesim/data/api/EsimApi.kt
diff --git a/globalesim/src/main/java/com/vayunmathur/globalesim/data/api/EsimApi.kt b/globalesim/src/main/java/com/vayunmathur/globalesim/data/api/EsimApi.ktindex b962dcb649..656f29e2b9 100644--- a/globalesim/src/main/java/com/vayunmathur/globalesim/data/api/EsimApi.kt+++ b/globalesim/src/main/java/com/vayunmathur/globalesim/data/api/EsimApi.kt@@ -1,6 +1,7 @@ package com.vayunmathur.globalesim.data.api import com.vayunmathur.library.network.NetworkClient+import com.vayunmathur.library.network.StaticMirror import kotlinx.serialization.json.Json import kotlinx.serialization.serializer @@ -56,7 +57,23 @@ object EsimApi { private suspend inline fun <reified Res> get(path: String): Res = wrapApiErrors { NetworkClient.getJson(url(path)) } - suspend fun rates(): RatesResponse = get("/esim/v1/rates")+ /**+ * The rate card is the same for every account, so the server keeps an hourly+ * copy on R2; read it first and fall back to the endpoint when it is missing or+ * more than two days old. With [baseUrlOverride] set (tests) the static copy is+ * looked up under the override too, so nothing leaves the test sandbox.+ */+ suspend fun rates(): RatesResponse = wrapApiErrors {+ StaticMirror.getJson(+ staticUrl = (baseUrlOverride ?: StaticMirror.BASE) + STATIC_RATES_PATH,+ fallbackUrl = url("/esim/v1/rates"),+ deserializer = serializer<RatesResponse>(),+ maxAgeMs = STATIC_RATES_MAX_AGE_MS,+ )+ }++ private const val STATIC_RATES_PATH = "/rates/v1/esim.json"+ private const val STATIC_RATES_MAX_AGE_MS = 2L * 24 * 60 * 60 * 1000 suspend fun status(accountId: String): StatusResponse = post("/esim/v1/status", AccountRequest(accountId))