Updated on 2026-08-14
This commit is contained in:
parent
122d654f29
commit
54d8ce26bf
4 changed files with 7 additions and 32 deletions
|
|
@ -4,10 +4,8 @@ import com.squareup.moshi.Moshi
|
|||
import com.tangem.datasource.api.paymentology.PaymentologyApi
|
||||
import com.tangem.datasource.api.promotion.PromotionApi
|
||||
import com.tangem.datasource.api.tangemTech.TangemTechApi
|
||||
import com.tangem.datasource.utils.LazyRequestHeader
|
||||
import com.tangem.datasource.utils.RequestHeader.*
|
||||
import com.tangem.datasource.utils.addHeaders
|
||||
import com.tangem.datasource.utils.addLazyHeaders
|
||||
import com.tangem.datasource.utils.allowLogging
|
||||
import com.tangem.lib.auth.AuthProvider
|
||||
import com.tangem.lib.auth.BuildConfig
|
||||
|
|
@ -65,8 +63,8 @@ class NetworkModule {
|
|||
@PromotionOneInch
|
||||
fun providePromotionOneInchApi(authProvider: AuthProvider, @NetworkMoshi moshi: Moshi): PromotionApi {
|
||||
val okClient = OkHttpClient.Builder()
|
||||
.addLazyHeaders(
|
||||
LazyRequestHeader.LazyAuthenticationHeader(authProvider),
|
||||
.addHeaders(
|
||||
AuthenticationHeader(authProvider),
|
||||
)
|
||||
.allowLogging()
|
||||
.callTimeout(API_ONE_INCH_TIMEOUT_MS, TimeUnit.MILLISECONDS)
|
||||
|
|
|
|||
|
|
@ -13,20 +13,6 @@ internal fun OkHttpClient.Builder.addHeaders(vararg requestHeaders: RequestHeade
|
|||
val request = chain.request().newBuilder().apply {
|
||||
requestHeaders
|
||||
.flatMap(RequestHeader::values)
|
||||
.forEach { addHeader(it.first, it.second) }
|
||||
}.build()
|
||||
|
||||
chain.proceed(request)
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
internal fun OkHttpClient.Builder.addLazyHeaders(vararg requestHeaders: LazyRequestHeader): OkHttpClient.Builder {
|
||||
return addInterceptor(
|
||||
Interceptor { chain ->
|
||||
val request = chain.request().newBuilder().apply {
|
||||
requestHeaders
|
||||
.flatMap(LazyRequestHeader::values)
|
||||
.forEach { addHeader(it.first, it.second.invoke()) }
|
||||
}.build()
|
||||
|
||||
|
|
|
|||
|
|
@ -7,24 +7,14 @@ import com.tangem.lib.auth.AuthProvider
|
|||
*
|
||||
* @param pairs header name and header value pairs
|
||||
*/
|
||||
sealed class RequestHeader(vararg pairs: Pair<String, String>) {
|
||||
sealed class RequestHeader(vararg pairs: Pair<String, () -> String>) {
|
||||
|
||||
/** Header list */
|
||||
val values: List<Pair<String, String>> = pairs.toList()
|
||||
|
||||
object CacheControlHeader : RequestHeader("Cache-Control" to "max-age=600")
|
||||
|
||||
class AuthenticationHeader(authProvider: AuthProvider) : RequestHeader(
|
||||
"card_id" to authProvider.getCardId(),
|
||||
"card_public_key" to authProvider.getCardPublicKey(),
|
||||
)
|
||||
}
|
||||
|
||||
sealed class LazyRequestHeader(vararg pairs: Pair<String, () -> String>) {
|
||||
|
||||
val values: List<Pair<String, () -> String>> = pairs.toList()
|
||||
|
||||
class LazyAuthenticationHeader(authProvider: AuthProvider) : LazyRequestHeader(
|
||||
object CacheControlHeader : RequestHeader("Cache-Control" to { "max-age=600" })
|
||||
|
||||
class AuthenticationHeader(authProvider: AuthProvider) : RequestHeader(
|
||||
"card_id" to { authProvider.getCardId() },
|
||||
"card_public_key" to { authProvider.getCardPublicKey() },
|
||||
)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue