Skip to content

Commit b5c548a

Browse files
committed
Fix PastebinClient on Kotlin/Native
1 parent e1ddac3 commit b5c548a

File tree

2 files changed

+124
-110
lines changed

2 files changed

+124
-110
lines changed

src/commonMain/kotlin/net/pearx/kpastebin/PastebinClient.kt

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@
88
package net.pearx.kpastebin
99

1010
import io.ktor.client.HttpClient
11+
import io.ktor.client.engine.HttpClientEngine
1112
import io.ktor.client.features.ClientRequestException
13+
import io.ktor.client.features.HttpResponseValidator
14+
import io.ktor.client.features.RedirectResponseException
15+
import io.ktor.client.features.ServerResponseException
1216
import io.ktor.client.request.get
1317
import io.ktor.client.request.post
1418
import io.ktor.http.*
@@ -22,18 +26,32 @@ import net.pearx.kpastebin.model.UserDetails
2226
* Pastebin API client with specified unique developer API key.
2327
* You can get your key on [official Pastebin website](https://pastebin.com/doc_api#1).
2428
*
25-
* @param http Ktor [HttpClient] to use
29+
* @param engine Ktor [HttpClientEngine] to use
2630
* @param devKey Unique developer API key
2731
* @param userKey user key used for requests. Use null for guest user.
2832
*/
2933
public class PastebinClient(
30-
private val http: HttpClient,
34+
private val engine: HttpClientEngine,
3135
private val devKey: String,
3236
/**
3337
* User key used for requests. Use null for guest user. It also can be set using [login] method.
3438
*/
3539
public var userKey: String? = null
3640
) {
41+
private val http = HttpClient(engine) {
42+
HttpResponseValidator { // https://youtrack.jetbrains.com/issue/KTOR-406
43+
validateResponse { response ->
44+
when (response.status.value) {
45+
in 300..399 -> throw RedirectResponseException(response)
46+
in 400..499 -> throw ClientRequestException(response)
47+
in 500..599 -> throw ServerResponseException(response)
48+
}
49+
}
50+
}
51+
52+
expectSuccess = false
53+
}
54+
3755
private suspend fun sendRequest(url: String, userKeyRequired: Boolean, userKey: String?, parameters: Parameters): String {
3856
val usrKey = userKey ?: this.userKey
3957
if (userKeyRequired && usrKey == null)

0 commit comments

Comments
 (0)