mirror of
https://github.com/NiciDieNase/chaosflix
synced 2024-11-23 04:43:07 +00:00
refactor apiFactory to use stageConfig directly
This commit is contained in:
parent
d592055b5d
commit
c3e7c29b07
1 changed files with 24 additions and 28 deletions
|
@ -4,7 +4,6 @@ import android.os.Build
|
||||||
import com.google.gson.Gson
|
import com.google.gson.Gson
|
||||||
import de.nicidienase.chaosflix.StageConfiguration
|
import de.nicidienase.chaosflix.StageConfiguration
|
||||||
import de.nicidienase.chaosflix.common.SingletonHolder
|
import de.nicidienase.chaosflix.common.SingletonHolder
|
||||||
import java.io.File
|
|
||||||
import java.util.concurrent.TimeUnit
|
import java.util.concurrent.TimeUnit
|
||||||
import okhttp3.Cache
|
import okhttp3.Cache
|
||||||
import okhttp3.Interceptor
|
import okhttp3.Interceptor
|
||||||
|
@ -14,48 +13,45 @@ import org.koin.core.get
|
||||||
import retrofit2.Retrofit
|
import retrofit2.Retrofit
|
||||||
import retrofit2.converter.gson.GsonConverterFactory
|
import retrofit2.converter.gson.GsonConverterFactory
|
||||||
|
|
||||||
class ApiFactory(stageConfiguration: StageConfiguration) {
|
class ApiFactory(private val stageConfiguration: StageConfiguration) {
|
||||||
|
|
||||||
private val apiUrl: String = stageConfiguration.recordingUrl
|
|
||||||
private val eventInfoUrl: String = stageConfiguration.eventInfoUrl
|
|
||||||
private val cache: File? = stageConfiguration.cacheDir
|
|
||||||
|
|
||||||
private val chaosflixUserAgent: String by lazy { buildUserAgent() }
|
private val chaosflixUserAgent: String by lazy { buildUserAgent() }
|
||||||
private val gsonConverterFactory: GsonConverterFactory by lazy { GsonConverterFactory.create(Gson()) }
|
private val gsonConverterFactory: GsonConverterFactory by lazy { GsonConverterFactory.create(Gson()) }
|
||||||
|
|
||||||
val client: OkHttpClient by lazy {
|
val client: OkHttpClient by lazy {
|
||||||
OkHttpClient.Builder()
|
OkHttpClient.Builder()
|
||||||
.connectTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS)
|
.connectTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS)
|
||||||
.readTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS)
|
.readTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS)
|
||||||
.addInterceptor(useragentInterceptor)
|
.addInterceptor(useragentInterceptor)
|
||||||
.apply {
|
.apply {
|
||||||
if (cache != null) {
|
if (stageConfiguration.cacheDir != null) {
|
||||||
cache(Cache(cache, CACHE_SIZE))
|
cache(Cache(stageConfiguration.cacheDir, CACHE_SIZE))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
.build()
|
||||||
.build()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
val recordingApi: RecordingApi by lazy {
|
val recordingApi: RecordingApi by lazy {
|
||||||
Retrofit.Builder()
|
Retrofit.Builder()
|
||||||
.baseUrl(apiUrl)
|
.baseUrl(stageConfiguration.recordingUrl)
|
||||||
.client(client)
|
.client(client)
|
||||||
.addConverterFactory(gsonConverterFactory)
|
.addConverterFactory(gsonConverterFactory)
|
||||||
.build()
|
.build()
|
||||||
.create(RecordingApi::class.java)
|
.create(RecordingApi::class.java)
|
||||||
}
|
}
|
||||||
|
|
||||||
val streamingApi: StreamingApi by lazy {
|
val streamingApi: StreamingApi by lazy {
|
||||||
Retrofit.Builder()
|
Retrofit.Builder()
|
||||||
.baseUrl(stageConfiguration.streamingApiBaseUrl)
|
.baseUrl(stageConfiguration.streamingApiBaseUrl)
|
||||||
.client(client)
|
.client(client)
|
||||||
.addConverterFactory(gsonConverterFactory)
|
.addConverterFactory(gsonConverterFactory)
|
||||||
.build()
|
.build()
|
||||||
.create(StreamingApi::class.java) }
|
.create(StreamingApi::class.java)
|
||||||
|
}
|
||||||
|
|
||||||
val eventInfoApi: EventInfoApi by lazy {
|
val eventInfoApi: EventInfoApi by lazy {
|
||||||
Retrofit.Builder()
|
Retrofit.Builder()
|
||||||
.baseUrl(eventInfoUrl)
|
.baseUrl(stageConfiguration.eventInfoUrl)
|
||||||
.client(client)
|
.client(client)
|
||||||
.addConverterFactory(gsonConverterFactory)
|
.addConverterFactory(gsonConverterFactory)
|
||||||
.build()
|
.build()
|
||||||
|
@ -64,9 +60,9 @@ class ApiFactory(stageConfiguration: StageConfiguration) {
|
||||||
|
|
||||||
private val useragentInterceptor: Interceptor = Interceptor { chain ->
|
private val useragentInterceptor: Interceptor = Interceptor { chain ->
|
||||||
val requestWithUseragent = chain.request().newBuilder()
|
val requestWithUseragent = chain.request().newBuilder()
|
||||||
.header("User-Agent", chaosflixUserAgent)
|
.header("User-Agent", chaosflixUserAgent)
|
||||||
.build()
|
.build()
|
||||||
return@Interceptor chain.proceed(requestWithUseragent)
|
return@Interceptor chain.proceed(requestWithUseragent)
|
||||||
}
|
}
|
||||||
|
|
||||||
companion object : SingletonHolder<ApiFactory, StageConfiguration>(::ApiFactory), KoinComponent {
|
companion object : SingletonHolder<ApiFactory, StageConfiguration>(::ApiFactory), KoinComponent {
|
||||||
|
|
Loading…
Reference in a new issue