LibreTube/app/src/main/java/com/github/libretube/api/RetrofitInstance.kt

65 lines
1.9 KiB
Kotlin
Raw Normal View History

2022-08-14 13:25:28 +05:30
package com.github.libretube.api
2021-12-18 16:34:14 +05:30
2022-09-08 22:59:35 +05:30
import com.github.libretube.constants.PIPED_API_URL
import com.github.libretube.constants.PreferenceKeys
import com.github.libretube.util.PreferenceHelper
2021-12-18 16:34:14 +05:30
import retrofit2.Retrofit
import retrofit2.converter.jackson.JacksonConverterFactory
object RetrofitInstance {
2022-02-08 19:57:13 +05:30
lateinit var url: String
lateinit var authUrl: String
2022-02-09 23:40:39 +05:30
val lazyMgr = resettableManager()
2022-09-08 22:59:35 +05:30
val jacksonConverterFactory = JacksonConverterFactory.create()
2022-02-09 23:40:39 +05:30
val api: PipedApi by resettableLazy(lazyMgr) {
2021-12-18 16:34:14 +05:30
Retrofit.Builder()
2022-02-08 19:57:13 +05:30
.baseUrl(url)
2022-07-21 23:28:38 +05:30
.callFactory(CronetHelper.callFactory)
2022-09-08 22:59:35 +05:30
.addConverterFactory(jacksonConverterFactory)
2021-12-18 16:34:14 +05:30
.build()
.create(PipedApi::class.java)
}
2022-09-08 22:59:35 +05:30
val authApi: PipedApi by resettableLazy(lazyMgr) {
Retrofit.Builder()
.baseUrl(authUrl)
2022-07-21 23:28:38 +05:30
.callFactory(CronetHelper.callFactory)
2022-09-08 22:59:35 +05:30
.addConverterFactory(jacksonConverterFactory)
.build()
.create(PipedApi::class.java)
}
2022-09-08 22:59:35 +05:30
val externalApi: ExternalApi by resettableLazy(lazyMgr) {
Retrofit.Builder()
.baseUrl(url)
.callFactory(CronetHelper.callFactory)
.addConverterFactory(jacksonConverterFactory)
.build()
.create(ExternalApi::class.java)
}
/**
* Set the api urls needed for the [RetrofitInstance]
*/
fun initialize() {
url =
PreferenceHelper.getString(PreferenceKeys.FETCH_INSTANCE, PIPED_API_URL)
// set auth instance
authUrl =
if (
PreferenceHelper.getBoolean(
PreferenceKeys.AUTH_INSTANCE_TOGGLE,
false
)
) {
PreferenceHelper.getString(
PreferenceKeys.AUTH_INSTANCE,
PIPED_API_URL
)
} else {
url
}
}
2022-05-20 03:52:10 +05:30
}