Skip to content

Commit

Permalink
Use Retrofit client without cache in OpenSeaService
Browse files Browse the repository at this point in the history
- cancel coroutine scope in NftHoldingsService
- change StateFlow to SharedFlow
  • Loading branch information
omurovch committed Oct 5, 2022
1 parent 675e166 commit 564fd97
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import io.horizontalsystems.bankwallet.core.App
import io.horizontalsystems.bankwallet.core.INetworkManager
import io.reactivex.Flowable
import io.reactivex.Single
import okhttp3.Interceptor
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
Expand Down Expand Up @@ -171,6 +172,36 @@ object APIClient {
level = HttpLoggingInterceptor.Level.BASIC
}

private fun buildClient(headers: Map<String, String>): OkHttpClient {
val headersInterceptor = Interceptor { chain ->
val requestBuilder = chain.request().newBuilder()
headers.forEach { (name, value) ->
requestBuilder.header(name, value)
}
chain.proceed(requestBuilder.build())
}

return OkHttpClient.Builder()
.connectTimeout(5, TimeUnit.SECONDS)
.readTimeout(60, TimeUnit.SECONDS)
.addInterceptor(logger)
.addInterceptor(headersInterceptor)
.build()
}

fun build(baseUrl: String, headers: Map<String, String> = mapOf()): Retrofit {
val client = buildClient(headers)

return Retrofit.Builder()
.baseUrl(baseUrl)
.client(client)
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.addConverterFactory(
GsonConverterFactory.create(GsonBuilder().setLenient().create())
)
.build()
}

//share OkHttpClient
val okHttpClient: OkHttpClient = OkHttpClient.Builder()
.addInterceptor(logger)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import io.horizontalsystems.bankwallet.core.providers.nft.OpenSeaNftProvider
import io.horizontalsystems.bankwallet.core.storage.NftStorage
import io.horizontalsystems.bankwallet.entities.nft.*
import io.horizontalsystems.marketkit.models.BlockchainType
import kotlinx.coroutines.channels.BufferOverflow
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.filterNotNull
import kotlinx.coroutines.flow.MutableSharedFlow

class NftMetadataManager(
marketKit: MarketKitWrapper,
Expand All @@ -19,8 +19,8 @@ class NftMetadataManager(
BlockchainType.Ethereum to OpenSeaNftProvider(marketKit, appConfigProvider)
)

private val _addressMetadataFlow = MutableStateFlow<Pair<NftKey, NftAddressMetadata>?>(null)
val addressMetadataFlow: Flow<Pair<NftKey, NftAddressMetadata>> = _addressMetadataFlow.filterNotNull()
private val _addressMetadataFlow = MutableSharedFlow<Pair<NftKey, NftAddressMetadata>?>(replay = 1, onBufferOverflow = BufferOverflow.DROP_OLDEST)
val addressMetadataFlow: Flow<Pair<NftKey, NftAddressMetadata>?> = _addressMetadataFlow

suspend fun addressMetadata(blockchainType: BlockchainType, address: String): NftAddressMetadata {
return provider(blockchainType).addressMetadata(blockchainType, address)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.horizontalsystems.bankwallet.core.providers.nft

import io.horizontalsystems.marketkit.providers.RetrofitUtils
import io.horizontalsystems.bankwallet.core.managers.APIClient
import retrofit2.http.GET
import retrofit2.http.Path
import retrofit2.http.Query
Expand All @@ -11,14 +11,14 @@ class OpenSeaService(
apiKey: String
) {
private val service by lazy {
RetrofitUtils.build(
APIClient.build(
baseUrl = "https://api.opensea.io/api/v1/",
headers = mapOf("User-Agent" to "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36")
).create(OpenSeaApi::class.java)
}

private val hsService by lazy {
RetrofitUtils.build("${hsBaseUrl}/v1/nft/", mapOf("apikey" to apiKey))
APIClient.build("${hsBaseUrl}/v1/nft/", mapOf("apikey" to apiKey))
.create(HsNftApi::class.java)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,10 @@ class NftHoldingsService(
syncItems()
}

fun stop() {
adaptersMapScope?.cancel()
}

data class NftCollectionItem(
val metadata: NftCollectionShortMetadata?,
val nftItems: List<NftItem>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ class NftHoldingsViewModel(
}

override fun onCleared() {
service.stop()
totalService.stop()
}

Expand Down

0 comments on commit 564fd97

Please sign in to comment.