Updated on 2026-08-14
This commit is contained in:
parent
0ecd5e8da1
commit
b87f415e29
13 changed files with 175 additions and 46 deletions
|
|
@ -0,0 +1,8 @@
|
|||
package com.tangem.datasource.api.oneinch
|
||||
|
||||
import com.tangem.datasource.api.oneinch.models.SwapErrorDto
|
||||
|
||||
data class BaseOneInchResponse<T>(
|
||||
val body: T?,
|
||||
val errorDto: SwapErrorDto?,
|
||||
)
|
||||
|
|
@ -8,6 +8,7 @@ import com.tangem.datasource.api.oneinch.models.QuoteResponse
|
|||
import com.tangem.datasource.api.oneinch.models.StatusResponse
|
||||
import com.tangem.datasource.api.oneinch.models.SwapResponse
|
||||
import com.tangem.datasource.api.oneinch.models.TokensResponse
|
||||
import retrofit2.Response
|
||||
import retrofit2.http.GET
|
||||
import retrofit2.http.Query
|
||||
|
||||
|
|
@ -127,7 +128,7 @@ interface OneInchApi {
|
|||
@Query("mainRouteParts") mainRouteParts: String? = null,
|
||||
@Query("parts") parts: String? = null,
|
||||
@Query("gasPrice") gasPrice: String? = null,
|
||||
): QuoteResponse
|
||||
): Response<QuoteResponse>
|
||||
|
||||
/**
|
||||
* Generate data for calling the 1inch router for exchange
|
||||
|
|
@ -197,6 +198,6 @@ interface OneInchApi {
|
|||
@Query("complexityLevel") complexityLevel: String? = null,
|
||||
@Query("gasLimit") gasLimit: String? = null,
|
||||
@Query("gasPrice") gasPrice: String? = null,
|
||||
): SwapResponse
|
||||
): Response<SwapResponse>
|
||||
//endregion Swap
|
||||
}
|
||||
|
|
@ -0,0 +1,34 @@
|
|||
package com.tangem.datasource.api.oneinch
|
||||
|
||||
import com.squareup.moshi.Moshi
|
||||
import com.tangem.datasource.api.oneinch.models.SwapErrorDto
|
||||
import retrofit2.HttpException
|
||||
import retrofit2.Response
|
||||
import javax.inject.Inject
|
||||
|
||||
class OneInchErrorsHandler @Inject constructor(private val moshi: Moshi) {
|
||||
|
||||
fun <T> handleOneInchResponse(response: Response<T>): BaseOneInchResponse<T> {
|
||||
val body = response.body()
|
||||
return if (response.isSuccessful) {
|
||||
return BaseOneInchResponse(body, null)
|
||||
} else {
|
||||
when (response.code()) {
|
||||
HTTP_CODE_400 -> {
|
||||
if (body != null) {
|
||||
BaseOneInchResponse(null, moshi.adapter(SwapErrorDto::class.java).fromJson(body.toString()))
|
||||
} else {
|
||||
throw HttpException(response)
|
||||
}
|
||||
}
|
||||
else -> {
|
||||
throw HttpException(response)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
private const val HTTP_CODE_400 = 400
|
||||
}
|
||||
}
|
||||
|
|
@ -15,7 +15,6 @@ import dagger.hilt.InstallIn
|
|||
import dagger.hilt.components.SingletonComponent
|
||||
import okhttp3.OkHttpClient
|
||||
import okhttp3.logging.HttpLoggingInterceptor
|
||||
import retrofit2.Converter
|
||||
import retrofit2.Retrofit
|
||||
import retrofit2.converter.moshi.MoshiConverterFactory
|
||||
import javax.inject.Singleton
|
||||
|
|
@ -28,10 +27,12 @@ class NetworkModule {
|
|||
@Singleton
|
||||
fun provideTangemTechApi(
|
||||
okHttpClient: OkHttpClient,
|
||||
converter: Converter.Factory,
|
||||
moshi: Moshi,
|
||||
): TangemTechApi {
|
||||
return Retrofit.Builder()
|
||||
.addConverterFactory(converter)
|
||||
.addConverterFactory(
|
||||
MoshiConverterFactory.create(moshi),
|
||||
)
|
||||
.baseUrl(DEV_TANGEM_TECH_BASE_URL)
|
||||
.client(okHttpClient)
|
||||
.build()
|
||||
|
|
@ -41,9 +42,11 @@ class NetworkModule {
|
|||
@Provides
|
||||
@Singleton
|
||||
@OneInchEthereum
|
||||
fun provideOneInchEthereumApi(converter: Converter.Factory): OneInchApi {
|
||||
fun provideOneInchEthereumApi(moshi: Moshi): OneInchApi {
|
||||
return Retrofit.Builder()
|
||||
.addConverterFactory(converter)
|
||||
.addConverterFactory(
|
||||
MoshiConverterFactory.create(moshi),
|
||||
)
|
||||
.baseUrl(ONE_INCH_ETHER_BASE_URL)
|
||||
.client(
|
||||
OkHttpClient.Builder()
|
||||
|
|
@ -58,9 +61,11 @@ class NetworkModule {
|
|||
|
||||
@Provides
|
||||
@Singleton
|
||||
fun provideReferralApi(okHttpClient: OkHttpClient, converter: Converter.Factory): ReferralApi {
|
||||
fun provideReferralApi(okHttpClient: OkHttpClient, moshi: Moshi): ReferralApi {
|
||||
return Retrofit.Builder()
|
||||
.addConverterFactory(converter)
|
||||
.addConverterFactory(
|
||||
MoshiConverterFactory.create(moshi),
|
||||
)
|
||||
.baseUrl(DEV_TANGEM_TECH_BASE_URL)
|
||||
.client(okHttpClient)
|
||||
.build()
|
||||
|
|
@ -80,13 +85,11 @@ class NetworkModule {
|
|||
|
||||
@Provides
|
||||
@Singleton
|
||||
fun provideMoshiConverter(): Converter.Factory {
|
||||
return MoshiConverterFactory.create(
|
||||
Moshi.Builder()
|
||||
.add(KotlinJsonAdapterFactory())
|
||||
.add(BigDecimalAdapter())
|
||||
.build(),
|
||||
)
|
||||
fun provideMoshi(): Moshi {
|
||||
return Moshi.Builder()
|
||||
.add(KotlinJsonAdapterFactory())
|
||||
.add(BigDecimalAdapter())
|
||||
.build()
|
||||
}
|
||||
|
||||
private companion object {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue