Hello! I want to create a simple Kotlin class that...
# multiplatform
a
Hello! I want to create a simple Kotlin class that I can use to send multiple HTTP requests on the same HttpClient connection using ktor. When I create my HttpClient in a class variable and use it to send http requests in a separate function, I get the following error (running on iOS):
Copy code
kotlinx.coroutines.JobCancellationException: Parent job is Completed
I managed to solve this by using the following code:
Copy code
fun class MyClass {
    private var client = HttpClient()
    private val apiUrl = "<url-string>"

    @Throws(Exception::class)
    suspend fun myFunction(): String = withContext(Dispatchers.Default) {
        try {
            val response: HttpResponse = client.get(apiUrlOneCall)
            if ((200..299).contains(response.status.value)) {
                return@withContext response.bodyAsText()
            } else {
                throw Exception(response.bodyAsText())
            }
        } catch (e: Exception) {
        throw e
        }
    }

    fun closeConnection() {
        client.close()
    }
}
Anyone who has encountered this issue before and know if this is an okay solution?