Modern Apps git
git clone https://git.maproj.org/Modern-Apps.git
translate ▾

Modern-Apps.git / communicate-dev / src / main / java / com / vayunmathur / communicate / data / whatsapp / mex / WhatsAppWwwGraphQlClient.kt

WhatsAppWwwGraphQlClient.kt65 lines · 2.9 KiBRaw
1package com.vayunmathur.communicate.data.whatsapp.mex
2
3import android.content.Context
4import android.os.Build
5import com.vayunmathur.communicate.data.whatsapp.WhatsAppDiag
6import com.vayunmathur.communicate.data.whatsapp.WhatsAppProtocol
7import com.vayunmathur.library.network.NetworkClient
8
9/**
10 * WWW GraphQL HTTP transport — the fallback used when the XMPP/`w:mex` socket is not connected
11 * (w2.md §4.1 / §5.3). Posts the persisted-query envelope to `graph.whatsapp.com/graphql` over the
12 * repo's own HTTP stack ([NetworkClient], no OkHttp).
13 *
14 * The body is built JVM-pure by [MexEnvelope.buildWwwBody]:
15 * `{"access_token":…,"doc_id":<int64>,"lang":"en_US","Content-Type":"application/json","variables":{…}}`.
16 * Unauthenticated calls use the consumer client token from §4.1; there is no `Authorization` header
17 * (auth lives in the body).
18 *
19 * Dev-only, reached via [WhatsAppMex]; gated at the repository layer by `WhatsAppFeature.enabled`.
20 */
21object WhatsAppWwwGraphQlClient {
22
23 private const val TAG = "WAMex"
24 private const val URL = "https://graph.whatsapp.com/graphql"
25 private const val LOG_SNIPPET = 200
26
27 /** Consumer/wearos/orbit/vr client token (w2.md §4.1 `GraphqlRequestBase.kt:618-636`). */
28 private const val CLIENT_TOKEN = "WA|1015890928915437|3201f239340c1c8ec6262a6dad04200e"
29
30 /**
31 * POST the given [docId] + [variablesJson] to the WWW GraphQL endpoint and parse the
32 * `{"data":…,"errors":…}` envelope into a [MexResult]. Transport failures (network, non-2xx)
33 * surface as [MexResult.transport].
34 */
35 suspend fun post(docId: String, variablesJson: String): MexResult {
36 val body = MexEnvelope.buildWwwBody(CLIENT_TOKEN, docId, variablesJson)
37 val headers = mapOf(
38 "Content-Type" to "application/json",
39 "User-Agent" to userAgent(),
40 )
41 return try {
42 val resp = NetworkClient.performRequest(
43 url = URL,
44 method = "POST",
45 headers = headers,
46 body = body,
47 useSystemTrust = true,
48 )
49 if (!resp.isSuccess) {
50 WhatsAppDiag.log(TAG, "www[$docId]: HTTP ${resp.status}: ${resp.body.take(LOG_SNIPPET)}")
51 return MexResult.transport("http:${resp.status}")
52 }
53 MexResult.fromEnvelope(resp.body)
54 } catch (expected: Throwable) {
55 WhatsAppDiag.log(TAG, "www[$docId]: request failed: ${expected.message}")
56 MexResult.transport("network:${expected.message}")
57 }
58 }
59
60 /** WhatsApp/<ver> Android/<osrel> Device/<manufacturer>-<model> — same shape as registration. */
61 private fun userAgent(): String {
62 val device = "${Build.MANUFACTURER}-${Build.MODEL}".replace(' ', '_')
63 return "WhatsApp/${WhatsAppProtocol.WA_VERSION_NAME} Android/${Build.VERSION.RELEASE} Device/$device"
64 }
65}