From 464347df899dfe712a94876c5c4753a44383e6af Mon Sep 17 00:00:00 2001 From: ascheja Date: Wed, 13 Apr 2022 13:42:32 +0200 Subject: [PATCH] Upgrade to ktor 2.0.0 --- ktor-client-xmlrpc/build.gradle.kts | 2 +- ktor-client-xmlrpc/src/main/kotlin/Library.kt | 14 ++++++++------ ktor-server-xmlrpc/build.gradle.kts | 2 +- ktor-server-xmlrpc/src/main/kotlin/Library.kt | 19 ++++++++++--------- .../src/test/kotlin/LibraryTest.kt | 7 ++++--- 5 files changed, 24 insertions(+), 20 deletions(-) diff --git a/ktor-client-xmlrpc/build.gradle.kts b/ktor-client-xmlrpc/build.gradle.kts index 70f1fe3..84019a5 100644 --- a/ktor-client-xmlrpc/build.gradle.kts +++ b/ktor-client-xmlrpc/build.gradle.kts @@ -1,4 +1,4 @@ -val ktorVersion = "1.6.7" +val ktorVersion = "2.0.0" dependencies { api(project(":protocol")) diff --git a/ktor-client-xmlrpc/src/main/kotlin/Library.kt b/ktor-client-xmlrpc/src/main/kotlin/Library.kt index d9ecf3d..657d143 100644 --- a/ktor-client-xmlrpc/src/main/kotlin/Library.kt +++ b/ktor-client-xmlrpc/src/main/kotlin/Library.kt @@ -7,10 +7,11 @@ package org.ascheja.xmlrpc.ktor.client import io.ktor.client.HttpClient import io.ktor.client.request.header import io.ktor.client.request.post -import io.ktor.client.statement.HttpResponse +import io.ktor.client.request.setBody +import io.ktor.client.statement.bodyAsChannel import io.ktor.http.ContentType import io.ktor.http.HttpHeaders -import io.ktor.http.contentType +import io.ktor.http.content.ByteArrayContent import io.ktor.utils.io.jvm.javaio.toInputStream import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.withContext @@ -22,13 +23,14 @@ import org.ascheja.xmlrpc.protocol.writeToByteArray public class XmlRpcFault(public val methodResponse: MethodResponseFault) : RuntimeException() public suspend fun HttpClient.xmlRpc(urlString: String, methodCall: MethodCall, throwOnFault: Boolean = true): MethodResponse { - val response = post(urlString) { + val response = post(urlString) { header(HttpHeaders.Accept, ContentType.Application.Xml) - contentType(ContentType.Application.Xml) - body = methodCall.toDocument().writeToByteArray() + setBody(ByteArrayContent(methodCall.toDocument().writeToByteArray(), ContentType.Application.Xml)) } val methodResponse = withContext(Dispatchers.IO) { - MethodResponse.parse { it.parse(response.content.toInputStream()) } + response.bodyAsChannel().toInputStream().use { body -> + MethodResponse.parse { it.parse(body) } + } } if (methodResponse is MethodResponseFault && throwOnFault) throw XmlRpcFault(methodResponse) return methodResponse diff --git a/ktor-server-xmlrpc/build.gradle.kts b/ktor-server-xmlrpc/build.gradle.kts index ae307c3..ff8ce03 100644 --- a/ktor-server-xmlrpc/build.gradle.kts +++ b/ktor-server-xmlrpc/build.gradle.kts @@ -1,4 +1,4 @@ -val ktorVersion = "1.6.7" +val ktorVersion = "2.0.0" dependencies { api(project(":protocol")) diff --git a/ktor-server-xmlrpc/src/main/kotlin/Library.kt b/ktor-server-xmlrpc/src/main/kotlin/Library.kt index 6e7581a..3dd98b6 100644 --- a/ktor-server-xmlrpc/src/main/kotlin/Library.kt +++ b/ktor-server-xmlrpc/src/main/kotlin/Library.kt @@ -4,20 +4,21 @@ package org.ascheja.xmlrpc.ktor.server -import io.ktor.application.call import io.ktor.http.ContentType import io.ktor.http.HttpStatusCode -import io.ktor.request.contentType -import io.ktor.request.receiveStream -import io.ktor.response.respond -import io.ktor.response.respondBytes -import io.ktor.routing.Route -import io.ktor.routing.post +import io.ktor.server.application.ApplicationCall +import io.ktor.server.application.call +import io.ktor.server.request.contentType +import io.ktor.server.request.receiveStream +import io.ktor.server.response.respond +import io.ktor.server.response.respondBytes +import io.ktor.server.routing.Route +import io.ktor.server.routing.post import org.ascheja.xmlrpc.protocol.MethodCall import org.ascheja.xmlrpc.protocol.MethodResponse import org.ascheja.xmlrpc.protocol.writeToByteArray -public fun Route.xmlRpc(path: String, handler: suspend (MethodCall) -> MethodResponse) { +public fun Route.xmlRpc(path: String, handler: suspend ApplicationCall.(MethodCall) -> MethodResponse) { post(path) { if (call.request.contentType() != ContentType.Application.Xml) { return@post call.respond(HttpStatusCode.NotAcceptable, "") @@ -25,7 +26,7 @@ public fun Route.xmlRpc(path: String, handler: suspend (MethodCall) -> MethodRes val methodCall = call.receiveStream().use { input -> MethodCall.parse { it.parse(input) } } - val methodResponse = handler(methodCall) + val methodResponse = call.handler(methodCall) call.respondBytes(ContentType.Application.Xml, HttpStatusCode.OK) { methodResponse.toDocument().writeToByteArray() } diff --git a/ktor-server-xmlrpc/src/test/kotlin/LibraryTest.kt b/ktor-server-xmlrpc/src/test/kotlin/LibraryTest.kt index 58217be..89f1aba 100644 --- a/ktor-server-xmlrpc/src/test/kotlin/LibraryTest.kt +++ b/ktor-server-xmlrpc/src/test/kotlin/LibraryTest.kt @@ -4,10 +4,11 @@ package org.ascheja.xmlrpc.ktor.server -import io.ktor.application.Application import io.ktor.http.HttpHeaders import io.ktor.http.HttpMethod -import io.ktor.routing.routing +import io.ktor.server.application.Application +import io.ktor.server.application.ApplicationCall +import io.ktor.server.routing.routing import io.ktor.server.testing.handleRequest import io.ktor.server.testing.setBody import io.ktor.server.testing.withTestApplication @@ -22,7 +23,7 @@ import kotlin.test.assertEquals class LibraryTest { - private lateinit var handler: suspend (MethodCall) -> MethodResponse + private lateinit var handler: suspend ApplicationCall.(MethodCall) -> MethodResponse private val moduleFn: Application.() -> Unit = { routing {