Skip to content

Commit

Permalink
migrate to ktor 2.0
Browse files Browse the repository at this point in the history
https://ktor.io/docs/migrating-2.html

* align with ktor versions
  * kotlin 1.6.21
  * coroutines 1.6.1
* fix packages for ktor 2.0
* fix deprecated methods for ktor 2.0
  • Loading branch information
nise-nabe authored and christian-draeger committed Jul 18, 2022
1 parent 3510e76 commit 0a358b7
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 24 deletions.
12 changes: 6 additions & 6 deletions buildSrc/buildSrc/src/main/kotlin/Deps.kt
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
object Versions {
const val kotlin = "1.5.31"
const val coroutines = "1.5.0"
const val ktor = "1.5.4"
const val kotlin = "1.6.21"
const val coroutines = "1.6.1"
const val ktor = "2.0.1"
const val serialization = "1.0.1"
const val datetime = "0.1.1"
const val jsoup = "1.13.1" // 1.14.3
const val htmlUnit = "2.63.0"
const val testContainers = "1.16.2"
const val wireMock = "2.28.0"
const val log4jOverSlf4j = "1.7.32"
const val log4jOverSlf4j = "1.7.36"
const val logback = "1.2.7"
const val strikt = "0.33.0"
const val mockk = "1.12.1"
Expand Down Expand Up @@ -60,8 +60,8 @@ object Deps {
val clientLogging = dependency("ktor-client-logging")
val serverNetty = dependency("ktor-server-netty")
val serverTestHost = dependency("ktor-server-test-host")
val freemarker = dependency("ktor-freemarker")
val locations = dependency("ktor-locations")
val freemarker = dependency("ktor-server-freemarker")
val locations = dependency("ktor-server-locations")
}

object KotlinX {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package it.skrape.fetcher

import io.ktor.client.*
import io.ktor.client.engine.apache.*
import io.ktor.client.features.*
import io.ktor.client.features.logging.*
import io.ktor.client.network.sockets.*
import io.ktor.client.plugins.*
import io.ktor.client.plugins.logging.*
import io.ktor.client.request.*
import io.ktor.client.statement.*
import io.ktor.network.sockets.*

public object AsyncFetcher : NonBlockingFetcher<Request> {

Expand All @@ -30,7 +30,7 @@ public object AsyncFetcher : NonBlockingFetcher<Request> {
}
HttpResponseValidator {

handleResponseException { cause: Throwable ->
handleResponseExceptionWithRequest { cause: Throwable, _: HttpRequest ->
when (cause) {
is SocketTimeoutException -> {
throw cause
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package it.skrape.fetcher

import io.ktor.client.*
import io.ktor.client.engine.apache.*
import io.ktor.client.features.*
import io.ktor.client.plugins.*
import io.ktor.client.request.*
import io.ktor.client.statement.*
import io.ktor.http.*
Expand Down Expand Up @@ -30,7 +30,7 @@ internal fun Request.toHttpRequest(): HttpRequestBuilder {
}
}
request.body?.run {
body = this
setBody(this)
}
timeout {
socketTimeoutMillis = request.timeout.toLong()
Expand Down Expand Up @@ -110,7 +110,7 @@ internal fun HttpClientConfig<ApacheEngineConfig>.trustSelfSignedClient() {
}

internal suspend fun HttpResponse.toResult(): Result = Result(
responseBody = this.readText(),
responseBody = this.bodyAsText(),
responseStatus = this.toStatus(),
contentType = this.contentType()?.toString()?.replace(" ", ""),
headers = this.headers.flattenEntries()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ class KtorAdapterTest {

class KtorBlockingFetcher(val ktorClient: HttpClient) : BlockingFetcher<HttpRequestBuilder> {
override fun fetch(request: HttpRequestBuilder): Result = runBlocking {
with(ktorClient.request<HttpResponse>(request)) {
with(ktorClient.request(request)) {
Result(
responseBody = readText(),
responseBody = bodyAsText(),
responseStatus = Result.Status(status.value, status.description),
contentType = contentType()?.toString(),
headers = headers.toMap().mapValues { it.value.firstOrNull().orEmpty() },
Expand Down Expand Up @@ -56,4 +56,4 @@ class KtorAdapterTest {
expectThat(result.responseStatus.code).isEqualTo(200)
expectThat(result.responseBody).contains("i'm the title")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package it.skrape.fetcher

import io.ktor.client.*
import io.ktor.client.engine.apache.*
import io.ktor.client.features.*
import io.ktor.client.features.logging.*
import io.ktor.client.network.sockets.*
import io.ktor.client.plugins.*
import io.ktor.client.plugins.logging.*
import io.ktor.client.request.*
import io.ktor.client.statement.*
import io.ktor.http.*
import io.ktor.network.sockets.*
import io.ktor.util.*
import io.ktor.util.network.*
import kotlinx.coroutines.runBlocking
Expand Down Expand Up @@ -39,7 +39,7 @@ public object HttpFetcher : BlockingFetcher<Request> {
}
HttpResponseValidator {

handleResponseException { cause: Throwable ->
handleResponseExceptionWithRequest { cause: Throwable, _: HttpRequest ->
when (cause) {
is SocketTimeoutException -> {
throw cause
Expand Down Expand Up @@ -81,7 +81,7 @@ private fun Request.toHttpRequest(): HttpRequestBuilder {
}
}
request.body?.run {
body = this
setBody(this)
}
timeout {
socketTimeoutMillis = request.timeout.toLong()
Expand Down Expand Up @@ -162,7 +162,7 @@ private fun HttpClientConfig<ApacheEngineConfig>.trustSelfSignedClient() {

private fun HttpResponse.toResult(): Result =
Result(
responseBody = runBlocking { readText() },
responseBody = runBlocking { bodyAsText() },
responseStatus = toStatus(),
contentType = contentType()?.toString()?.replace(" ", ""),
headers = headers.flattenEntries()
Expand Down
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
kotlin.code.style=official
kotlin_version=1.6.0
kotlin_version=1.6.21
org.gradle.jvmargs=-Dfile.encoding=UTF-8

release_version=1.2.1
release_version=1.2.1

0 comments on commit 0a358b7

Please sign in to comment.