diff --git a/build-plugin/common-dependencies/src/org/jetbrains/kotlinx/jupyter/common/httpUtil.kt b/build-plugin/common-dependencies/src/org/jetbrains/kotlinx/jupyter/common/httpUtil.kt index 5606ae1e7..36bc8c415 100644 --- a/build-plugin/common-dependencies/src/org/jetbrains/kotlinx/jupyter/common/httpUtil.kt +++ b/build-plugin/common-dependencies/src/org/jetbrains/kotlinx/jupyter/common/httpUtil.kt @@ -22,18 +22,18 @@ class ResponseWrapper( val url: String, ) : Response by response -fun createHttpClient(): HttpHandler { +private fun createHttpClient(): HttpHandler { return ClientFilters.FollowRedirects().then(ApacheClient()) } -fun httpRequest(request: Request): ResponseWrapper { +fun httpRequest(request: Request, mapper: (ResponseWrapper) -> R): R { val client = createHttpClient() val response = client(request) - return ResponseWrapper(response, request.uri.toString()) + return ResponseWrapper(response, request.uri.toString()).use(mapper) } -fun getHttp(url: String) = httpRequest(Request(Method.GET, url)) +fun getHttp(url: String, mapper: (ResponseWrapper) -> R): R = httpRequest(Request(Method.GET, url), mapper) fun Request.withBasicAuth(username: String, password: String): Request { val b64 = Base64.getEncoder().encode("$username:$password".toByteArray()).toString(Charsets.UTF_8) diff --git a/build-plugin/common-dependencies/src/org/jetbrains/kotlinx/jupyter/common/libraries.kt b/build-plugin/common-dependencies/src/org/jetbrains/kotlinx/jupyter/common/libraries.kt index 0a016b8ac..1508cbef0 100644 --- a/build-plugin/common-dependencies/src/org/jetbrains/kotlinx/jupyter/common/libraries.kt +++ b/build-plugin/common-dependencies/src/org/jetbrains/kotlinx/jupyter/common/libraries.kt @@ -63,7 +63,7 @@ class LibraryDescriptorsManager private constructor( url += "&since=$sinceTimestamp" } logger.info("Checking for new commits to library descriptors at $url") - val arr = getHttp(url).jsonArrayOrNull + val arr = getHttp(url) { it.jsonArrayOrNull } if (arr == null) { logger.error("Request for the latest commit in libraries failed") return@catchAll null @@ -91,8 +91,7 @@ class LibraryDescriptorsManager private constructor( } fun checkRefExistence(ref: String): Boolean { - val response = getHttp("$apiPrefix/contents/$remotePath?ref=$ref") - return response.status.successful + return getHttp("$apiPrefix/contents/$remotePath?ref=$ref") { it.status.successful } } fun checkIfRefUpToDate(remoteRef: String?): Boolean { @@ -110,7 +109,7 @@ class LibraryDescriptorsManager private constructor( val url = "$apiPrefix/contents/$remotePath?ref=$ref" logger.info("Requesting library descriptors at $url") - val response = getHttp(url).jsonArray + val response = getHttp(url) { it.jsonArray } for (item in response) { item as JsonObject @@ -120,9 +119,8 @@ class LibraryDescriptorsManager private constructor( if (!fileName.endsWith(".$DESCRIPTOR_EXTENSION")) continue val downloadUrl = item["download_url"]!!.jsonPrimitive.content - val descriptorResponse = getHttp(downloadUrl) - val descriptorText = descriptorResponse.text + val descriptorText = getHttp(downloadUrl) { it.text } val file = localLibrariesDir.resolve(fileName) file.writeText(descriptorText) } @@ -144,10 +142,9 @@ class LibraryDescriptorsManager private constructor( } private fun downloadSingleFile(contentsApiUrl: String): String { - val response = getHttp(contentsApiUrl).jsonObject + val response = getHttp(contentsApiUrl) { it.jsonObject } val downloadUrl = response["download_url"]!!.jsonPrimitive.content - val res = getHttp(downloadUrl) - return res.text + return getHttp(downloadUrl) { it.text } } private fun saveLocalRef(ref: String) { diff --git a/build-plugin/src/build/KernelVersionUpdateTasksConfigurator.kt b/build-plugin/src/build/KernelVersionUpdateTasksConfigurator.kt index 2d204e79d..79a483946 100644 --- a/build-plugin/src/build/KernelVersionUpdateTasksConfigurator.kt +++ b/build-plugin/src/build/KernelVersionUpdateTasksConfigurator.kt @@ -21,11 +21,10 @@ class KernelVersionUpdateTasksConfigurator( val teamcityUrl = teamcityProject.url val locator = "buildType:(id:${teamcityProject.projectId}),status:SUCCESS,branch:default:any,count:1" - val response = httpRequest( + val builds = httpRequest( Request(Method.GET, "$teamcityUrl/$TEAMCITY_REQUEST_ENDPOINT/?locator=$locator") .header("accept", "application/json") - ) - val builds = response.jsonObject["build"] as JsonArray + ) { it.jsonObject["build"] as JsonArray } val lastBuild = builds[0] as JsonObject val lastBuildNumber = (lastBuild["number"] as JsonPrimitive).content println("Last Kotlin dev version: $lastBuildNumber") diff --git a/build-plugin/src/build/LibraryUpdateTasksConfigurator.kt b/build-plugin/src/build/LibraryUpdateTasksConfigurator.kt index 1fa777b6e..dc2073569 100644 --- a/build-plugin/src/build/LibraryUpdateTasksConfigurator.kt +++ b/build-plugin/src/build/LibraryUpdateTasksConfigurator.kt @@ -14,7 +14,6 @@ import org.gradle.process.ExecSpec import org.gradle.tooling.BuildException import org.http4k.core.Method import org.http4k.core.Request -import org.http4k.core.Response import org.jetbrains.kotlinx.jupyter.common.ResponseWrapper import org.jetbrains.kotlinx.jupyter.common.httpRequest import org.jetbrains.kotlinx.jupyter.common.jsonObject @@ -99,25 +98,21 @@ class LibraryUpdateTasksConfigurator( val user = settings.prGithubUser val password = settings.prGithubToken val repoUserAndName = settings.librariesRepoUserAndName - fun githubRequest( + fun githubRequest( method: Method, request: String, json: JsonElement, - onFailure: (Response) -> Unit, - ): ResponseWrapper { - val response = httpRequest( - Request(method, "https://api.github.com/$request") - .withJson(json) - .withBasicAuth(user, password) - ) - println(response.text) - if (!response.status.successful) { - onFailure(response) - } - return response + mapper: (ResponseWrapper) -> R + ): R = httpRequest( + Request(method, "https://api.github.com/$request") + .withJson(json) + .withBasicAuth(user, password) + ) { + println(it.text) + mapper(it) } - val prResponse = githubRequest( + val prNumber = githubRequest( Method.POST, "repos/$repoUserAndName/pulls", Json.encodeToJsonElement( NewPrData( @@ -126,18 +121,21 @@ class LibraryUpdateTasksConfigurator( base = "master" ) ) - ) { response -> - throw BuildException("Creating PR failed with code ${response.status.code}", null) + ) { + if (!it.status.successful) { + throw BuildException("Creating PR failed with code ${it.status.code}", null) + } + (it.jsonObject["number"] as JsonPrimitive).int } - - val prNumber = (prResponse.jsonObject["number"] as JsonPrimitive).int githubRequest( Method.POST, "repos/$repoUserAndName/issues/$prNumber/labels", Json.encodeToJsonElement( SetLabelsData(listOf("no-changelog", "library-descriptors")) ) - ) { response -> - throw BuildException("Cannot setup labels for created PR: ${response.text}", null) + ) { + if (!it.status.successful) { + throw BuildException("Cannot setup labels for created PR: ${it.text}", null) + } } } } diff --git a/jupyter-lib/lib-ext/src/main/kotlin/org/jetbrains/kotlinx/jupyter/ext/Image.kt b/jupyter-lib/lib-ext/src/main/kotlin/org/jetbrains/kotlinx/jupyter/ext/Image.kt index 56da5cd89..91f387361 100644 --- a/jupyter-lib/lib-ext/src/main/kotlin/org/jetbrains/kotlinx/jupyter/ext/Image.kt +++ b/jupyter-lib/lib-ext/src/main/kotlin/org/jetbrains/kotlinx/jupyter/ext/Image.kt @@ -86,7 +86,7 @@ class Image(private val attributes: List) : Renderable { val client = ApacheClient() val request = Request(Method.GET, url) val response = client(request) - return response.body.payload.array() + return response.use { it.body.payload.array() } } fun loadData(file: File): ByteArray { diff --git a/src/main/kotlin/org/jetbrains/kotlinx/jupyter/libraries/CssLibraryResourcesProcessor.kt b/src/main/kotlin/org/jetbrains/kotlinx/jupyter/libraries/CssLibraryResourcesProcessor.kt index 4eae7346f..e239ced94 100644 --- a/src/main/kotlin/org/jetbrains/kotlinx/jupyter/libraries/CssLibraryResourcesProcessor.kt +++ b/src/main/kotlin/org/jetbrains/kotlinx/jupyter/libraries/CssLibraryResourcesProcessor.kt @@ -25,7 +25,7 @@ class CssLibraryResourcesProcessor : LibraryResourcesProcessor { ResourcePathType.URL -> """ """.trimIndent() - ResourcePathType.URL_EMBEDDED -> wrapInTag(getHttp(path).text) + ResourcePathType.URL_EMBEDDED -> getHttp(path) { wrapInTag(it.text) } ResourcePathType.LOCAL_PATH -> wrapInTag(File(path).readText()) ResourcePathType.CLASSPATH_PATH -> wrapInTag(classLoader.getResource(path)?.readText().orEmpty()) } diff --git a/src/main/kotlin/org/jetbrains/kotlinx/jupyter/libraries/JsLibraryResourcesProcessor.kt b/src/main/kotlin/org/jetbrains/kotlinx/jupyter/libraries/JsLibraryResourcesProcessor.kt index 6fc971d71..7e8b4ae4b 100644 --- a/src/main/kotlin/org/jetbrains/kotlinx/jupyter/libraries/JsLibraryResourcesProcessor.kt +++ b/src/main/kotlin/org/jetbrains/kotlinx/jupyter/libraries/JsLibraryResourcesProcessor.kt @@ -27,7 +27,7 @@ class JsLibraryResourcesProcessor : LibraryResourcesProcessor { URLScriptModifierFunctionGenerator(path) } ResourcePathType.URL_EMBEDDED -> { - val scriptText = getHttp(path).text + val scriptText = getHttp(path) { it.text } CodeScriptModifierFunctionGenerator(scriptText) } ResourcePathType.LOCAL_PATH -> { diff --git a/src/main/kotlin/org/jetbrains/kotlinx/jupyter/libraries/StandardResolutionInfoProvider.kt b/src/main/kotlin/org/jetbrains/kotlinx/jupyter/libraries/StandardResolutionInfoProvider.kt index c085c0dca..bbf011ffb 100644 --- a/src/main/kotlin/org/jetbrains/kotlinx/jupyter/libraries/StandardResolutionInfoProvider.kt +++ b/src/main/kotlin/org/jetbrains/kotlinx/jupyter/libraries/StandardResolutionInfoProvider.kt @@ -25,7 +25,6 @@ class StandardResolutionInfoProvider(override var fallback: LibraryResolutionInf } private fun tryGetAsURL(url: String): LibraryResolutionInfo? { - val response = getHttp(url) - return if (response.status.successful) AbstractLibraryResolutionInfo.ByURL(URL(url)) else null + return getHttp(url) { if (it.status.successful) AbstractLibraryResolutionInfo.ByURL(URL(url)) else null } } } diff --git a/src/main/kotlin/org/jetbrains/kotlinx/jupyter/libraries/libraryResolvers.kt b/src/main/kotlin/org/jetbrains/kotlinx/jupyter/libraries/libraryResolvers.kt index e548924ee..6bf4f6fb1 100644 --- a/src/main/kotlin/org/jetbrains/kotlinx/jupyter/libraries/libraryResolvers.kt +++ b/src/main/kotlin/org/jetbrains/kotlinx/jupyter/libraries/libraryResolvers.kt @@ -90,8 +90,7 @@ object FallbackLibraryResolver : ChainedLibraryResolver() { file.readText() }, resolver { - val response = getHttp(url.toString()) - response.text + getHttp(url.toString()) { it.text } }, resolver { "{}" }, resolver { null }