Fixed potential resource leak
This commit is contained in:
parent
eefd4ee615
commit
2c271d9116
1 changed files with 19 additions and 18 deletions
|
@ -99,25 +99,26 @@ open class CryptoPrice(val base: String, val currency: String, val amount: BigDe
|
||||||
}.build()
|
}.build()
|
||||||
|
|
||||||
val request = Request.Builder().url(httpUrl).build()
|
val request = Request.Builder().url(httpUrl).build()
|
||||||
val response = client.newCall(request).execute()
|
client.newCall(request).execute().use { response ->
|
||||||
val body = response.body?.string() ?: throw CryptoException(
|
val body = response.body?.string() ?: throw CryptoException(
|
||||||
response.code,
|
response.code,
|
||||||
id = "empty_response",
|
id = "empty_response",
|
||||||
message = "Empty response."
|
message = "Empty response."
|
||||||
)
|
)
|
||||||
try {
|
try {
|
||||||
val json = JSONObject(body)
|
val json = JSONObject(body)
|
||||||
if (response.isSuccessful) {
|
if (response.isSuccessful) {
|
||||||
return body
|
return body
|
||||||
} else {
|
} else {
|
||||||
val data = json.getJSONArray("errors")
|
val data = json.getJSONArray("errors")
|
||||||
throw CryptoException(
|
throw CryptoException(
|
||||||
response.code, data.getJSONObject(0).getString("id"),
|
response.code, data.getJSONObject(0).getString("id"),
|
||||||
data.getJSONObject(0).getString("message")
|
data.getJSONObject(0).getString("message")
|
||||||
)
|
)
|
||||||
|
}
|
||||||
|
} catch (e: JSONException) {
|
||||||
|
throw CryptoException(response.code, id = "parse_error", "Could not parse data.", e)
|
||||||
}
|
}
|
||||||
} catch (e: JSONException) {
|
|
||||||
throw CryptoException(response.code, id = "parse_error", "Could not parse data.", e)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue