Modern Apps git

← Modern-Apps.git

calculator: read currency rates from the static R2 copy first (#static-r2)

Commit
245a187fde8fbc748ec71ef153082437f4dc9518
Author
Vayun Mathur <[email protected]>
When
2026-11-13 22:38 UTC (22 days ago)
Parents
07df9ad
What changed: CurrencyApi.rates() reads
https://static.maproj.org/rates/v1/currency.json first and falls back to
api.maproj.org/api/currency/rates when that copy is missing, failing,
undecodable, or older than two days (judged by its Last-Modified).

Why: the rates are the same JSON for every user (USD base from
open.er-api.com, refreshed about daily), yet every converter open cost
the server a request (146 a day today). The server
(location_share_server feat/static-r2, src/static_r2/snapshots.rs) now
re-renders the endpoint hourly and publishes the identical body to R2,
rewriting it at least every 6 h so Last-Modified stays honest. The
two-day limit means a server that stopped refreshing the copy is
noticed and asked directly.

Details: same DTO, same lenient JSON decoding as NetworkClient.getJson,
same IOException on a failing fallback, so UnitConverterTab's error
handling is unchanged. Still the app's only network call.

User-visible effect: none (rates load from Cloudflare's edge instead of
the origin server).

Verified: the policy is covered by StaticMirrorTest in library/network;
compiles with :calculator:compileDevKotlin in the container build.

Diff

calculator/src/main/java/com/vayunmathur/calculator/util/CurrencyApi.kt
diff --git a/calculator/src/main/java/com/vayunmathur/calculator/util/CurrencyApi.kt b/calculator/src/main/java/com/vayunmathur/calculator/util/CurrencyApi.ktindex b7df511d5d..a18f6b0c89 100644--- a/calculator/src/main/java/com/vayunmathur/calculator/util/CurrencyApi.kt+++ b/calculator/src/main/java/com/vayunmathur/calculator/util/CurrencyApi.kt@@ -1,11 +1,12 @@ package com.vayunmathur.calculator.util -import com.vayunmathur.library.network.NetworkClient+import com.vayunmathur.library.network.StaticMirror import kotlinx.serialization.Serializable  /**  * Live currency exchange rates from the self-hosted proxy on `api.maproj.org`, which- * fetches and caches them from a free upstream. Rates are "units of the currency per 1+ * fetches and caches them from a free upstream; read from the server's static copy on+ * R2 first (same JSON, refreshed hourly), with the endpoint as the fallback. Rates are "units of the currency per 1  * [base]" (base is USD, so USD == 1.0), which is exactly the `factorToBase` inverse the  * converter needs — see [UnitRegistry.currencyCategory].  */@@ -13,7 +14,13 @@ object CurrencyApi {      private const val URL = "https://api.maproj.org/api/currency/rates" -    suspend fun rates(): CurrencyRatesDto = NetworkClient.getJson(URL)+    /** The server's hourly copy of the same JSON on R2 (read first). */+    private const val STATIC_URL = "${StaticMirror.BASE}/rates/v1/currency.json"++    /** Older static copies mean the server stopped refreshing them: ask it directly. */+    private const val STATIC_MAX_AGE_MS = 2L * 24 * 60 * 60 * 1000++    suspend fun rates(): CurrencyRatesDto = StaticMirror.getJson(STATIC_URL, URL, STATIC_MAX_AGE_MS) }  @Serializable