Skip to content

Commit

Permalink
Catch exceptions from http client, related #11
Browse files Browse the repository at this point in the history
  • Loading branch information
juraj-hrivnak committed May 5, 2024
1 parent df01f0b commit 0474652
Showing 1 changed file with 63 additions and 19 deletions.
82 changes: 63 additions & 19 deletions src/commonMain/kotlin/teksturepako/pakku/api/http/Http.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,45 +18,89 @@ open class Http
url: String, onDownload: (bytesSentTotal: Long, contentLength: Long) -> Unit = { _: Long, _: Long ->}
): ByteArray?
{
return client.get(url) {
onDownload { bytesSentTotal, contentLength -> onDownload(bytesSentTotal, contentLength) }
return try
{
client.get(url) {
onDownload { bytesSentTotal, contentLength -> onDownload(bytesSentTotal, contentLength) }
}
.debug { println("${this::class.simpleName} $it") }
.checkLimit()
.bodyIfOK()
}
catch (e: Exception)
{
println(
"Error: ${this@Http::class.simpleName} " +
"HTTP request failed with exception: (${e.javaClass}) ${e.message}"
)
null
}
.debug { println("${this::class.simpleName} $it") }
.checkLimit()
.bodyIfOK()
}

/**
* @return A body [String] of a https request or null if status code is not OK.
*/
open suspend fun requestBody(url: String): String?
{
return client.get(url)
.debug { println("${this::class.simpleName} $it") }
.checkLimit()
.bodyIfOK()
return try
{
client.get(url)
.debug { println("${this::class.simpleName} $it") }
.checkLimit()
.bodyIfOK()
}
catch (e: Exception)
{
println(
"Error: ${this@Http::class.simpleName} " +
"HTTP request failed with exception: (${e.javaClass}) ${e.message}"
)
null
}
}

/**
* @return A body [String] of a https request, with headers provided or null if status code is not OK.
*/
suspend fun requestBody(url: String, vararg headers: Pair<String, String>): String?
{
return client.get(url) { headers.forEach { this.headers.append(it.first, it.second) } }
.debug { println("${this::class.simpleName} $it") }
.checkLimit()
.bodyIfOK()
return try
{
client.get(url) { headers.forEach { this.headers.append(it.first, it.second) } }
.debug { println("${this::class.simpleName} $it") }
.checkLimit()
.bodyIfOK()
}
catch (e: Exception)
{
println(
"Error: ${this@Http::class.simpleName} " +
"HTTP request failed with exception: (${e.javaClass}) ${e.message}"
)
null
}
}

suspend inline fun <reified T> requestBody(url: String, bodyContent: T): String?
{
return client.post(url) {
contentType(ContentType.Application.Json)
setBody(Json.encodeToString(bodyContent)) // Don't use pretty print
return try
{
client.post(url) {
contentType(ContentType.Application.Json)
setBody(Json.encodeToString(bodyContent)) // Don't use pretty print
}
.debug { println("${this::class.simpleName} ${it.call} ${it.request.content}") }
.checkLimit()
.bodyIfOK()
}
catch (e: Exception)
{
println(
"Error: ${this@Http::class.simpleName} " +
"HTTP request failed with exception: (${e.javaClass}) ${e.message}"
)
null
}
.debug { println("${this::class.simpleName} ${it.call} ${it.request.content}") }
.checkLimit()
.bodyIfOK()
}

open suspend fun HttpResponse.checkLimit(): HttpResponse = this
Expand Down

0 comments on commit 0474652

Please sign in to comment.