Skip to content

Commit

Permalink
Upgrade to ktor 2.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
ascheja committed Apr 13, 2022
1 parent fa155b8 commit 464347d
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 20 deletions.
2 changes: 1 addition & 1 deletion ktor-client-xmlrpc/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
val ktorVersion = "1.6.7"
val ktorVersion = "2.0.0"

dependencies {
api(project(":protocol"))
Expand Down
14 changes: 8 additions & 6 deletions ktor-client-xmlrpc/src/main/kotlin/Library.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<HttpResponse>(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
Expand Down
2 changes: 1 addition & 1 deletion ktor-server-xmlrpc/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
val ktorVersion = "1.6.7"
val ktorVersion = "2.0.0"

dependencies {
api(project(":protocol"))
Expand Down
19 changes: 10 additions & 9 deletions ktor-server-xmlrpc/src/main/kotlin/Library.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,29 @@

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, "")
}
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()
}
Expand Down
7 changes: 4 additions & 3 deletions ktor-server-xmlrpc/src/test/kotlin/LibraryTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down

0 comments on commit 464347d

Please # to comment.