mirror of
https://github.com/mollyim/monero-wallet-sdk.git
synced 2025-05-13 05:30:43 +01:00
lib: fix 499 response when no remote node is available
This commit is contained in:
parent
da50496271
commit
7f167fb145
1 changed files with 36 additions and 47 deletions
|
@ -112,6 +112,23 @@ class RemoteNodeClient private constructor(
|
||||||
header: String?,
|
header: String?,
|
||||||
body: ByteArray?,
|
body: ByteArray?,
|
||||||
): Response {
|
): Response {
|
||||||
|
val headers = parseHttpHeader(header)
|
||||||
|
val contentType = headers["Content-Type"]?.toMediaType()
|
||||||
|
// TODO: Log unsupported headers
|
||||||
|
val requestBuilder = with(Request.Builder()) {
|
||||||
|
when {
|
||||||
|
method.equals("GET", ignoreCase = true) -> {}
|
||||||
|
method.equals("POST", ignoreCase = true) -> {
|
||||||
|
val content = body ?: ByteArray(0)
|
||||||
|
post(content.toRequestBody(contentType))
|
||||||
|
}
|
||||||
|
|
||||||
|
else -> throw IllegalArgumentException("Unsupported method")
|
||||||
|
}
|
||||||
|
url("http:$path")
|
||||||
|
// TODO: Add authentication
|
||||||
|
}
|
||||||
|
|
||||||
val attempts = mutableMapOf<Uri, Int>()
|
val attempts = mutableMapOf<Uri, Int>()
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
|
@ -119,7 +136,12 @@ class RemoteNodeClient private constructor(
|
||||||
if (selected == null) {
|
if (selected == null) {
|
||||||
logger.i("No remote node available")
|
logger.i("No remote node available")
|
||||||
|
|
||||||
return Response.Builder().code(499).build()
|
return Response.Builder()
|
||||||
|
.request(requestBuilder.build())
|
||||||
|
.protocol(Protocol.HTTP_1_1)
|
||||||
|
.code(499)
|
||||||
|
.message("No remote node available")
|
||||||
|
.build()
|
||||||
}
|
}
|
||||||
|
|
||||||
val uri = selected.uriForPath(path)
|
val uri = selected.uriForPath(path)
|
||||||
|
@ -128,14 +150,9 @@ class RemoteNodeClient private constructor(
|
||||||
delay(retryBackoff.waitTime(retryCount))
|
delay(retryBackoff.waitTime(retryCount))
|
||||||
|
|
||||||
val response = try {
|
val response = try {
|
||||||
executeCall(
|
val request = requestBuilder.url(uri.toString()).build()
|
||||||
method = method,
|
|
||||||
uri = uri,
|
httpClient.newCall(request).await()
|
||||||
username = selected.username,
|
|
||||||
password = selected.password,
|
|
||||||
header = header,
|
|
||||||
body = body,
|
|
||||||
)
|
|
||||||
} catch (e: IOException) {
|
} catch (e: IOException) {
|
||||||
logger.e("HTTP: Request failed", e)
|
logger.e("HTTP: Request failed", e)
|
||||||
// TODO: Notify loadBalancer
|
// TODO: Notify loadBalancer
|
||||||
|
@ -151,33 +168,6 @@ class RemoteNodeClient private constructor(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private suspend fun executeCall(
|
|
||||||
method: String?,
|
|
||||||
uri: Uri,
|
|
||||||
username: String?,
|
|
||||||
password: String?,
|
|
||||||
header: String?,
|
|
||||||
body: ByteArray?,
|
|
||||||
): Response {
|
|
||||||
val headers = parseHttpHeader(header)
|
|
||||||
val contentType = headers["Content-Type"]?.toMediaType()
|
|
||||||
// TODO: Log unsupported headers
|
|
||||||
val request = with(Request.Builder()) {
|
|
||||||
when {
|
|
||||||
method.equals("GET", ignoreCase = true) -> {}
|
|
||||||
method.equals("POST", ignoreCase = true) -> {
|
|
||||||
val content = body ?: ByteArray(0)
|
|
||||||
post(content.toRequestBody(contentType))
|
|
||||||
}
|
|
||||||
else -> throw IllegalArgumentException("Unsupported method")
|
|
||||||
}
|
|
||||||
// TODO: Add authentication
|
|
||||||
url(uri.toString())
|
|
||||||
build()
|
|
||||||
}
|
|
||||||
return httpClient.newCall(request).await()
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun parseHttpHeader(header: String?): Headers =
|
private fun parseHttpHeader(header: String?): Headers =
|
||||||
with(Headers.Builder()) {
|
with(Headers.Builder()) {
|
||||||
header?.splitToSequence("\r\n")
|
header?.splitToSequence("\r\n")
|
||||||
|
@ -186,18 +176,17 @@ class RemoteNodeClient private constructor(
|
||||||
build()
|
build()
|
||||||
}
|
}
|
||||||
|
|
||||||
private suspend fun Call.await() =
|
private suspend fun Call.await() = suspendCoroutine { continuation ->
|
||||||
suspendCoroutine { continuation ->
|
enqueue(object : Callback {
|
||||||
enqueue(object : Callback {
|
override fun onResponse(call: Call, response: Response) {
|
||||||
override fun onResponse(call: Call, response: Response) {
|
continuation.resume(response)
|
||||||
continuation.resume(response)
|
}
|
||||||
}
|
|
||||||
|
|
||||||
override fun onFailure(call: Call, e: IOException) {
|
override fun onFailure(call: Call, e: IOException) {
|
||||||
continuation.resumeWithException(e)
|
continuation.resumeWithException(e)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// private val Response.roundTripMillis: Long
|
// private val Response.roundTripMillis: Long
|
||||||
// get() = sentRequestAtMillis() - receivedResponseAtMillis()
|
// get() = sentRequestAtMillis() - receivedResponseAtMillis()
|
||||||
|
|
Loading…
Reference in a new issue