Updated on 2026-08-14
This commit is contained in:
parent
aed32a47f1
commit
58e26ca5e6
8 changed files with 139 additions and 14 deletions
|
|
@ -18,6 +18,7 @@ import com.tangem.datasource.api.utils.WriteTimeout
|
|||
import com.tangem.datasource.di.NetworkMoshi
|
||||
import com.tangem.datasource.local.logs.AppLogsStore
|
||||
import com.tangem.datasource.utils.NetworkLogsSaveInterceptor
|
||||
import com.tangem.datasource.utils.WireMockRedirectInterceptor
|
||||
import com.tangem.datasource.utils.addHeaders
|
||||
import dagger.hilt.android.qualifiers.ApplicationContext
|
||||
import okhttp3.Interceptor
|
||||
|
|
@ -77,6 +78,7 @@ internal class RetrofitApiBuilder @Inject constructor(
|
|||
.client(
|
||||
OkHttpClient.Builder()
|
||||
.applyApiConfig(apiConfigId = apiConfigId, environmentConfig = environmentConfig)
|
||||
.applyWireMockRedirect()
|
||||
.let {
|
||||
if (applyTimeoutAnnotations) it.applyTimeoutAnnotations() else it
|
||||
}
|
||||
|
|
@ -181,6 +183,14 @@ internal class RetrofitApiBuilder @Inject constructor(
|
|||
)
|
||||
}
|
||||
|
||||
private fun OkHttpClient.Builder.applyWireMockRedirect(): OkHttpClient.Builder {
|
||||
return if (BuildConfig.MOCK_DATA_SOURCE) {
|
||||
addInterceptor(interceptor = WireMockRedirectInterceptor())
|
||||
} else {
|
||||
this
|
||||
}
|
||||
}
|
||||
|
||||
private fun OkHttpClient.Builder.addLoggers(apiConfigId: ApiConfig.ID, context: Context): OkHttpClient.Builder {
|
||||
if (apiConfigId in excludedApiForLogging) return this
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,40 @@
|
|||
package com.tangem.datasource.utils
|
||||
|
||||
import okhttp3.Interceptor
|
||||
import okhttp3.Response
|
||||
import timber.log.Timber
|
||||
|
||||
/**
|
||||
* OkHttp interceptor that redirects requests from wiremock.tests-d.com to a local WireMock instance.
|
||||
*/
|
||||
class WireMockRedirectInterceptor : Interceptor {
|
||||
|
||||
override fun intercept(chain: Interceptor.Chain): Response {
|
||||
val override = overriddenBaseUrl ?: return chain.proceed(chain.request())
|
||||
|
||||
val request = chain.request()
|
||||
val url = request.url.toString()
|
||||
|
||||
if (url.contains(WIREMOCK_REMOTE_URL)) {
|
||||
val newUrl = url.replace(WIREMOCK_REMOTE_URL, override.trimEnd('/'))
|
||||
Timber.d("WireMockRedirect: $url -> $newUrl")
|
||||
val newRequest = request.newBuilder()
|
||||
.url(newUrl)
|
||||
.build()
|
||||
return chain.proceed(newRequest)
|
||||
}
|
||||
|
||||
return chain.proceed(request)
|
||||
}
|
||||
|
||||
companion object {
|
||||
private const val WIREMOCK_REMOTE_URL = "[REDACTED_ENV_URL]"
|
||||
|
||||
/**
|
||||
* Override base URL for WireMock requests.
|
||||
* When null (default), requests go to wiremock.tests-d.com.
|
||||
* When set (e.g., "http://localhost:8080"), requests are redirected to local WireMock instance.
|
||||
*/
|
||||
var overriddenBaseUrl: String? = null
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue