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.*
|
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)
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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()
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user