Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ktor Dispatcher patch v1.0.4 #157

Merged
merged 3 commits into from
Dec 17, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import kotlinx.serialization.json.Json
import kotlinx.coroutines.CoroutineScope
import com.sdk.growthbook.PlatformDependentIODispatcher
import kotlinx.coroutines.launch
import io.ktor.client.request.get
import io.ktor.client.request.post
import io.ktor.client.request.prepareGet
import io.ktor.client.call.body
Expand All @@ -24,6 +23,9 @@ import kotlinx.coroutines.channels.awaitClose
import io.ktor.http.ContentType
import io.ktor.http.contentType
import io.ktor.client.request.HttpRequestBuilder
import io.ktor.client.request.setBody
import io.ktor.http.HttpStatusCode
import io.ktor.utils.io.core.use
import io.ktor.utils.io.errors.IOException
import kotlinx.serialization.json.JsonArray
import kotlinx.serialization.json.JsonElement
Expand Down Expand Up @@ -63,21 +65,27 @@ class GBNetworkDispatcherKtor(
onError: (Throwable) -> Unit
): Job =
CoroutineScope(PlatformDependentIODispatcher).launch {
try {
val result = client.get(request)
client.use {
try {
onSuccess(result.body())
} catch (exception: Exception) {
val result = prepareGetRequest(request).execute()
try {
if (result.status == HttpStatusCode.OK) {
onSuccess(result.body())
} else {
onError(Exception("Response status in not ok: Response is: ${result.body<String>()}"))
}
} catch (exception: Exception) {
onError(exception)
}
} catch (clientRequestException: ClientRequestException) {
onError(clientRequestException)
} catch (serverResponseException: ServerResponseException) {
onError(serverResponseException)
} catch (ioException: IOException) {
onError(ioException)
} catch (exception: Exception) { // for the case if something was missed
onError(exception)
}
} catch (clientRequestException: ClientRequestException) {
onError(clientRequestException)
} catch (serverResponseException: ServerResponseException) {
onError(serverResponseException)
} catch (ioException: IOException) {
onError(ioException)
} catch (exception: Exception) { // for the case if something was missed
onError(exception)
}
}

Expand Down Expand Up @@ -131,31 +139,35 @@ class GBNetworkDispatcherKtor(
onError: (Throwable) -> Unit
) {
CoroutineScope(PlatformDependentIODispatcher).launch {
try {
val response = client.post(url) {
headers {
append("Content-Type", "application/json")
append("Accept", "application/json")
client.use { okHttpClient ->
try {
val response = okHttpClient.post(url) {
headers {
append("Content-Type", "application/json")
append("Accept", "application/json")
}
contentType(ContentType.Application.Json)
setBody(bodyParams.toJsonElement()) // don't comment this code. Here we pass request body for POST request
if (enableLogging) {
println("body = $body")
}
}
contentType(ContentType.Application.Json)
//setBody(bodyParams.toJsonElement())
if (response.status.value in 200..299) {
onSuccess(response.body())
} else {
onError(
Exception(
"Response not successful status code is : ${response.status.value} " +
"and description : ${response.status.description}"
)
)
}
} catch (e: Exception) {
if (enableLogging) {
println("body = $body")
println("exception $e")
}
onError(e)
}
if (response.status.value in 200 .. 299) {
onSuccess(response.body())
} else {
onError(
Exception(
"Response not successful status code is : ${response.status.value} " +
"and description : ${response.status.description}"))
}
} catch (e: Exception) {
if (enableLogging) {
println("exception $e")
}
onError(e)
}
}
}
Expand Down
Loading