@@ -3,10 +3,10 @@ package com.aman.cryptotracker.network
3
3
import com.aman.cryptotracker.entity.CryptoResponse
4
4
import com.aman.cryptotracker.network.ApiKey.COIN_MARKET_KEY
5
5
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.*
9
8
import io.ktor.client.request.*
9
+ import io.ktor.serialization.kotlinx.json.*
10
10
import kotlinx.coroutines.Dispatchers
11
11
import kotlinx.coroutines.withContext
12
12
import kotlinx.serialization.json.Json
@@ -17,41 +17,36 @@ import kotlinx.serialization.json.Json
17
17
class CryptoApi {
18
18
19
19
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 {
25
23
ignoreUnknownKeys = true
26
- }
27
-
28
- serializer = KotlinxSerializer (json)
24
+ isLenient = true
25
+ })
29
26
}
30
27
31
- install(Logging ) {
32
- logger = Logger .DEFAULT
33
- level = LogLevel .HEADERS
34
- }
35
28
}
36
29
}
37
30
38
31
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 ) {
40
34
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" )
45
39
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
+ }
49
43
44
+ }.body()
50
45
}
51
- }
52
46
53
- companion object {
47
+ companion object {
54
48
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"
56
51
}
57
52
}
0 commit comments