shop: resolve merchant links via scoped lookup with raw-host fallback (#shop)
- Commit
e6d82443dd7c6ed074f4529ab6672f603c392173- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-09 03:18 UTC (27 days ago)
- Parents
7bc6d7e
The server replaced the public bulk GET /shop/v1/merchants directory with a scoped GET /shop/v1/merchant?host= lookup (learn-on-miss for validated storefronts, 404 for unknown hosts). Point the client at it. - ShopBackend/ShopHttpBackend: add merchantFor(host), hitting /shop/v1/merchant?host=. A 404 maps to success(null) so link opens never fail on a directory miss; other errors keep the existing envelope mapping. - ShopDtos: add the single-record ShopMerchant DTO and its MerchantListing mapper (no bulk response type). - data/MerchantListing: restore the model removed with the bulk call; it is referenced by the scoped lookup again. - ShopViewModel.openLink: resolve the link host through merchantFor, falling back to the raw host (and host-as-name) when the lookup misses or fails. - ShopBackendTest: scoped-hit decode test plus unknown-host 404 test. Verified with :shop:compileDevKotlin, :shop:testDevUnitTest (ShopBackendTest 11/11 green) and :shop:lint.
Diff
shop/src/main/java/com/vayunmathur/shop/data/MerchantListing.kt
diff --git a/shop/src/main/java/com/vayunmathur/shop/data/MerchantListing.kt b/shop/src/main/java/com/vayunmathur/shop/data/MerchantListing.ktnew file mode 100644index 0000000000..f8be39c47f--- /dev/null+++ b/shop/src/main/java/com/vayunmathur/shop/data/MerchantListing.kt@@ -0,0 +1,12 @@+package com.vayunmathur.shop.data++/** Directory metadata is a discovery lead, never merchant access or payment authorization. */+data class MerchantListing(+ val id: String,+ val name: String,+ val domain: String,+ val siteUrl: String,+ val description: String = "",+ val directoryStatus: String = "unknown",+ val directoryVerifiedAt: String? = null,+)
shop/src/main/java/com/vayunmathur/shop/network/ShopBackend.kt
diff --git a/shop/src/main/java/com/vayunmathur/shop/network/ShopBackend.kt b/shop/src/main/java/com/vayunmathur/shop/network/ShopBackend.ktindex c892be3673..aa13996b17 100644--- a/shop/src/main/java/com/vayunmathur/shop/network/ShopBackend.kt+++ b/shop/src/main/java/com/vayunmathur/shop/network/ShopBackend.kt@@ -4,6 +4,7 @@ import com.vayunmathur.library.network.NetworkClient import com.vayunmathur.shop.data.CartLine import com.vayunmathur.shop.data.CatalogOptionSelection import com.vayunmathur.shop.data.CheckoutInputs+import com.vayunmathur.shop.data.MerchantListing import com.vayunmathur.shop.data.OrderStatusQuery import com.vayunmathur.shop.data.OrderTracking import com.vayunmathur.shop.data.UcpCheckout@@ -44,6 +45,12 @@ interface ShopBackend { suspend fun updateCheckout(shop: String, sessionId: String, lines: List<CartLine>, inputs: CheckoutInputs, idempotencyKey: String): Result<UcpCheckout> /** Latest tracking per token; tokens without a merchant update yet are omitted. */ suspend fun orderStatus(orders: List<OrderStatusQuery>): Result<Map<String, OrderTracking>>+ /**+ * Scoped merchant lookup for an opened merchant link. Returns the+ * directory record, or null when the host is unknown (404) so callers+ * fall back to the raw link host. Never throws for a miss.+ */+ suspend fun merchantFor(host: String): Result<MerchantListing?> } class ShopBackendException(val error: UcpError) : Exception(error.toString())@@ -153,6 +160,18 @@ internal object ShopHttpBackend : ShopBackend { .toMap() } + override suspend fun merchantFor(host: String): Result<MerchantListing?> =+ catching {+ require(host.isNotBlank() && host.length <= 253) { "Invalid merchant host" }+ try {+ get("$BASE/merchant?host=${host.encode()}").parse<ShopMerchant>().toModel()+ } catch (error: ShopBackendException) {+ // Unknown host: the caller falls back to the raw link host.+ val http = error.error as? UcpError.Http+ if (http?.status == 404) null else throw error+ }+ }+ private const val MAX_STATUS_TOKENS = 50 private const val MAX_ORDER_ID_LENGTH = 500 private val TOKEN = Regex("^[0-9a-f]{32}$")
shop/src/main/java/com/vayunmathur/shop/network/ShopDtos.kt
diff --git a/shop/src/main/java/com/vayunmathur/shop/network/ShopDtos.kt b/shop/src/main/java/com/vayunmathur/shop/network/ShopDtos.ktindex 34d9c39670..5525cb027d 100644--- a/shop/src/main/java/com/vayunmathur/shop/network/ShopDtos.kt+++ b/shop/src/main/java/com/vayunmathur/shop/network/ShopDtos.kt@@ -3,6 +3,7 @@ package com.vayunmathur.shop.network import com.vayunmathur.shop.data.CartLine import com.vayunmathur.shop.data.CatalogOptionSelection import com.vayunmathur.shop.data.CheckoutInputs+import com.vayunmathur.shop.data.MerchantListing import com.vayunmathur.shop.data.Money import com.vayunmathur.shop.data.OrderStatusQuery import com.vayunmathur.shop.data.OrderTracking@@ -210,6 +211,18 @@ data class ShopProductResponse( val product: ShopProduct = ShopProduct(), ) +/** Single record from the scoped GET /shop/v1/merchant?host= lookup. */+@Serializable+data class ShopMerchant(+ val id: String = "",+ val name: String = "",+ val domain: String = "",+ val siteUrl: String = "",+ val description: String = "",+ val directoryStatus: String = "unknown",+ val directoryVerifiedAt: String? = null,+)+ @Serializable data class ShopErrorBody( val code: String = "",@@ -256,6 +269,16 @@ internal fun ShopProduct.toModel(): UcpProduct = UcpProduct( storeName = storeName, ) +internal fun ShopMerchant.toModel(): MerchantListing = MerchantListing(+ id = id,+ name = name,+ domain = domain,+ siteUrl = siteUrl,+ description = description,+ directoryStatus = directoryStatus,+ directoryVerifiedAt = directoryVerifiedAt,+)+ internal fun ShopCheckoutSession.toModel(): UcpCheckout = UcpCheckout( checkoutId = checkoutId, status = status,
shop/src/main/java/com/vayunmathur/shop/platform/ShopViewModel.kt
diff --git a/shop/src/main/java/com/vayunmathur/shop/platform/ShopViewModel.kt b/shop/src/main/java/com/vayunmathur/shop/platform/ShopViewModel.ktindex 64d7cb4847..0d2db9b0b6 100644--- a/shop/src/main/java/com/vayunmathur/shop/platform/ShopViewModel.kt+++ b/shop/src/main/java/com/vayunmathur/shop/platform/ShopViewModel.kt@@ -162,10 +162,11 @@ class ShopViewModel( val link = MerchantLinks.parse(url) ?: return viewModelScope.launch { AppInit.awaitReady()- // Raw-host fallback: the link host itself scopes the search. The- // former merchant-directory lookup is gone with the endpoint.- val domain = link.host- val name = domain+ // Scoped directory lookup; unknown hosts (or lookup failures)+ // fall back to the raw link host.+ val merchant = backend.merchantFor(link.host).getOrNull()+ val domain = merchant?.domain?.takeIf { it.isNotBlank() } ?: link.host+ val name = merchant?.name?.takeIf { it.isNotBlank() } ?: domain when (link) { is MerchantLink.Product -> backend.getProductByHandle(domain, link.handle).fold( onSuccess = { product ->
shop/src/test/java/com/vayunmathur/shop/network/ShopBackendTest.kt
diff --git a/shop/src/test/java/com/vayunmathur/shop/network/ShopBackendTest.kt b/shop/src/test/java/com/vayunmathur/shop/network/ShopBackendTest.ktindex 67cb1a50a0..5ba93b3c74 100644--- a/shop/src/test/java/com/vayunmathur/shop/network/ShopBackendTest.kt+++ b/shop/src/test/java/com/vayunmathur/shop/network/ShopBackendTest.kt@@ -140,6 +140,30 @@ class ShopBackendTest { return@runBlocking } + @Test fun merchantForDecodesScopedLookup() = runBlocking {+ var sentUrl: String? = null+ ShopHttpBackend.request = { method, url, _ ->+ assertEquals("GET", method)+ sentUrl = url+ ok("""{"id": "learned:shop.example.com", "name": "Trail Store", "domain": "shop.example.com",+ "siteUrl": "https://shop.example.com", "description": "Shoes",+ "directoryStatus": "learned", "directoryVerifiedAt": "2026-11-08T00:00:00Z"}""")+ }+ val merchant = ShopHttpBackend.merchantFor("shop.example.com").getOrThrow()+ assertTrue(sentUrl!!.endsWith("/merchant?host=shop.example.com"))+ assertEquals("shop.example.com", merchant!!.domain)+ assertEquals("Trail Store", merchant.name)+ assertEquals("learned", merchant.directoryStatus)+ return@runBlocking+ }++ @Test fun merchantForMapsUnknownHostToNull() = runBlocking {+ stub(BackendRawResponse(404, """{"error":{"code":"not_found","message":"Unknown merchant host"}}""", emptyMap(), null))+ val merchant = ShopHttpBackend.merchantFor("unknown.example.com").getOrThrow()+ assertEquals(null, merchant)+ return@runBlocking+ }+ @Test fun errorEnvelopeMapsToUcpErrors() = runBlocking { suspend fun errorFor(code: String, status: Int): UcpError? { val body = ShopHttpBackend.json.encodeToString(ShopErrorEnvelope(ShopErrorBody(code, "m", null)))