Skip to content

Commit

Permalink
Merge pull request #101 from metaplex-foundation/martini/fix-http-err…
Browse files Browse the repository at this point in the history
…or-handling-my-mistake
  • Loading branch information
ajamaica authored Feb 17, 2023
2 parents f492700 + 6b719e0 commit 0aa7adf
Showing 1 changed file with 10 additions and 11 deletions.
21 changes: 10 additions & 11 deletions lib/src/main/java/com/metaplex/lib/drivers/network/JdkHttpDriver.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ package com.metaplex.lib.drivers.network
import kotlinx.coroutines.suspendCancellableCoroutine
import java.net.HttpURLConnection
import java.net.URL
import kotlin.coroutines.resume

/**
* A [HttpNetworkDriver] implementation using the native JDK [HttpURLConnection]
Expand All @@ -34,25 +35,23 @@ class JdkHttpDriver : HttpNetworkDriver {
continuation.invokeOnCancellation { disconnect() }

// send request body
request.body?.run {
request.body?.runCatching {
doOutput = true
outputStream.write(toByteArray(Charsets.UTF_8))
outputStream.flush()
outputStream.close()
}?.onFailure { error ->
continuation.resume(error.toString())
return@with
}

// read response
val response = (inputStream ?: errorStream)?.bufferedReader()?.use {
it.readText()
}?.let { responseString -> Result.success(responseString) }
?: Result.failure(Throwable("No Response"))

// TODO: should check response code and/or errorStream for errors
// println("URL : $url")
// println("Response Code : $responseCode")
// println("input stream : $responseString")
val responseString = when (responseCode) {
HttpURLConnection.HTTP_OK -> inputStream.bufferedReader().use { it.readText() }
else -> errorStream.bufferedReader().use { it.readText() }
}

continuation.resumeWith(response)
continuation.resume(responseString)
}
}
}

0 comments on commit 0aa7adf

Please # to comment.