fix: okhttp url interceptor
This commit is contained in:
@@ -11,15 +11,23 @@ import okhttp3.OkHttpClient
|
||||
import okhttp3.logging.HttpLoggingInterceptor
|
||||
import retrofit2.Retrofit
|
||||
import retrofit2.converter.gson.GsonConverterFactory
|
||||
import timber.log.Timber
|
||||
import java.util.concurrent.TimeUnit
|
||||
|
||||
object LNbitsClient {
|
||||
|
||||
val logging = HttpLoggingInterceptor { message ->
|
||||
Timber.tag("OkHttp").d(message)
|
||||
}.apply {
|
||||
level = HttpLoggingInterceptor.Level.BODY // logs full request + response body
|
||||
}
|
||||
|
||||
// Single shared client — used by both Retrofit and WebSocket
|
||||
val httpClient: OkHttpClient = OkHttpClient.Builder()
|
||||
.connectTimeout(WalletConstants.CONNECT_TIMEOUT_S, TimeUnit.SECONDS)
|
||||
.readTimeout(WalletConstants.READ_TIMEOUT_S, TimeUnit.SECONDS)
|
||||
.writeTimeout(WalletConstants.WRITE_TIMEOUT_S, TimeUnit.SECONDS)
|
||||
.addInterceptor(logging)
|
||||
.apply {
|
||||
if (BuildConfig.DEBUG) {
|
||||
addInterceptor(HttpLoggingInterceptor().apply {
|
||||
@@ -30,27 +38,25 @@ object LNbitsClient {
|
||||
.build()
|
||||
|
||||
fun create(secrets: SecretStore): LNbitsApi {
|
||||
// Auth header interceptor — reads invoice key per-request (unchanged)
|
||||
// val authInterceptor = Interceptor { chain ->
|
||||
// val key = runBlocking { secrets.invoiceKey() }
|
||||
// chain.proceed(
|
||||
// chain.request().newBuilder()
|
||||
// .header("X-Api-Key", key)
|
||||
// .build()
|
||||
// )
|
||||
// }
|
||||
|
||||
// Dynamic base URL interceptor — rewrites the placeholder host to the
|
||||
// real base URL at request time, not at Retrofit construction time.
|
||||
// This means credentials only need to be in DataStore by the time the
|
||||
// first HTTP request fires, not at app startup.
|
||||
val dynamicBaseUrlInterceptor = Interceptor { chain ->
|
||||
val originalRequest = chain.request()
|
||||
val originalUrl = originalRequest.url
|
||||
|
||||
// Only rewrite requests targeting the Retrofit placeholder host.
|
||||
// External LNURL/Lightning Address requests must pass through unchanged.
|
||||
if (originalUrl.host != "placeholder.invalid") {
|
||||
return@Interceptor chain.proceed(originalRequest)
|
||||
}
|
||||
|
||||
val realBaseUrl = runBlocking { secrets.baseUrl() }
|
||||
.ifBlank { "https://placeholder.invalid" }
|
||||
.trimEnd('/')
|
||||
val realHttpUrl = realBaseUrl.toHttpUrl()
|
||||
|
||||
val originalUrl = chain.request().url
|
||||
val newUrl = originalUrl.newBuilder()
|
||||
.scheme(realHttpUrl.scheme)
|
||||
.host(realHttpUrl.host)
|
||||
@@ -58,7 +64,7 @@ object LNbitsClient {
|
||||
.build()
|
||||
|
||||
chain.proceed(
|
||||
chain.request().newBuilder()
|
||||
originalRequest.newBuilder()
|
||||
.url(newUrl)
|
||||
.build()
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user