8
8
package net.pearx.kpastebin
9
9
10
10
import io.ktor.client.HttpClient
11
+ import io.ktor.client.engine.HttpClientEngine
11
12
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
12
16
import io.ktor.client.request.get
13
17
import io.ktor.client.request.post
14
18
import io.ktor.http.*
@@ -22,18 +26,32 @@ import net.pearx.kpastebin.model.UserDetails
22
26
* Pastebin API client with specified unique developer API key.
23
27
* You can get your key on [official Pastebin website](https://pastebin.com/doc_api#1).
24
28
*
25
- * @param http Ktor [HttpClient ] to use
29
+ * @param engine Ktor [HttpClientEngine ] to use
26
30
* @param devKey Unique developer API key
27
31
* @param userKey user key used for requests. Use null for guest user.
28
32
*/
29
33
public class PastebinClient (
30
- private val http : HttpClient ,
34
+ private val engine : HttpClientEngine ,
31
35
private val devKey : String ,
32
36
/* *
33
37
* User key used for requests. Use null for guest user. It also can be set using [login] method.
34
38
*/
35
39
public var userKey : String? = null
36
40
) {
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
+
37
55
private suspend fun sendRequest (url : String , userKeyRequired : Boolean , userKey : String? , parameters : Parameters ): String {
38
56
val usrKey = userKey ? : this .userKey
39
57
if (userKeyRequired && usrKey == null )
0 commit comments