https://kotlinlang.org logo
#ktor
Title
# ktor
j

JoakimForslund

02/05/2019, 9:35 AM
Why is the httpClient.request changing response/return type depending on if i use Features or not? Trying to wrap a promise like solution to the httpClient.request response but for whatever reason it will change between HttpResponse and io.ktor.client.features.observer.DelegatedResponse if I use logging. This is while using KotlinxSerializer too, so it does not even seem to map it to json
Copy code
internal suspend inline fun <reified T> makeRequest(): T? {

            val httpClient = HttpClient(mBlockHttpClientConfig)
            var result: T? = null
            try {
                result = httpClient.request<T> {
                    method = mHttpMethod
                    url {
                        takeFrom(restieUrl)
                        encodedPath = mPath
                    }
                    mContentType?.let {
                        accept(it)
                    }
                    mParameters.takeIf { it.size > 0 } ?: let {
                        mParameters.forEach { param ->
                            parameter(param.first, param.second)
                        }
                    }
                    body = mBody
                }
                SdkLogger(LOGGER_TYPE.DANGEROUS, "WTF: $result")
            } catch (e: Exception) {
                SdkLogger(LOGGER_TYPE.ERROR, "BOOOM: ${e.message}")
            } finally {
                return result
            }
        }
This is currently not able to cast the specified generic even though mBlockHttpClientConfig contains:
Copy code
install(JsonFeature) {
                serializer = KotlinxSerializer().apply {
                    setMapper(GameAvailable::class, GameAvailable.serializer())
                    setMapper(GameOptions::class, GameOptions.serializer())
                    setMapper(ReportMatch::class, ReportMatch.serializer())
                }
            }
This is all written in commonCode
Solved it, without wrapping Json.*.parse in a try and catch in the Serializer things will just stop mid-way without ever reporting what went wrong, this probably needs to added to ktor because else we can't handle the crash since its being done at feature level
2 Views