large upgrade

This commit is contained in:
InsanusMokrassar 2020-08-29 19:09:54 +06:00
parent 09b4b45b75
commit 270be95784
29 changed files with 123 additions and 102 deletions

View File

@ -2,6 +2,8 @@
### 0.6.0
**MAIN PACKAGE WAS CHANGED: `com.github.insanusmokrassar` -> `com.insanusmokrassar`**
* All known fields were added to `ResultData`
* Versions updates:
* `Kotlin`: `1.3.72` -> `1.4.0`
@ -10,7 +12,6 @@
* `Klock`: `1.11.14` -> `1.12.0`
* `Ktor`: `1.3.2` -> `1.4.0`
## 0.5.0
* Versions updates

View File

@ -14,11 +14,11 @@ buildscript {
plugins {
id "org.jetbrains.kotlin.multiplatform" version "$kotlin_version"
id "org.jetbrains.kotlin.plugin.serialization" version "$kotlin_version"
}
apply plugin: 'kotlinx-serialization'
project.version = "0.6.0"
project.group = "com.github.insanusmokrassar"
project.group = "com.insanusmokrassar"
apply from: "publish.gradle"

View File

@ -1,23 +0,0 @@
package com.github.insanusmokrassar.SauceNaoAPI.additional
import com.github.insanusmokrassar.SauceNaoAPI.additional.header.ResultMetaInfo
import com.github.insanusmokrassar.SauceNaoAPI.additional.header.adapted
import com.github.insanusmokrassar.SauceNaoAPI.additional.results.AdaptedResult
import com.github.insanusmokrassar.SauceNaoAPI.additional.results.adapted
import com.github.insanusmokrassar.SauceNaoAPI.models.SauceNaoAnswer
val SauceNaoAnswer.adapted: AdaptedAnswer
get() = header.adapted.let { resultMetainfo ->
val adaptedResults = results.map {
it.adapted(resultMetainfo)
}
AdaptedAnswer(
resultMetainfo,
adaptedResults
)
}
data class AdaptedAnswer(
val resultMetaInfo: ResultMetaInfo,
val results: List<AdaptedResult>
)

View File

@ -1,9 +0,0 @@
package com.github.insanusmokrassar.SauceNaoAPI.additional.results
import com.github.insanusmokrassar.SauceNaoAPI.additional.header.IndexInfo
data class ResultHeader(
val similarity: Float,
val thumbnail: String,
val index: IndexInfo
)

View File

@ -1,4 +1,4 @@
package com.github.insanusmokrassar.SauceNaoAPI
package com.insanusmokrassar.SauceNaoAPI
sealed class OutputType {
abstract val typeCode: Int

View File

@ -1,24 +1,23 @@
package com.github.insanusmokrassar.SauceNaoAPI
package com.insanusmokrassar.SauceNaoAPI
import com.github.insanusmokrassar.SauceNaoAPI.exceptions.TooManyRequestsException
import com.github.insanusmokrassar.SauceNaoAPI.exceptions.sauceNaoAPIException
import com.github.insanusmokrassar.SauceNaoAPI.models.*
import com.github.insanusmokrassar.SauceNaoAPI.utils.*
import com.insanusmokrassar.SauceNaoAPI.exceptions.TooManyRequestsException
import com.insanusmokrassar.SauceNaoAPI.exceptions.sauceNaoAPIException
import com.insanusmokrassar.SauceNaoAPI.models.*
import com.insanusmokrassar.SauceNaoAPI.utils.*
import io.ktor.client.HttpClient
import io.ktor.client.features.ClientRequestException
import io.ktor.client.request.*
import io.ktor.client.request.forms.*
import io.ktor.client.request.forms.MultiPartFormDataContent
import io.ktor.client.request.forms.formData
import io.ktor.client.statement.HttpResponse
import io.ktor.client.statement.readText
import io.ktor.http.*
import io.ktor.utils.io.core.Closeable
import io.ktor.utils.io.core.Input
import kotlinx.coroutines.*
import kotlinx.coroutines.channels.Channel
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.nonstrict
import kotlin.Result
import kotlin.coroutines.*
import kotlin.coroutines.Continuation
import kotlin.coroutines.suspendCoroutine
private const val API_TOKEN_FIELD = "api_key"
private const val OUTPUT_TYPE_FIELD = "output_type"
@ -47,7 +46,7 @@ data class SauceNaoAPI(
private val searchUrl: String = SEARCH_URL,
private val scope: CoroutineScope = CoroutineScope(Dispatchers.Default),
private val parser: Json = defaultSauceNaoParser
) : Closeable {
) : SauceCloseable {
private val requestsChannel = Channel<Pair<Continuation<SauceNaoAnswer>, HttpRequestBuilder>>(Channel.UNLIMITED)
private val timeManager = TimeManager(scope)
private val quotaManager = RequestQuotaManager(scope)

View File

@ -1,4 +1,4 @@
package com.github.insanusmokrassar.SauceNaoAPI
package com.insanusmokrassar.SauceNaoAPI
import io.ktor.http.ContentType
import io.ktor.utils.io.core.Input

View File

@ -0,0 +1,23 @@
package com.insanusmokrassar.SauceNaoAPI.additional
import com.insanusmokrassar.SauceNaoAPI.additional.header.ResultMetaInfo
import com.insanusmokrassar.SauceNaoAPI.additional.header.adapted
import com.insanusmokrassar.SauceNaoAPI.additional.results.AdaptedResult
import com.insanusmokrassar.SauceNaoAPI.additional.results.adapted
import com.insanusmokrassar.SauceNaoAPI.models.SauceNaoAnswer
val SauceNaoAnswer.adapted: AdaptedAnswer
get() = header.adapted.let { resultMetainfo ->
val adaptedResults = results.map {
it.adapted(resultMetainfo)
}
AdaptedAnswer(
resultMetainfo,
adaptedResults
)
}
data class AdaptedAnswer(
val resultMetaInfo: ResultMetaInfo,
val results: List<AdaptedResult>
)

View File

@ -1,4 +1,4 @@
package com.github.insanusmokrassar.SauceNaoAPI.additional
package com.insanusmokrassar.SauceNaoAPI.additional
import com.soywiz.klock.TimeSpan

View File

@ -1,7 +1,7 @@
package com.github.insanusmokrassar.SauceNaoAPI.additional.header
package com.insanusmokrassar.SauceNaoAPI.additional.header
import com.github.insanusmokrassar.SauceNaoAPI.additional.*
import com.github.insanusmokrassar.SauceNaoAPI.models.Header
import com.insanusmokrassar.SauceNaoAPI.additional.*
import com.insanusmokrassar.SauceNaoAPI.models.Header
val Header.shortLimitStatus: LimitStatus
get() = LimitStatus(

View File

@ -1,6 +1,6 @@
package com.github.insanusmokrassar.SauceNaoAPI.additional.header
package com.insanusmokrassar.SauceNaoAPI.additional.header
import com.github.insanusmokrassar.SauceNaoAPI.models.Header
import com.insanusmokrassar.SauceNaoAPI.models.Header
data class IndexInfo(
val id: Int,

View File

@ -1,6 +1,6 @@
package com.github.insanusmokrassar.SauceNaoAPI.additional.header
package com.insanusmokrassar.SauceNaoAPI.additional.header
import com.github.insanusmokrassar.SauceNaoAPI.models.Header
import com.insanusmokrassar.SauceNaoAPI.models.Header
val Header.queryPreview
get() = QueryResultPreview(

View File

@ -1,6 +1,6 @@
package com.github.insanusmokrassar.SauceNaoAPI.additional.header
package com.insanusmokrassar.SauceNaoAPI.additional.header
import com.github.insanusmokrassar.SauceNaoAPI.models.Header
import com.insanusmokrassar.SauceNaoAPI.models.Header
data class ResultMetaInfo(
val accountInfo: AccountInfo = AccountInfo(),

View File

@ -1,9 +1,9 @@
package com.github.insanusmokrassar.SauceNaoAPI.additional.results
package com.insanusmokrassar.SauceNaoAPI.additional.results
import com.github.insanusmokrassar.SauceNaoAPI.additional.header.IndexInfo
import com.github.insanusmokrassar.SauceNaoAPI.additional.header.ResultMetaInfo
import com.github.insanusmokrassar.SauceNaoAPI.models.Result
import com.github.insanusmokrassar.SauceNaoAPI.models.ResultData
import com.insanusmokrassar.SauceNaoAPI.additional.header.IndexInfo
import com.insanusmokrassar.SauceNaoAPI.additional.header.ResultMetaInfo
import com.insanusmokrassar.SauceNaoAPI.models.Result
import com.insanusmokrassar.SauceNaoAPI.models.ResultData
fun Result.adapted(
resultMetaInfo: ResultMetaInfo

View File

@ -0,0 +1,9 @@
package com.insanusmokrassar.SauceNaoAPI.additional.results
import com.insanusmokrassar.SauceNaoAPI.additional.header.IndexInfo
data class ResultHeader(
val similarity: Float,
val thumbnail: String,
val index: IndexInfo
)

View File

@ -1,7 +1,7 @@
package com.github.insanusmokrassar.SauceNaoAPI.exceptions
package com.insanusmokrassar.SauceNaoAPI.exceptions
import com.github.insanusmokrassar.SauceNaoAPI.additional.LONG_TIME_RECALCULATING_MILLIS
import com.github.insanusmokrassar.SauceNaoAPI.additional.SHORT_TIME_RECALCULATING_MILLIS
import com.insanusmokrassar.SauceNaoAPI.additional.LONG_TIME_RECALCULATING_MILLIS
import com.insanusmokrassar.SauceNaoAPI.additional.SHORT_TIME_RECALCULATING_MILLIS
import com.soywiz.klock.TimeSpan
import io.ktor.client.features.ClientRequestException
import io.ktor.client.statement.readText

View File

@ -1,12 +1,13 @@
package com.github.insanusmokrassar.SauceNaoAPI.models
package com.insanusmokrassar.SauceNaoAPI.models
import com.github.insanusmokrassar.SauceNaoAPI.defaultSauceNaoParser
import com.insanusmokrassar.SauceNaoAPI.defaultSauceNaoParser
import kotlinx.serialization.*
import kotlinx.serialization.builtins.serializer
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
import kotlinx.serialization.json.*
import kotlinx.serialization.json.JsonObject
import kotlinx.serialization.json.jsonObject
@Serializable
data class Header(

View File

@ -1,4 +1,4 @@
package com.github.insanusmokrassar.SauceNaoAPI.models
package com.insanusmokrassar.SauceNaoAPI.models
import kotlinx.serialization.Serializable

View File

@ -1,4 +1,4 @@
package com.github.insanusmokrassar.SauceNaoAPI.models
package com.insanusmokrassar.SauceNaoAPI.models
import kotlinx.serialization.Serializable

View File

@ -1,4 +1,4 @@
package com.github.insanusmokrassar.SauceNaoAPI.models
package com.insanusmokrassar.SauceNaoAPI.models
import kotlinx.serialization.Serializable

View File

@ -1,6 +1,6 @@
package com.github.insanusmokrassar.SauceNaoAPI.models
package com.insanusmokrassar.SauceNaoAPI.models
import com.github.insanusmokrassar.SauceNaoAPI.utils.CommonMultivariantStringSerializer
import com.insanusmokrassar.SauceNaoAPI.utils.CommonMultivariantStringSerializer
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

View File

@ -1,4 +1,4 @@
package com.github.insanusmokrassar.SauceNaoAPI.models
package com.insanusmokrassar.SauceNaoAPI.models
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

View File

@ -1,11 +1,12 @@
package com.github.insanusmokrassar.SauceNaoAPI.models
package com.insanusmokrassar.SauceNaoAPI.models
import com.github.insanusmokrassar.SauceNaoAPI.defaultSauceNaoParser
import com.insanusmokrassar.SauceNaoAPI.defaultSauceNaoParser
import kotlinx.serialization.*
import kotlinx.serialization.builtins.ListSerializer
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
import kotlinx.serialization.json.*
import kotlinx.serialization.json.JsonObject
import kotlinx.serialization.json.buildJsonObject
@Serializable
private data class TemporalSauceNaoAnswerRepresentation(

View File

@ -1,6 +1,7 @@
package com.github.insanusmokrassar.SauceNaoAPI.utils
package com.insanusmokrassar.SauceNaoAPI.utils
import kotlinx.serialization.*
import kotlinx.serialization.KSerializer
import kotlinx.serialization.Serializer
import kotlinx.serialization.builtins.ListSerializer
import kotlinx.serialization.builtins.serializer
import kotlinx.serialization.encoding.Decoder

View File

@ -1,13 +1,12 @@
package com.github.insanusmokrassar.SauceNaoAPI.utils
package com.insanusmokrassar.SauceNaoAPI.utils
import com.github.insanusmokrassar.SauceNaoAPI.additional.LONG_TIME_RECALCULATING_MILLIS
import com.github.insanusmokrassar.SauceNaoAPI.additional.SHORT_TIME_RECALCULATING_MILLIS
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.insanusmokrassar.SauceNaoAPI.additional.LONG_TIME_RECALCULATING_MILLIS
import com.insanusmokrassar.SauceNaoAPI.additional.SHORT_TIME_RECALCULATING_MILLIS
import com.insanusmokrassar.SauceNaoAPI.exceptions.TooManyRequestsException
import com.insanusmokrassar.SauceNaoAPI.exceptions.TooManyRequestsLongException
import com.insanusmokrassar.SauceNaoAPI.models.Header
import com.insanusmokrassar.SauceNaoAPI.models.LimitsState
import com.soywiz.klock.DateTime
import io.ktor.utils.io.core.Closeable
import kotlinx.coroutines.*
import kotlinx.coroutines.channels.Channel
import kotlin.coroutines.suspendCoroutine
@ -16,7 +15,7 @@ import kotlin.math.min
internal class RequestQuotaManager (
scope: CoroutineScope
) : Closeable {
) : SauceCloseable {
private var longQuota = 1
private var shortQuota = 1
private var longMaxQuota = 1

View File

@ -0,0 +1,21 @@
package com.insanusmokrassar.SauceNaoAPI.utils
import kotlinx.coroutines.supervisorScope
interface SauceCloseable {
fun close()
}
fun <T> SauceCloseable.use(block: (SauceCloseable) -> T): T = try {
block(this)
} finally {
close()
}
suspend fun <T> SauceCloseable.useSafe(block: suspend (SauceCloseable) -> T): T = try {
supervisorScope {
block(this@useSafe)
}
} finally {
close()
}

View File

@ -1,10 +1,9 @@
package com.github.insanusmokrassar.SauceNaoAPI.utils
package com.insanusmokrassar.SauceNaoAPI.utils
import com.github.insanusmokrassar.SauceNaoAPI.additional.LONG_TIME_RECALCULATING_MILLIS
import com.github.insanusmokrassar.SauceNaoAPI.additional.SHORT_TIME_RECALCULATING_MILLIS
import com.github.insanusmokrassar.SauceNaoAPI.models.Header
import com.insanusmokrassar.SauceNaoAPI.additional.LONG_TIME_RECALCULATING_MILLIS
import com.insanusmokrassar.SauceNaoAPI.additional.SHORT_TIME_RECALCULATING_MILLIS
import com.insanusmokrassar.SauceNaoAPI.models.Header
import com.soywiz.klock.DateTime
import com.soywiz.klock.TimeSpan
internal suspend fun calculateSleepTime(
header: Header,

View File

@ -1,9 +1,8 @@
package com.github.insanusmokrassar.SauceNaoAPI.utils
package com.insanusmokrassar.SauceNaoAPI.utils
import com.github.insanusmokrassar.SauceNaoAPI.additional.LONG_TIME_RECALCULATING_MILLIS
import com.github.insanusmokrassar.SauceNaoAPI.additional.SHORT_TIME_RECALCULATING_MILLIS
import com.insanusmokrassar.SauceNaoAPI.additional.LONG_TIME_RECALCULATING_MILLIS
import com.insanusmokrassar.SauceNaoAPI.additional.SHORT_TIME_RECALCULATING_MILLIS
import com.soywiz.klock.DateTime
import io.ktor.utils.io.core.Closeable
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.launch
@ -69,7 +68,7 @@ private data class TimeManagerMostOldestInShortGetter(
internal class TimeManager(
scope: CoroutineScope
) : Closeable {
) : SauceCloseable {
private val actionsChannel = Channel<TimeManagerAction>(Channel.UNLIMITED)
private val timeUpdateJob = scope.launch {

View File

@ -1,6 +1,6 @@
import com.github.insanusmokrassar.SauceNaoAPI.SauceNaoAPI
import com.insanusmokrassar.SauceNaoAPI.SauceNaoAPI
import com.insanusmokrassar.SauceNaoAPI.utils.useSafe
import io.ktor.http.ContentType
import io.ktor.utils.io.streams.asInput
import kotlinx.coroutines.*
import java.io.File
import java.nio.file.Files
@ -11,7 +11,7 @@ suspend fun main(vararg args: String) {
val scope = CoroutineScope(Dispatchers.Default)
val api = SauceNaoAPI(key, scope = scope)
api.use { _ ->
api.useSafe { _ ->
println(
when {
requestSubject.startsWith("/") -> File(requestSubject).let {