Skip to content

Commit

Permalink
js tonapi bridge
Browse files Browse the repository at this point in the history
polstianka committed Oct 2, 2024
1 parent b401994 commit 9bc62bb
Showing 2 changed files with 52 additions and 10 deletions.
9 changes: 2 additions & 7 deletions apps/wallet/api/src/main/java/com/tonapps/wallet/api/API.kt
Original file line number Diff line number Diff line change
@@ -117,7 +117,7 @@ class API(
suspend fun tonapiFetch(
url: String,
options: String
): String = withContext(Dispatchers.IO) {
): Response = withContext(Dispatchers.IO) {
val uri = url.toUriOrNull() ?: throw Exception("Invalid URL")
if (uri.scheme != "https") {
throw Exception("Invalid scheme. Should be https")
@@ -150,12 +150,7 @@ class API(
builder.post(bodyOptions.toRequestBody(contentTypeOptions.toMediaType()))
}

val response = tonAPIHttpClient.newCall(builder.build()).execute()
if (response.isSuccessful) {
response.body?.string() ?: throw Exception("Empty response")
} else {
throw Exception("Failed request: ${response.code}")
}
tonAPIHttpClient.newCall(builder.build()).execute()
}

private val provider: Provider by lazy {
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.tonapps.tonkeeper.ui.screen.browser.dapp

import com.tonapps.tonkeeper.manager.tonconnect.ConnectRequest
import okhttp3.Headers
import okhttp3.Response
import org.json.JSONArray
import org.json.JSONObject
import uikit.widget.webview.bridge.JsBridge
@@ -14,7 +16,7 @@ class DAppBridge(
val connect: suspend (protocolVersion: Int, request: ConnectRequest) -> JSONObject,
val restoreConnection: suspend () -> JSONObject,
val disconnect: suspend () -> Unit,
val tonapiFetch: suspend (url: String, options: String) -> String
val tonapiFetch: suspend (url: String, options: String) -> Response
): JsBridge("tonkeeper") {

override val availableFunctions = arrayOf("send", "connect", "restoreConnection", "disconnect")
@@ -31,11 +33,43 @@ class DAppBridge(
"send" -> send(args).toString()
"restoreConnection" -> restoreConnection().toString()
"disconnect" -> disconnect()
"tonapi.fetch" -> tonapiFetch(args.getString(0), args.optString(1) ?: "")
"tonapi.fetch" -> {
val response = tonapiFetch(args.getString(0), args.optString(1) ?: "")
webAPIResponse(response).toString()
}
else -> null
}
}

private fun webAPIResponse(response: Response): JSONObject {
val body = response.body?.string() ?: ""
val json = JSONObject()
json.put("body", body)
json.put("ok", response.isSuccessful)
json.put("status", response.code)
json.put("statusText", response.message)
json.put("type", webAPIResponseType(response.code))
json.put("headers", webAPIResponseHeaders(response.headers))
json.put("redirected", response.isRedirect)
json.put("url", response.request.url.toString())
return json
}

private fun webAPIResponseHeaders(headers: Headers): JSONObject {
val json = JSONObject()
for (i in 0 until headers.size) {
json.put(headers.name(i), headers.value(i))
}
return json
}

private fun webAPIResponseType(code: Int): String {
return when (code) {
0 -> "error"
else -> "cors"
}
}

override fun jsInjection(): String {
val funcs = availableFunctions.joinToString(",") {"""
$it: (...args) => {
@@ -109,7 +143,20 @@ class DAppBridge(
window.tonapi = {
fetch: async (url, options) => {
return new Promise((resolve, reject) => {
window.invokeRnFunc('tonapi.fetch', [url, options], resolve, reject)
window.invokeRnFunc('tonapi.fetch', [url, options], (result) => {
try {
const json = JSON.parse(result);
const headers = new Headers(json.headers);
const response = new Response(json.body, {
status: json.status,
statusText: json.statusText,
headers: headers
});
resolve(response);
} catch (e) {
reject(e);
}
}, reject)
});
}
};

0 comments on commit 9bc62bb

Please sign in to comment.