Skip to content

Commit 9261713

Browse files
committed
Upgraded to ktor-client 1.6.8
1 parent 00025b6 commit 9261713

File tree

2 files changed

+32
-20
lines changed
  • kotlin-http-client/kotlin-http-client-ktor

2 files changed

+32
-20
lines changed

kotlin-http-client/kotlin-http-client-ktor/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
<artifactId>kotlin-http-client-ktor</artifactId>
1313

1414
<properties>
15-
<ktor.version>1.1.3</ktor.version>
15+
<ktor.version>1.6.8</ktor.version>
1616
</properties>
1717

1818
<dependencies>
@@ -35,7 +35,7 @@
3535
</dependency>
3636
<dependency>
3737
<groupId>io.ktor</groupId>
38-
<artifactId>ktor-client-auth-basic</artifactId>
38+
<artifactId>ktor-client-auth-jvm</artifactId>
3939
<version>${ktor.version}</version>
4040
</dependency>
4141
</dependencies>

kotlin-http-client/kotlin-http-client-ktor/src/main/kotlin/com/linkedplanet/kotlinhttpclient/ktor/KtorHttpClient.kt

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ import com.linkedplanet.kotlinhttpclient.error.HttpDomainError
2727
import io.ktor.client.HttpClient
2828
import io.ktor.client.call.receive
2929
import io.ktor.client.engine.apache.Apache
30-
import io.ktor.client.features.auth.basic.BasicAuth
30+
import io.ktor.client.features.auth.Auth
31+
import io.ktor.client.features.auth.providers.BasicAuthCredentials
32+
import io.ktor.client.features.auth.providers.basic
3133
import io.ktor.client.features.json.GsonSerializer
3234
import io.ktor.client.features.json.JsonFeature
3335
import io.ktor.client.request.*
@@ -41,9 +43,13 @@ fun httpClient(username: String, password: String) =
4143
install(JsonFeature) {
4244
serializer = GsonSerializer()
4345
}
44-
install(BasicAuth) {
45-
this.username = username
46-
this.password = password
46+
install(Auth) {
47+
basic {
48+
sendWithoutRequest { true }
49+
credentials {
50+
BasicAuthCredentials(username = username, password = password)
51+
}
52+
}
4753
}
4854
}
4955

@@ -72,6 +78,8 @@ class KtorHttpClient(
7278
requestBuilder.url("$baseUrl/$path$parameterString")
7379
requestBuilder.contentType(parsedContentType)
7480
if (bodyIn != null) {
81+
// Keep JsonParser instantiation for downwards compatibility
82+
@Suppress("DEPRECATION")
7583
requestBuilder.body = JsonParser().parse(bodyIn)
7684
}
7785
}
@@ -80,32 +88,36 @@ class KtorHttpClient(
8088
method: String,
8189
path: String,
8290
params: Map<String, String>,
83-
bodyIn: String?,
91+
body: String?,
8492
contentType: String?,
8593
headers: Map<String, String>
8694
): Either<HttpDomainError, HttpResponse<String>> {
8795
val pathWithoutPrefix = path.removePrefix("/")
8896
return when (method) {
8997
"GET" -> {
90-
httpClient.get<io.ktor.client.response.HttpResponse> {
91-
prepareRequest(this, pathWithoutPrefix, params, bodyIn, contentType)
98+
httpClient.get<io.ktor.client.statement.HttpResponse> {
99+
prepareRequest(this, pathWithoutPrefix, params, body, contentType)
92100
}.handleResponse()
93101
}
102+
94103
"POST" -> {
95-
httpClient.post<io.ktor.client.response.HttpResponse> {
96-
prepareRequest(this, pathWithoutPrefix, params, bodyIn, contentType)
104+
httpClient.post<io.ktor.client.statement.HttpResponse>(pathWithoutPrefix) {
105+
prepareRequest(this, pathWithoutPrefix, params, body, contentType)
97106
}.handleResponse()
98107
}
108+
99109
"PUT" -> {
100-
httpClient.put<io.ktor.client.response.HttpResponse> {
101-
prepareRequest(this, pathWithoutPrefix, params, bodyIn, contentType)
110+
httpClient.put<io.ktor.client.statement.HttpResponse> {
111+
prepareRequest(this, pathWithoutPrefix, params, body, contentType)
102112
}.handleResponse()
103113
}
114+
104115
"DELETE" -> {
105-
httpClient.delete<io.ktor.client.response.HttpResponse> {
106-
prepareRequest(this, pathWithoutPrefix, params, bodyIn, contentType)
116+
httpClient.delete<io.ktor.client.statement.HttpResponse> {
117+
prepareRequest(this, pathWithoutPrefix, params, body, contentType)
107118
}.handleResponse()
108119
}
120+
109121
else -> {
110122
HttpDomainError(500, "HTTP-ERROR", "Method '$method' not available").left()
111123
}
@@ -114,13 +126,13 @@ class KtorHttpClient(
114126

115127
override suspend fun executeDownload(
116128
method: String,
117-
url: String,
129+
path: String,
118130
params: Map<String, String>,
119131
body: String?,
120132
contentType: String?
121133
): Either<HttpDomainError, HttpResponse<ByteArray>> {
122-
return httpClient.get<io.ktor.client.response.HttpResponse> {
123-
url(url)
134+
return httpClient.get<io.ktor.client.statement.HttpResponse> {
135+
url(path)
124136
}.handleResponse()
125137
}
126138

@@ -132,7 +144,7 @@ class KtorHttpClient(
132144
filename: String,
133145
byteArray: ByteArray
134146
): Either<HttpDomainError, HttpResponse<ByteArray>> =
135-
httpClient.post<io.ktor.client.response.HttpResponse> {
147+
httpClient.post<io.ktor.client.statement.HttpResponse> {
136148
url("$baseUrl$url")
137149
header("Connection", "keep-alive")
138150
header("Cache-Control", "no-cache")
@@ -150,7 +162,7 @@ class KtorHttpClient(
150162
}.handleResponse()
151163

152164

153-
private suspend inline fun <reified T> io.ktor.client.response.HttpResponse.handleResponse(): Either<HttpDomainError, HttpResponse<T>> =
165+
private suspend inline fun <reified T> io.ktor.client.statement.HttpResponse.handleResponse(): Either<HttpDomainError, HttpResponse<T>> =
154166
if (this.status.value < 400) {
155167
HttpResponse<T>(
156168
this.status.value,

0 commit comments

Comments
 (0)