Add contentType and charset extension properties to HttpResponse

Signed-off-by: Graham <gpe@openrs2.org>
bzip2
Graham 4 years ago
parent 326371312d
commit b2ccbad031
  1. 12
      http/src/main/kotlin/org/openrs2/http/HttpResponseExtensions.kt

@ -1,7 +1,10 @@
package org.openrs2.http package org.openrs2.http
import com.google.common.net.HttpHeaders
import com.google.common.net.MediaType
import java.io.IOException import java.io.IOException
import java.net.http.HttpResponse import java.net.http.HttpResponse
import java.nio.charset.Charset
private val DEFAULT_EXPECTED_STATUS_CODES = setOf(200) private val DEFAULT_EXPECTED_STATUS_CODES = setOf(200)
@ -11,3 +14,12 @@ public fun <T> HttpResponse<T>.checkStatusCode(expectedStatusCodes: Set<Int> = D
throw IOException("Unexpected status code: $status") throw IOException("Unexpected status code: $status")
} }
} }
public val <T> HttpResponse<T>.contentType: MediaType?
get() = headers().firstValue(HttpHeaders.CONTENT_TYPE).map(MediaType::parse).orElse(null)
public val <T> HttpResponse<T>.charset: Charset?
get() {
val contentType = contentType ?: return null
return contentType.charset().orNull()
}

Loading…
Cancel
Save