Add a debug constant to the open subtitles API

This commit is contained in:
Nicolas Pomepuy 2024-11-07 09:30:23 +01:00 committed by Duncan McNamara
parent cb8bd4b48a
commit 1879a9bcf7

View File

@ -5,7 +5,6 @@ import com.moczul.ok2curl.CurlInterceptor
import com.moczul.ok2curl.logger.Logger import com.moczul.ok2curl.logger.Logger
import com.squareup.moshi.Moshi import com.squareup.moshi.Moshi
import com.squareup.moshi.adapters.Rfc3339DateJsonAdapter import com.squareup.moshi.adapters.Rfc3339DateJsonAdapter
import main.java.org.videolan.resources.opensubtitles.OpenSubtitlesUtils
import okhttp3.HttpUrl.Companion.toHttpUrlOrNull import okhttp3.HttpUrl.Companion.toHttpUrlOrNull
import okhttp3.Interceptor import okhttp3.Interceptor
import okhttp3.OkHttpClient import okhttp3.OkHttpClient
@ -23,25 +22,12 @@ import java.util.concurrent.TimeUnit
private const val BASE_URL = "https://api.opensubtitles.com/api/v1/" private const val BASE_URL = "https://api.opensubtitles.com/api/v1/"
const val USER_AGENT = "VLSub v0.9" const val USER_AGENT = "VLSub v0.9"
private const val DEBUG = false
private fun buildClient() = Retrofit.Builder() private fun buildClient() = Retrofit.Builder()
.baseUrl(BASE_URL) .baseUrl(BASE_URL)
.client( .client(
OkHttpClient.Builder() getOkHttpClient()
.addInterceptor(HttpLoggingInterceptor().apply {
level = HttpLoggingInterceptor.Level.BODY
})
.addInterceptor(DomainInterceptor())
.addInterceptor(UserAgentInterceptor(USER_AGENT))
.addInterceptor(ConnectivityInterceptor(AppContextProvider.appContext))
.addInterceptor(CurlInterceptor(object : Logger {
override fun log(message: String) {
Log.v("Ok2Curl", message)
}
}))
.readTimeout(10, TimeUnit.SECONDS)
.connectTimeout(5, TimeUnit.SECONDS)
.build()
) )
.addConverterFactory( .addConverterFactory(
MoshiConverterFactory.create( MoshiConverterFactory.create(
@ -53,6 +39,30 @@ private fun buildClient() = Retrofit.Builder()
.build() .build()
.create(IOpenSubtitleService::class.java) .create(IOpenSubtitleService::class.java)
private fun getOkHttpClient(): OkHttpClient {
val builder = OkHttpClient.Builder()
.addInterceptor(DomainInterceptor())
.addInterceptor(UserAgentInterceptor(USER_AGENT))
.addInterceptor(ConnectivityInterceptor(AppContextProvider.appContext))
.readTimeout(10, TimeUnit.SECONDS)
.connectTimeout(5, TimeUnit.SECONDS)
if (DEBUG) {
builder
.addInterceptor(HttpLoggingInterceptor().apply {
level = HttpLoggingInterceptor.Level.BODY
})
.addInterceptor(CurlInterceptor(object : Logger {
override fun log(message: String) {
Log.v("Ok2Curl", message)
}
}))
}
return builder.build()
}
private class UserAgentInterceptor(val userAgent: String): Interceptor { private class UserAgentInterceptor(val userAgent: String): Interceptor {
override fun intercept(chain: Interceptor.Chain): Response { override fun intercept(chain: Interceptor.Chain): Response {