Reviews closing of response.body calls

This commit is contained in:
Vitor Pamplona
2023-07-25 11:20:27 -04:00
parent aab42bb341
commit a5e40d5ac9
4 changed files with 23 additions and 18 deletions
@@ -80,8 +80,8 @@ class CashuProcessor {
.post(requestBody) .post(requestBody)
.build() .build()
val response = client.newCall(request).execute() client.newCall(request).execute().use {
val body = response.body.string() val body = it.body.string()
val tree = jacksonObjectMapper().readTree(body) val tree = jacksonObjectMapper().readTree(body)
val successful = tree?.get("paid")?.asText() == "true" val successful = tree?.get("paid")?.asText() == "true"
@@ -91,6 +91,7 @@ class CashuProcessor {
} else { } else {
onError(tree?.get("detail")?.asText()?.split('.')?.getOrNull(0) ?: "Cashu: Tokens already spent.") onError(tree?.get("detail")?.asText()?.split('.')?.getOrNull(0) ?: "Cashu: Tokens already spent.")
} }
}
} catch (e: Exception) { } catch (e: Exception) {
onError("Token melt failure: " + e.message) onError("Token melt failure: " + e.message)
} }
@@ -56,7 +56,9 @@ class RegisterAccounts(
val client = HttpClient.getHttpClient() val client = HttpClient.getHttpClient()
client.newCall(request).execute() val isSucess = client.newCall(request).execute().use {
it.isSuccessful
}
} catch (e: java.lang.Exception) { } catch (e: java.lang.Exception) {
Log.e("FirebaseMsgService", "Unable to register with push server", e) Log.e("FirebaseMsgService", "Unable to register with push server", e)
} }
@@ -63,7 +63,9 @@ private const val CONTENT = "content"
suspend fun getDocument(url: String, timeOut: Int = 30000): Document = suspend fun getDocument(url: String, timeOut: Int = 30000): Document =
withContext(Dispatchers.IO) { withContext(Dispatchers.IO) {
val request: Request = Request.Builder().url(url).get().build() val request: Request = Request.Builder().url(url).get().build()
val html = HttpClient.getHttpClient().newCall(request).execute().body.string() val html = HttpClient.getHttpClient().newCall(request).execute().use {
it.body.string()
}
Jsoup.parse(html) Jsoup.parse(html)
} }
@@ -234,9 +234,8 @@ class NostrCheckMeServer : FileServer() {
.build() .build()
while (!isCompleted) { while (!isCompleted) {
val response = client.newCall(request).execute() client.newCall(request).execute().use {
val body = response.body?.string() val tree = jacksonObjectMapper().readTree(it.body.string())
val tree = jacksonObjectMapper().readTree(body)
isCompleted = tree?.get("status")?.asText() == "completed" isCompleted = tree?.get("status")?.asText() == "completed"
try { try {
Thread.sleep(500) Thread.sleep(500)
@@ -244,6 +243,7 @@ class NostrCheckMeServer : FileServer() {
e.printStackTrace() e.printStackTrace()
} }
} }
}
return url return url
} }