From df5fbef909aae55ee98505398ba078d9e8afb810 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sat, 12 Oct 2019 12:32:17 +0600 Subject: [PATCH] async work of SauceNaoAPI requests --- CHANGELOG.md | 1 + .../SauceNaoAPI/SauceNaoAPI.kt | 23 ++++++++++--------- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b41b734..0a1a7a0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,7 @@ * Add `TimeManager` - it will manage work with requests times * Add `RequestQuotaMagager` - it will manage quota for requests and call suspend if they will be over +* `SauceNaoAPI` now working (almost) asynchronously ## 0.3.0 diff --git a/src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/SauceNaoAPI.kt b/src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/SauceNaoAPI.kt index 446fd14..e8e6dd6 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/SauceNaoAPI.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/SauceNaoAPI/SauceNaoAPI.kt @@ -45,18 +45,19 @@ data class SauceNaoAPI( private val requestsJob = scope.launch { for ((callback, requestBuilder) in requestsChannel) { - try { - quotaManager.getQuota() - - val answer = makeRequest(requestBuilder) - callback.resumeWith(Result.success(answer)) - - quotaManager.updateQuota(answer.header, timeManager) - } catch (e: Exception) { + quotaManager.getQuota() + launch { try { - callback.resumeWith(Result.failure(e)) - } catch (e: IllegalStateException) { // may happen when already resumed and api was closed - // do nothing + val answer = makeRequest(requestBuilder) + callback.resumeWith(Result.success(answer)) + + quotaManager.updateQuota(answer.header, timeManager) + } catch (e: Exception) { + try { + callback.resumeWith(Result.failure(e)) + } catch (e: IllegalStateException) { // may happen when already resumed and api was closed + // do nothing + } } } }