Skip to content

Commit 371ac23

Browse files
committed
Properly close the ktor-client after use
1 parent a35c728 commit 371ac23

File tree

1 file changed

+44
-43
lines changed

1 file changed

+44
-43
lines changed

src/jsMain/kotlin/net/kautler/github/action/setup_wsl/Distribution.kt

Lines changed: 44 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020-2024 Björn Kautler
2+
* Copyright 2020-2025 Björn Kautler
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -27,6 +27,7 @@ import io.ktor.client.request.forms.submitForm
2727
import io.ktor.client.statement.bodyAsText
2828
import io.ktor.http.HttpStatusCode.Companion.OK
2929
import io.ktor.http.parameters
30+
import io.ktor.utils.io.core.use
3031
import js.objects.recordOf
3132
import kotlinx.coroutines.CoroutineStart.LAZY
3233
import kotlinx.coroutines.DelicateCoroutinesApi
@@ -62,58 +63,58 @@ sealed class Distribution(
6263
return@async _downloadUrl
6364
}
6465

65-
val client = HttpClient(Js) {
66-
install(UserAgent) {
67-
agent = "Setup WSL GitHub Action"
68-
}
69-
}
70-
7166
val parameters = parameters {
7267
append("type", "ProductId")
7368
append("url", productId!!)
7469
}
7570

76-
return@async retry(5) {
77-
val response = client.submitForm(
78-
url = "https://store.rg-adguard.net/api/GetFiles",
79-
formParameters = parameters
80-
)
71+
return@async HttpClient(Js) {
72+
install(UserAgent) {
73+
agent = "Setup WSL GitHub Action"
74+
}
75+
}.use { client ->
76+
retry(5) {
77+
val response = client.submitForm(
78+
url = "https://store.rg-adguard.net/api/GetFiles",
79+
formParameters = parameters
80+
)
8181

82-
if (response.status != OK) {
83-
if (isDebug()) {
84-
val echoResponse = client.submitForm(
85-
url = "https://echo.free.beeceptor.com/api/GetFiles",
86-
formParameters = parameters
87-
)
88-
if (echoResponse.status == OK) {
89-
debug("Request:\n${echoResponse.bodyAsText()}")
90-
} else {
91-
debug("Could not get echo response (statusCode: ${echoResponse.status.value} / statusMessage: ${echoResponse.status.description})")
82+
if (response.status != OK) {
83+
if (isDebug()) {
84+
val echoResponse = client.submitForm(
85+
url = "https://echo.free.beeceptor.com/api/GetFiles",
86+
formParameters = parameters
87+
)
88+
if (echoResponse.status == OK) {
89+
debug("Request:\n${echoResponse.bodyAsText()}")
90+
} else {
91+
debug("Could not get echo response (statusCode: ${echoResponse.status.value} / statusMessage: ${echoResponse.status.description})")
92+
}
93+
94+
val responseMessage = JSON.stringify(
95+
recordOf(
96+
"httpVersion" to "${response.version}",
97+
"headers" to "${response.headers}",
98+
"method" to "${response.call.request.method}",
99+
"url" to "${response.call.request.url}",
100+
"statusCode" to response.status.value,
101+
"statusMessage" to response.status.description,
102+
"body" to response.bodyAsText()
103+
),
104+
space = 2
105+
)
106+
debug("Response:\n$responseMessage")
92107
}
93-
94-
val responseMessage = JSON.stringify(
95-
recordOf(
96-
"httpVersion" to "${response.version}",
97-
"headers" to "${response.headers}",
98-
"method" to "${response.call.request.method}",
99-
"url" to "${response.call.request.url}",
100-
"statusCode" to response.status.value,
101-
"statusMessage" to response.status.description,
102-
"body" to response.bodyAsText()
103-
),
104-
space = 2
105-
)
106-
debug("Response:\n$responseMessage")
108+
error("Could not determine download URL (statusCode: ${response.status.value} / statusMessage: ${response.status.description})")
107109
}
108-
error("Could not determine download URL (statusCode: ${response.status.value} / statusMessage: ${response.status.description})")
109-
}
110110

111-
val body = response.bodyAsText()
112-
val downloadLinkAnchorMatch =
113-
"""<a [^>]*href="(?<url>[^"]+)"[^>]*>[^<]*\.appx(?:bundle)?</a>""".toRegex().find(body)
114-
?: error("Could not determine download URL from:\n$body")
111+
val body = response.bodyAsText()
112+
val downloadLinkAnchorMatch =
113+
"""<a [^>]*href="(?<url>[^"]+)"[^>]*>[^<]*\.appx(?:bundle)?</a>""".toRegex().find(body)
114+
?: error("Could not determine download URL from:\n$body")
115115

116-
return@retry URL(downloadLinkAnchorMatch.groups[1]!!.value)
116+
return@retry URL(downloadLinkAnchorMatch.groups[1]!!.value)
117+
}
117118
}
118119
}
119120

0 commit comments

Comments
 (0)