[O] Proper null handling for http

pull/131/head
Azalea 2025-03-19 04:58:22 -04:00
parent 1a0e70636a
commit b519537b69
3 changed files with 10 additions and 11 deletions

View File

@ -20,7 +20,7 @@ fun HttpRequest.Builder.post(body: Any? = null) = this.POST(when (body) {
}).send()
inline fun <reified T> HttpResponse<String>.json(): T = body().json()
inline fun <reified T> HttpResponse<String>.json(): T? = body()?.json()
fun HttpRequest.Builder.postZ(body: String) = run {
header("Content-Type" to "application/json")
@ -29,6 +29,6 @@ fun HttpRequest.Builder.postZ(body: String) = run {
}
fun <T> HttpResponse<T>.header(key: String) = headers().firstValue(key).orElse(null)
fun HttpResponse<ByteArray>.bodyString() = body().toString(Charsets.UTF_8)
fun HttpResponse<ByteArray>.bodyZ() = ZLib.decompress(body()).decodeToString()
fun HttpResponse<ByteArray>.bodyString() = body()?.toString(Charsets.UTF_8)
fun HttpResponse<ByteArray>.bodyZ() = body()?.let { ZLib.decompress(it)?.decodeToString() }
fun HttpResponse<ByteArray>.bodyMaybeZ() = if (header("Content-Encoding") == "deflate") bodyZ() else bodyString()

View File

@ -30,8 +30,7 @@ class AllNetClient(val dns: String, val keychip: String, val game: String, val v
"$dns/sys/servlet/PowerOn".request()
.header("Content-Type" to "application/x-www-form-urlencoded")
.header("Pragma" to "DFI")
.post(
AllNetBillingDecoder.encodeAllNet(mapOf(
.post(AllNetBillingDecoder.encodeAllNet(mapOf(
"game_id" to game,
"ver" to version,
"serial" to keychipShort,
@ -39,10 +38,10 @@ class AllNetClient(val dns: String, val keychip: String, val game: String, val v
"encode" to "UTF-8", "format_ver" to "3", "hops" to "1", "token" to "2864179931"
)))
.bodyString()
.split("&")
.map { it.split("=") }
.filter { it.size == 2 }
.associate { it[0] to it[1] }["uri"]
?.split("&")
?.map { it.split("=") }
?.filter { it.size == 2 }
?.associate { it[0] to it[1] }?.get("uri")
?: throw Exception("PowerOn Failed: No game URL returned")
}

View File

@ -26,7 +26,7 @@ abstract class DataBroker(
inline fun <reified T> String.getNullable(key: String, data: JDict): T? = "$url/$this".request()
.postZ(mapper.write(data))
.bodyMaybeZ()
.jsonMap()[key]
?.jsonMaybeMap()?.get(key)
?.let { mapper.convert<T>(it) }
?.also {
if (it is List<*>) log("$this: ${it.size}")
@ -53,7 +53,7 @@ abstract class DataBroker(
"$url/UpsertUserAllApi".request().postZ(mapper.write(mapOf(
"userId" to allNet.userId,
"upsertUserAll" to data.jsonMap()
))).bodyMaybeZ().also { log(it) }
))).bodyMaybeZ()?.also { log(it) }
}
}