forcing raw response type for the wellknown as some servers may not correctly set the content-type

- migrates to the content negotiation api
This commit is contained in:
Adam Brown 2022-06-11 11:07:36 +01:00
parent 3fc28bd949
commit fbfc349b2e
5 changed files with 15 additions and 11 deletions

View File

@ -123,8 +123,10 @@ ext.Dependencies.with {
ktorAndroid = "io.ktor:ktor-client-android:${ktorVer}"
ktorCore = "io.ktor:ktor-client-core:${ktorVer}"
ktorSerialization = "io.ktor:ktor-client-serialization:${ktorVer}"
ktorJson = "io.ktor:ktor-serialization-kotlinx-json:${ktorVer}"
ktorLogging = "io.ktor:ktor-client-logging-jvm:${ktorVer}"
ktorJava = "io.ktor:ktor-client-java:${ktorVer}"
ktorContentNegotiation = "io.ktor:ktor-client-content-negotiation:${ktorVer}"
coil = "io.coil-kt:coil-compose:2.1.0"
accompanistSystemuicontroller = "com.google.accompanist:accompanist-systemuicontroller:0.24.9-beta"

View File

@ -9,5 +9,6 @@ dependencies {
implementation Dependencies.mavenCentral.ktorCore
implementation Dependencies.mavenCentral.ktorSerialization
implementation Dependencies.mavenCentral.ktorLogging
implementation Dependencies.mavenCentral.kotlinSerializationJson
implementation Dependencies.mavenCentral.ktorContentNegotiation
implementation Dependencies.mavenCentral.ktorJson
}

View File

@ -4,9 +4,9 @@ import app.dapk.st.matrix.common.CredentialsStore
import app.dapk.st.matrix.http.MatrixHttpClient
import app.dapk.st.matrix.http.ktor.internal.KtorMatrixHttpClient
import io.ktor.client.*
import io.ktor.client.plugins.json.*
import io.ktor.client.plugins.kotlinx.serializer.*
import io.ktor.client.plugins.contentnegotiation.*
import io.ktor.client.plugins.logging.*
import io.ktor.serialization.kotlinx.json.*
import kotlinx.serialization.json.Json
class KtorMatrixHttpClientFactory(
@ -14,10 +14,10 @@ class KtorMatrixHttpClientFactory(
private val includeLogging: Boolean,
) : MatrixHttpClient.Factory {
override fun create(json: Json): MatrixHttpClient {
override fun create(jsonInstance: Json): MatrixHttpClient {
val client = HttpClient {
install(JsonPlugin) {
serializer = KotlinxSerializer(json)
install(ContentNegotiation) {
json(jsonInstance)
}
expectSuccess = true
if (includeLogging) {

View File

@ -42,16 +42,16 @@ internal fun registerRequest(userName: String, password: String, baseUrl: String
baseUrl = baseUrl,
)
internal fun wellKnownRequest(baseUrl: String) = httpRequest<String>(
internal fun wellKnownRequest(baseUrl: String) = httpRequest<RawResponse>(
path = ".well-known/matrix/client",
method = MatrixHttpClient.Method.GET,
baseUrl = baseUrl,
authenticated = false,
)
@JvmInline
@Serializable
internal value class RawResponse(val value: String)
typealias RawResponse = ByteArray
fun RawResponse.readString() = this.toString(Charsets.UTF_8)
internal data class Auth(
val session: String,

View File

@ -6,6 +6,7 @@ import io.ktor.http.*
import kotlinx.serialization.SerializationException
import kotlinx.serialization.json.Json
import java.net.UnknownHostException
import java.nio.charset.Charset
internal typealias FetchWellKnownUseCase = suspend (String) -> WellKnownResult
@ -17,7 +18,7 @@ internal class FetchWellKnownUseCaseImpl(
override suspend fun invoke(domainUrl: String): WellKnownResult {
return runCatching {
val rawResponse = httpClient.execute(rawWellKnownRequestForServersWithoutContentTypes(domainUrl))
json.decodeFromString(ApiWellKnown.serializer(), rawResponse)
json.decodeFromString(ApiWellKnown.serializer(), rawResponse.readString())
}
.fold(
onSuccess = { WellKnownResult.Success(it) },