fix: okhttp url interceptor
This commit is contained in:
@@ -11,15 +11,23 @@ import okhttp3.OkHttpClient
|
|||||||
import okhttp3.logging.HttpLoggingInterceptor
|
import okhttp3.logging.HttpLoggingInterceptor
|
||||||
import retrofit2.Retrofit
|
import retrofit2.Retrofit
|
||||||
import retrofit2.converter.gson.GsonConverterFactory
|
import retrofit2.converter.gson.GsonConverterFactory
|
||||||
|
import timber.log.Timber
|
||||||
import java.util.concurrent.TimeUnit
|
import java.util.concurrent.TimeUnit
|
||||||
|
|
||||||
object LNbitsClient {
|
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
|
// Single shared client — used by both Retrofit and WebSocket
|
||||||
val httpClient: OkHttpClient = OkHttpClient.Builder()
|
val httpClient: OkHttpClient = OkHttpClient.Builder()
|
||||||
.connectTimeout(WalletConstants.CONNECT_TIMEOUT_S, TimeUnit.SECONDS)
|
.connectTimeout(WalletConstants.CONNECT_TIMEOUT_S, TimeUnit.SECONDS)
|
||||||
.readTimeout(WalletConstants.READ_TIMEOUT_S, TimeUnit.SECONDS)
|
.readTimeout(WalletConstants.READ_TIMEOUT_S, TimeUnit.SECONDS)
|
||||||
.writeTimeout(WalletConstants.WRITE_TIMEOUT_S, TimeUnit.SECONDS)
|
.writeTimeout(WalletConstants.WRITE_TIMEOUT_S, TimeUnit.SECONDS)
|
||||||
|
.addInterceptor(logging)
|
||||||
.apply {
|
.apply {
|
||||||
if (BuildConfig.DEBUG) {
|
if (BuildConfig.DEBUG) {
|
||||||
addInterceptor(HttpLoggingInterceptor().apply {
|
addInterceptor(HttpLoggingInterceptor().apply {
|
||||||
@@ -30,27 +38,25 @@ object LNbitsClient {
|
|||||||
.build()
|
.build()
|
||||||
|
|
||||||
fun create(secrets: SecretStore): LNbitsApi {
|
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
|
// Dynamic base URL interceptor — rewrites the placeholder host to the
|
||||||
// real base URL at request time, not at Retrofit construction time.
|
// real base URL at request time, not at Retrofit construction time.
|
||||||
// This means credentials only need to be in DataStore by the time the
|
// This means credentials only need to be in DataStore by the time the
|
||||||
// first HTTP request fires, not at app startup.
|
// first HTTP request fires, not at app startup.
|
||||||
val dynamicBaseUrlInterceptor = Interceptor { chain ->
|
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() }
|
val realBaseUrl = runBlocking { secrets.baseUrl() }
|
||||||
.ifBlank { "https://placeholder.invalid" }
|
.ifBlank { "https://placeholder.invalid" }
|
||||||
.trimEnd('/')
|
.trimEnd('/')
|
||||||
val realHttpUrl = realBaseUrl.toHttpUrl()
|
val realHttpUrl = realBaseUrl.toHttpUrl()
|
||||||
|
|
||||||
val originalUrl = chain.request().url
|
|
||||||
val newUrl = originalUrl.newBuilder()
|
val newUrl = originalUrl.newBuilder()
|
||||||
.scheme(realHttpUrl.scheme)
|
.scheme(realHttpUrl.scheme)
|
||||||
.host(realHttpUrl.host)
|
.host(realHttpUrl.host)
|
||||||
@@ -58,7 +64,7 @@ object LNbitsClient {
|
|||||||
.build()
|
.build()
|
||||||
|
|
||||||
chain.proceed(
|
chain.proceed(
|
||||||
chain.request().newBuilder()
|
originalRequest.newBuilder()
|
||||||
.url(newUrl)
|
.url(newUrl)
|
||||||
.build()
|
.build()
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user