async work of SauceNaoAPI requests

This commit is contained in:
InsanusMokrassar 2019-10-12 12:32:17 +06:00
parent bd12aa5610
commit df5fbef909
2 changed files with 13 additions and 11 deletions

View File

@ -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

View File

@ -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
}
}
}
}