mirror of
https://github.com/InsanusMokrassar/SauceNaoAPI.git
synced 2024-12-22 20:57:12 +00:00
LimitsState
This commit is contained in:
parent
0c2c7e9e50
commit
541134d8a7
@ -17,6 +17,8 @@
|
||||
* Uploading of file
|
||||
* Updates of versions
|
||||
* Now `SauceNaoAPI` do not require api key
|
||||
* `SauceNaoAPI` instances now can return `limitsState` object, which will contains `LimitsState` with currently known
|
||||
state of limits
|
||||
|
||||
### 0.4.3
|
||||
|
||||
|
@ -2,11 +2,9 @@ package com.github.insanusmokrassar.SauceNaoAPI
|
||||
|
||||
import com.github.insanusmokrassar.SauceNaoAPI.exceptions.TooManyRequestsException
|
||||
import com.github.insanusmokrassar.SauceNaoAPI.exceptions.sauceNaoAPIException
|
||||
import com.github.insanusmokrassar.SauceNaoAPI.models.SauceNaoAnswer
|
||||
import com.github.insanusmokrassar.SauceNaoAPI.models.SauceNaoAnswerSerializer
|
||||
import com.github.insanusmokrassar.SauceNaoAPI.models.*
|
||||
import com.github.insanusmokrassar.SauceNaoAPI.utils.*
|
||||
import io.ktor.client.HttpClient
|
||||
import io.ktor.client.call.UnsupportedContentTypeException
|
||||
import io.ktor.client.call.call
|
||||
import io.ktor.client.engine.okhttp.OkHttp
|
||||
import io.ktor.client.features.ClientRequestException
|
||||
@ -14,12 +12,12 @@ import io.ktor.client.request.*
|
||||
import io.ktor.client.request.forms.*
|
||||
import io.ktor.client.response.readText
|
||||
import io.ktor.http.*
|
||||
import io.ktor.http.content.OutgoingContent
|
||||
import kotlinx.coroutines.*
|
||||
import kotlinx.coroutines.channels.Channel
|
||||
import kotlinx.io.core.*
|
||||
import kotlinx.serialization.json.Json
|
||||
import java.util.logging.Logger
|
||||
import kotlin.Result
|
||||
import kotlin.coroutines.*
|
||||
|
||||
private const val API_TOKEN_FIELD = "api_key"
|
||||
@ -48,6 +46,9 @@ data class SauceNaoAPI(
|
||||
private val timeManager = TimeManager(scope)
|
||||
private val quotaManager = RequestQuotaManager(scope)
|
||||
|
||||
val limitsState: LimitsState
|
||||
get() = quotaManager.limitsState
|
||||
|
||||
private val requestsJob = scope.launch {
|
||||
for ((callback, requestBuilder) in requestsChannel) {
|
||||
quotaManager.getQuota()
|
||||
|
@ -0,0 +1,11 @@
|
||||
package com.github.insanusmokrassar.SauceNaoAPI.models
|
||||
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@Serializable
|
||||
data class LimitsState(
|
||||
val maxShortQuota: Int,
|
||||
val maxLongQuota: Int,
|
||||
val knownShortQuota: Int,
|
||||
val knownLongQuota: Int
|
||||
)
|
@ -5,6 +5,7 @@ import com.github.insanusmokrassar.SauceNaoAPI.additional.SHORT_TIME_RECALCULATI
|
||||
import com.github.insanusmokrassar.SauceNaoAPI.exceptions.TooManyRequestsException
|
||||
import com.github.insanusmokrassar.SauceNaoAPI.exceptions.TooManyRequestsLongException
|
||||
import com.github.insanusmokrassar.SauceNaoAPI.models.Header
|
||||
import com.github.insanusmokrassar.SauceNaoAPI.models.LimitsState
|
||||
import com.soywiz.klock.DateTime
|
||||
import kotlinx.coroutines.*
|
||||
import kotlinx.coroutines.channels.Channel
|
||||
@ -13,14 +14,22 @@ import kotlin.coroutines.suspendCoroutine
|
||||
import kotlin.math.max
|
||||
import kotlin.math.min
|
||||
|
||||
class RequestQuotaManager (
|
||||
private val scope: CoroutineScope
|
||||
internal class RequestQuotaManager (
|
||||
scope: CoroutineScope
|
||||
) : Closeable {
|
||||
private var longQuota = 1
|
||||
private var shortQuota = 1
|
||||
private var longMaxQuota = 1
|
||||
private var shortMaxQuota = 1
|
||||
|
||||
val limitsState: LimitsState
|
||||
get() = LimitsState(
|
||||
shortMaxQuota,
|
||||
longMaxQuota,
|
||||
shortQuota,
|
||||
longQuota
|
||||
)
|
||||
|
||||
private val quotaActions = Channel<suspend () -> Unit>(Channel.UNLIMITED)
|
||||
|
||||
private val quotaJob = scope.launch {
|
||||
|
@ -68,7 +68,7 @@ private data class TimeManagerMostOldestInShortGetter(
|
||||
}
|
||||
}
|
||||
|
||||
class TimeManager(
|
||||
internal class TimeManager(
|
||||
scope: CoroutineScope
|
||||
) : Closeable {
|
||||
private val actionsChannel = Channel<TimeManagerAction>(Channel.UNLIMITED)
|
||||
|
Loading…
Reference in New Issue
Block a user