mirror of
https://gitlab.com/2009scape/rt4-client.git
synced 2025-12-18 12:30:21 -07:00
fix up remaining spacing issues
This commit is contained in:
parent
3392d61d9e
commit
5bb81c7bd3
39 changed files with 244 additions and 271 deletions
|
|
@ -0,0 +1,60 @@
|
|||
package KondoKit.util
|
||||
|
||||
import java.io.IOException
|
||||
import java.net.HttpURLConnection
|
||||
import java.net.URL
|
||||
|
||||
/**
|
||||
* Lightweight helper for opening GET connections with shared defaults so callers don't repeat header setup.
|
||||
*/
|
||||
object HttpFetcher {
|
||||
const val DEFAULT_USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"
|
||||
|
||||
private val defaultHeaders = mapOf(
|
||||
"User-Agent" to DEFAULT_USER_AGENT,
|
||||
"Accept" to "*/*"
|
||||
)
|
||||
|
||||
fun openGetConnection(
|
||||
url: String,
|
||||
headers: Map<String, String> = emptyMap(),
|
||||
connectTimeoutMillis: Int? = null,
|
||||
readTimeoutMillis: Int? = null
|
||||
): HttpURLConnection {
|
||||
val connection = URL(url).openConnection() as HttpURLConnection
|
||||
connection.requestMethod = "GET"
|
||||
(defaultHeaders + headers).forEach { (key, value) ->
|
||||
connection.setRequestProperty(key, value)
|
||||
}
|
||||
connectTimeoutMillis?.let { connection.connectTimeout = it }
|
||||
readTimeoutMillis?.let { connection.readTimeout = it }
|
||||
return connection
|
||||
}
|
||||
|
||||
fun fetchString(
|
||||
url: String,
|
||||
headers: Map<String, String> = emptyMap(),
|
||||
connectTimeoutMillis: Int? = null,
|
||||
readTimeoutMillis: Int? = null
|
||||
): String {
|
||||
val connection = openGetConnection(url, headers, connectTimeoutMillis, readTimeoutMillis)
|
||||
val responseCode = connection.responseCode
|
||||
val responseStream = if (responseCode in 200..299) {
|
||||
connection.inputStream
|
||||
} else {
|
||||
connection.errorStream
|
||||
} ?: throw IOException("No response stream available for $url (HTTP $responseCode)")
|
||||
|
||||
val payload = responseStream.bufferedReader().use { it.readText() }
|
||||
if (responseCode !in 200..299) {
|
||||
throw HttpStatusException(url, responseCode, payload)
|
||||
}
|
||||
return payload
|
||||
}
|
||||
}
|
||||
|
||||
class HttpStatusException(
|
||||
val url: String,
|
||||
val statusCode: Int,
|
||||
val body: String
|
||||
) : IOException("Request to $url failed with HTTP $statusCode")
|
||||
Loading…
Add table
Add a link
Reference in a new issue