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.* import kotlinx.coroutines.*
fun main(vararg args: String) { fun main(vararg args: String) {
val key = args.first() val (key, requestUrl) = args
val api = SauceNaoAPI(key)
val launch = GlobalScope.launch {
api.use {
it.request(
args[1]
).also {
println(it)
}
}
}
runBlocking { 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 apiToken: String,
private val outputType: OutputType = JsonOutputType, private val outputType: OutputType = JsonOutputType,
private val client: HttpClient = HttpClient(OkHttp), 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 { ) : Closeable {
private val logger = Logger.getLogger("SauceNaoAPI") private val logger = Logger.getLogger("SauceNaoAPI")
@ -40,7 +41,7 @@ data class SauceNaoAPI(
private val requestsSendTimes = mutableListOf<DateTime>() private val requestsSendTimes = mutableListOf<DateTime>()
init { init {
CoroutineScope(Dispatchers.Default).launch { scope.launch {
for ((callback, requestBuilder) in requestsChannel) { for ((callback, requestBuilder) in requestsChannel) {
try { try {
val answer = makeRequest(requestBuilder) val answer = makeRequest(requestBuilder)
@ -191,7 +192,7 @@ data class SauceNaoAPI(
override fun close() { override fun close() {
requestsChannel.close() requestsChannel.close()
requestsSendTimes.clear()
client.close() client.close()
requestsSendTimes.clear()
} }
} }