blob: c321cf2d5f8ef5558ecadf95a4904cab5099880f (
plain)
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
|
package at.hannibal2.skyhanni.utils
import com.google.gson.JsonObject
import com.google.gson.JsonParser
import org.apache.http.client.config.RequestConfig
import org.apache.http.client.methods.HttpGet
import org.apache.http.impl.client.HttpClientBuilder
import org.apache.http.impl.client.HttpClients
import org.apache.http.message.BasicHeader
import org.apache.http.util.EntityUtils
object APIUtil {
private val parser = JsonParser()
val builder: HttpClientBuilder =
HttpClients.custom().setUserAgent("SkyHanni")
.setDefaultHeaders(
mutableListOf(
BasicHeader("Pragma", "no-cache"),
BasicHeader("Cache-Control", "no-cache")
)
)
.setDefaultRequestConfig(
RequestConfig.custom()
.build()
)
.useSystemProperties()
fun getJSONResponse(urlString: String): JsonObject {
val client = builder.build()
try {
client.execute(HttpGet(urlString)).use { response ->
val entity = response.entity
if (entity != null) {
val retSrc = EntityUtils.toString(entity)
return parser.parse(retSrc) as JsonObject
}
}
} catch (ex: Throwable) {
ex.printStackTrace()
LorenzUtils.error("SkyHanni ran into an ${ex::class.simpleName ?: "error"} whilst fetching a resource. See logs for more details.")
} finally {
client.close()
}
return JsonObject()
}
}
|