-
Notifications
You must be signed in to change notification settings - Fork 7
/
CustomHttpClient.kt
97 lines (87 loc) · 3.5 KB
/
CustomHttpClient.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import android.net.TrafficStats
import android.os.Handler
import android.os.Looper
import com.usabilla.sdk.ubform.net.http.UsabillaHttpClient
import com.usabilla.sdk.ubform.net.http.UsabillaHttpListener
import com.usabilla.sdk.ubform.net.http.UsabillaHttpRequest
import com.usabilla.sdk.ubform.net.http.UsabillaHttpResponse
import okhttp3.Call
import okhttp3.Callback
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.RequestBody.Companion.toRequestBody
import okhttp3.Response
import java.io.IOException
import java.util.HashMap
class CustomHttpClient : UsabillaHttpClient {
private val client = OkHttpClient()
private val mainHandler = Handler(Looper.getMainLooper())
override fun execute(request: UsabillaHttpRequest, listener: UsabillaHttpListener) {
val builder = Request.Builder().url(request.url)
addHeadersToRequest(request, builder)
addBodyToRequest(request, builder)
TrafficStats.setThreadStatsTag(1)
val callback = OkHttpAsynchronousCallback(listener)
client.newCall(builder.build()).enqueue(callback)
}
private fun addHeadersToRequest(request: UsabillaHttpRequest, builder: Request.Builder) {
request.headers?.let {
it.keys.forEach { key ->
it[key]?.let { header -> builder.addHeader(key, header) }
}
}
}
private fun addBodyToRequest(request: UsabillaHttpRequest, builder: Request.Builder) {
if (request.method == "POST" || request.method == "PATCH") {
val content = request.body ?: "application/json; charset=utf-8"
val body = content.toRequestBody()
if (request.method == "POST") {
builder.post(body)
return
}
builder.patch(body)
}
}
inner class OkHttpAsynchronousCallback(private val listener: UsabillaHttpListener) : Callback {
override fun onFailure(call: Call, e: IOException) {
val customResponse = CustomUsabillaHttpResponse(e)
mainHandler.post { listener.onFailure(customResponse) }
}
override fun onResponse(call: Call, response: Response) {
val customResponse = CustomUsabillaHttpResponse(response)
mainHandler.post {
if (response.isSuccessful) {
listener.onSuccess(customResponse)
} else {
listener.onFailure(customResponse)
}
}
}
}
internal class CustomUsabillaHttpResponse(response: Response?, e: IOException?) :
UsabillaHttpResponse {
override var statusCode: Int? = null
override var headers: Map<String, String>? = null
override var body: String? = null
override var error: String? = null
init {
run {
response?.let { res ->
res.body?.use {
body = it.string()
}
error = res.message
val tempHeaders = HashMap<String, String>()
val responseHeaders = res.headers
for (i in 0 until responseHeaders.size) {
tempHeaders[responseHeaders.name(i)] = responseHeaders.value(i)
}
headers = tempHeaders
statusCode = response.code
return@run
}
error = e?.localizedMessage ?: "Failure custom http client. Response is null"
}
}
}
}