now scope in SauceNaoAPI can be passed via constructor

This commit is contained in:
InsanusMokrassar 2019-10-11 00:10:19 +06:00
parent e61e094495
commit ab67180d19
2 changed files with 11 additions and 16 deletions

View File

@ -3,20 +3,14 @@ package com.github.insanusmokrassar.SauceNaoAPI
import kotlinx.coroutines.*
fun main(vararg args: String) {
val key = args.first()
val api = SauceNaoAPI(key)
val launch = GlobalScope.launch {
api.use {
it.request(
args[1]
).also {
println(it)
}
}
}
val (key, requestUrl) = args
runBlocking {
launch.join()
val api = SauceNaoAPI(key, scope = GlobalScope)
api.use {
println(
it.request(requestUrl)
)
}
}
}

View File

@ -32,7 +32,8 @@ data class SauceNaoAPI(
private val apiToken: String,
private val outputType: OutputType = JsonOutputType,
private val client: HttpClient = HttpClient(OkHttp),
private val searchUrl: String = SEARCH_URL
private val searchUrl: String = SEARCH_URL,
private val scope: CoroutineScope = CoroutineScope(Dispatchers.Default)
) : Closeable {
private val logger = Logger.getLogger("SauceNaoAPI")
@ -40,7 +41,7 @@ data class SauceNaoAPI(
private val requestsSendTimes = mutableListOf<DateTime>()
init {
CoroutineScope(Dispatchers.Default).launch {
scope.launch {
for ((callback, requestBuilder) in requestsChannel) {
try {
val answer = makeRequest(requestBuilder)
@ -191,7 +192,7 @@ data class SauceNaoAPI(
override fun close() {
requestsChannel.close()
requestsSendTimes.clear()
client.close()
requestsSendTimes.clear()
}
}