Skip to content

Commit de0ed7f

Browse files
committed
fix ktor client engine issue
1 parent 19c0cac commit de0ed7f

File tree

6 files changed

+45
-27
lines changed

6 files changed

+45
-27
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.aman.cryptotracker.network
2+
3+
import io.ktor.client.engine.*
4+
import io.ktor.client.engine.android.*
5+
6+
actual fun getKtorClientEngine(): HttpClientEngine = Android.create()

shared/src/commonMain/kotlin/com/aman/cryptotracker/network/CryptoApi.kt

+21-26
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ package com.aman.cryptotracker.network
33
import com.aman.cryptotracker.entity.CryptoResponse
44
import com.aman.cryptotracker.network.ApiKey.COIN_MARKET_KEY
55
import io.ktor.client.*
6-
import io.ktor.client.features.json.JsonFeature
7-
import io.ktor.client.features.json.serializer.*
8-
import io.ktor.client.features.logging.*
6+
import io.ktor.client.call.*
7+
import io.ktor.client.plugins.contentnegotiation.*
98
import io.ktor.client.request.*
9+
import io.ktor.serialization.kotlinx.json.*
1010
import kotlinx.coroutines.Dispatchers
1111
import kotlinx.coroutines.withContext
1212
import kotlinx.serialization.json.Json
@@ -17,41 +17,36 @@ import kotlinx.serialization.json.Json
1717
class CryptoApi {
1818

1919
private val httpClient by lazy {
20-
21-
HttpClient {
22-
23-
install(JsonFeature) {
24-
val json = Json {
20+
HttpClient(getKtorClientEngine()) {
21+
install(ContentNegotiation) {
22+
json(Json {
2523
ignoreUnknownKeys = true
26-
}
27-
28-
serializer = KotlinxSerializer(json)
24+
isLenient = true
25+
})
2926
}
3027

31-
install(Logging) {
32-
logger = Logger.DEFAULT
33-
level = LogLevel.HEADERS
34-
}
3528
}
3629
}
3730

3831

39-
suspend fun getCryptoData(limit:Int = DEFAULT_LIMIT):CryptoResponse = withContext(Dispatchers.Default) {
32+
suspend fun getCryptoData(limit: Int = DEFAULT_LIMIT): CryptoResponse =
33+
withContext(Dispatchers.Default) {
4034

41-
return@withContext httpClient.get(URL){
42-
parameter("start", 1)
43-
parameter("limit", limit)
44-
parameter("convert", "USD")
35+
return@withContext httpClient.get(URL) {
36+
parameter("start", 1)
37+
parameter("limit", limit)
38+
parameter("convert", "USD")
4539

46-
headers {
47-
append("X-CMC_PRO_API_KEY",COIN_MARKET_KEY)
48-
}
40+
headers {
41+
append("X-CMC_PRO_API_KEY", COIN_MARKET_KEY)
42+
}
4943

44+
}.body()
5045
}
51-
}
5246

53-
companion object{
47+
companion object {
5448
private const val DEFAULT_LIMIT = 50
55-
private const val URL = "https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest"
49+
private const val URL =
50+
"https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest"
5651
}
5752
}

shared/src/commonMain/kotlin/com/aman/cryptotracker/network/CryptoRepository.kt

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.aman.cryptotracker.network
22

33
import com.aman.cryptotracker.entity.Data
4-
import io.ktor.client.features.logging.*
54

65

76
/**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.aman.cryptotracker.network
2+
3+
import io.ktor.client.engine.*
4+
5+
expect fun getKtorClientEngine():HttpClientEngine
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.aman.cryptotracker.network
2+
3+
import io.ktor.client.engine.*
4+
import io.ktor.client.engine.java.*
5+
6+
actual fun getKtorClientEngine(): HttpClientEngine = Java.create()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.aman.cryptotracker.network
2+
3+
import io.ktor.client.engine.HttpClientEngine
4+
import io.ktor.client.engine.darwin.Darwin
5+
6+
7+
actual fun getKtorClientEngine(): HttpClientEngine = Darwin.create()

0 commit comments

Comments
 (0)