mirror of
https://github.com/InsanusMokrassar/SauceNaoAPI.git
synced 2024-12-23 13:17:11 +00:00
async work of SauceNaoAPI requests
This commit is contained in:
parent
bd12aa5610
commit
df5fbef909
@ -17,6 +17,7 @@
|
|||||||
* Add `TimeManager` - it will manage work with requests times
|
* Add `TimeManager` - it will manage work with requests times
|
||||||
* Add `RequestQuotaMagager` - it will manage quota for requests and call suspend
|
* Add `RequestQuotaMagager` - it will manage quota for requests and call suspend
|
||||||
if they will be over
|
if they will be over
|
||||||
|
* `SauceNaoAPI` now working (almost) asynchronously
|
||||||
|
|
||||||
## 0.3.0
|
## 0.3.0
|
||||||
|
|
||||||
|
@ -45,18 +45,19 @@ data class SauceNaoAPI(
|
|||||||
|
|
||||||
private val requestsJob = scope.launch {
|
private val requestsJob = scope.launch {
|
||||||
for ((callback, requestBuilder) in requestsChannel) {
|
for ((callback, requestBuilder) in requestsChannel) {
|
||||||
try {
|
quotaManager.getQuota()
|
||||||
quotaManager.getQuota()
|
launch {
|
||||||
|
|
||||||
val answer = makeRequest(requestBuilder)
|
|
||||||
callback.resumeWith(Result.success(answer))
|
|
||||||
|
|
||||||
quotaManager.updateQuota(answer.header, timeManager)
|
|
||||||
} catch (e: Exception) {
|
|
||||||
try {
|
try {
|
||||||
callback.resumeWith(Result.failure(e))
|
val answer = makeRequest(requestBuilder)
|
||||||
} catch (e: IllegalStateException) { // may happen when already resumed and api was closed
|
callback.resumeWith(Result.success(answer))
|
||||||
// do nothing
|
|
||||||
|
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
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user