mirror of
https://github.com/InsanusMokrassar/SauceNaoAPI.git
synced 2024-12-23 05:07:12 +00:00
now scope in SauceNaoAPI can be passed via constructor
This commit is contained in:
parent
e61e094495
commit
ab67180d19
@ -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)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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()
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user