refactor eternal apis

This commit is contained in:
Bnyro 2022-09-08 19:29:35 +02:00
parent ea5bdef9fc
commit 5041fd9fdd
9 changed files with 95 additions and 95 deletions

View File

@ -15,19 +15,6 @@
"versionName": "0.5.1",
"outputFile": "app-universal-release.apk"
},
{
"type": "ONE_OF_MANY",
"filters": [
{
"filterType": "ABI",
"value": "x86_64"
}
],
"attributes": [],
"versionCode": 18,
"versionName": "0.5.1",
"outputFile": "app-x86_64-release.apk"
},
{
"type": "ONE_OF_MANY",
"filters": [
@ -41,19 +28,6 @@
"versionName": "0.5.1",
"outputFile": "app-armeabi-v7a-release.apk"
},
{
"type": "ONE_OF_MANY",
"filters": [
{
"filterType": "ABI",
"value": "x86"
}
],
"attributes": [],
"versionCode": 18,
"versionName": "0.5.1",
"outputFile": "app-x86-release.apk"
},
{
"type": "ONE_OF_MANY",
"filters": [
@ -66,6 +40,32 @@
"versionCode": 18,
"versionName": "0.5.1",
"outputFile": "app-arm64-v8a-release.apk"
},
{
"type": "ONE_OF_MANY",
"filters": [
{
"filterType": "ABI",
"value": "x86_64"
}
],
"attributes": [],
"versionCode": 18,
"versionName": "0.5.1",
"outputFile": "app-x86_64-release.apk"
},
{
"type": "ONE_OF_MANY",
"filters": [
{
"filterType": "ABI",
"value": "x86"
}
],
"attributes": [],
"versionCode": 18,
"versionName": "0.5.1",
"outputFile": "app-x86-release.apk"
}
],
"elementType": "File"

View File

@ -16,9 +16,7 @@ import com.github.libretube.api.CronetHelper
import com.github.libretube.api.RetrofitInstance
import com.github.libretube.constants.BACKGROUND_CHANNEL_ID
import com.github.libretube.constants.DOWNLOAD_CHANNEL_ID
import com.github.libretube.constants.PIPED_API_URL
import com.github.libretube.constants.PUSH_CHANNEL_ID
import com.github.libretube.constants.PreferenceKeys
import com.github.libretube.db.DatabaseHolder
import com.github.libretube.db.obj.WatchHistoryItem
import com.github.libretube.db.obj.WatchPosition
@ -55,7 +53,9 @@ class MyApp : Application() {
/**
* Set the api and the auth api url
*/
initializeRetrofit()
RetrofitInstance.initialize()
CronetHelper.initCronet(this)
ImageHelper.initializeImageLoader(this)
/**
* Initialize the notification listener in the background
@ -82,26 +82,6 @@ class MyApp : Application() {
databaseMigration()
}
/**
* Set the api urls needed for the [RetrofitInstance]
*/
private fun initializeRetrofit() {
RetrofitInstance.url =
PreferenceHelper.getString(PreferenceKeys.FETCH_INSTANCE, PIPED_API_URL)
// set auth instance
RetrofitInstance.authUrl =
if (PreferenceHelper.getBoolean(PreferenceKeys.AUTH_INSTANCE_TOGGLE, false)) {
PreferenceHelper.getString(
PreferenceKeys.AUTH_INSTANCE,
PIPED_API_URL
)
} else {
RetrofitInstance.url
}
CronetHelper.initCronet(this)
ImageHelper.initializeImageLoader(this)
}
/**
* Initializes the required [NotificationChannel]s for the app.
*/

View File

@ -0,0 +1,17 @@
package com.github.libretube.api
import com.github.libretube.constants.GITHUB_API_URL
import com.github.libretube.constants.PIPED_INSTANCES_URL
import com.github.libretube.obj.Instances
import com.github.libretube.update.UpdateInfo
import retrofit2.http.GET
interface ExternalApi {
// only for fetching servers list
@GET(PIPED_INSTANCES_URL)
suspend fun getInstances(): List<Instances>
// fetch latest version info
@GET(GITHUB_API_URL)
suspend fun getUpdateInfo(): UpdateInfo
}

View File

@ -3,7 +3,6 @@ package com.github.libretube.api
import com.github.libretube.obj.Channel
import com.github.libretube.obj.CommentsPage
import com.github.libretube.obj.DeleteUserRequest
import com.github.libretube.obj.Instances
import com.github.libretube.obj.Login
import com.github.libretube.obj.Message
import com.github.libretube.obj.Playlist
@ -23,7 +22,6 @@ import retrofit2.http.Header
import retrofit2.http.POST
import retrofit2.http.Path
import retrofit2.http.Query
import retrofit2.http.Url
interface PipedApi {
@GET("trending")
@ -165,8 +163,4 @@ interface PipedApi {
@Header("Authorization") token: String,
@Body playlistId: PlaylistId
): Message
// only for fetching servers list
@GET
suspend fun getInstances(@Url url: String): List<Instances>
}

View File

@ -1,5 +1,8 @@
package com.github.libretube.api
import com.github.libretube.constants.PIPED_API_URL
import com.github.libretube.constants.PreferenceKeys
import com.github.libretube.util.PreferenceHelper
import retrofit2.Retrofit
import retrofit2.converter.jackson.JacksonConverterFactory
@ -7,20 +10,55 @@ object RetrofitInstance {
lateinit var url: String
lateinit var authUrl: String
val lazyMgr = resettableManager()
val jacksonConverterFactory = JacksonConverterFactory.create()
val api: PipedApi by resettableLazy(lazyMgr) {
Retrofit.Builder()
.baseUrl(url)
.callFactory(CronetHelper.callFactory)
.addConverterFactory(JacksonConverterFactory.create())
.addConverterFactory(jacksonConverterFactory)
.build()
.create(PipedApi::class.java)
}
val authApi: PipedApi by resettableLazy(lazyMgr) {
Retrofit.Builder()
.baseUrl(authUrl)
.callFactory(CronetHelper.callFactory)
.addConverterFactory(JacksonConverterFactory.create())
.addConverterFactory(jacksonConverterFactory)
.build()
.create(PipedApi::class.java)
}
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
}
}
}

View File

@ -34,6 +34,7 @@ const val YOUTUBE_FRONTEND_URL = "https://www.youtube.com"
* Retrofit Instance
*/
const val PIPED_API_URL = "https://pipedapi.kavin.rocks/"
const val PIPED_INSTANCES_URL = "https://piped-instances.kavin.rocks/"
/**
* Notification IDs

View File

@ -179,7 +179,7 @@ class InstanceSettings : MaterialPreferenceFragment() {
// fetch official public instances
val response = try {
RetrofitInstance.api.getInstances("https://instances.tokhmi.xyz/")
RetrofitInstance.externalApi.getInstances()
} catch (e: Exception) {
e.printStackTrace()
emptyList()

View File

@ -6,9 +6,9 @@ import androidx.preference.Preference
import com.github.libretube.BuildConfig
import com.github.libretube.R
import com.github.libretube.activities.SettingsActivity
import com.github.libretube.api.RetrofitInstance
import com.github.libretube.dialogs.UpdateDialog
import com.github.libretube.extensions.getStyledSnackBar
import com.github.libretube.update.UpdateChecker
import com.github.libretube.util.NetworkHelper
import com.github.libretube.views.MaterialPreferenceFragment
import kotlinx.coroutines.CoroutineScope
@ -102,8 +102,12 @@ class MainSettings : MaterialPreferenceFragment() {
return@launch
}
// check for update
val updateInfo = UpdateChecker.getLatestReleaseInfo()
if (updateInfo?.name == null) {
val updateInfo = try {
RetrofitInstance.externalApi.getUpdateInfo()
} catch (e: Exception) {
return@launch
}
if (updateInfo.name == null) {
// request failed
(activity as? SettingsActivity)?.binding?.root?.getStyledSnackBar(R.string.unknown_error)
?.show()

View File

@ -1,34 +0,0 @@
package com.github.libretube.update
import com.fasterxml.jackson.databind.ObjectMapper
import com.github.libretube.constants.GITHUB_API_URL
import com.github.libretube.extensions.await
import java.net.URL
object UpdateChecker {
fun getLatestReleaseInfo(): UpdateInfo? {
var versionInfo: UpdateInfo? = null
// run http request as thread to make it async
Thread {
// otherwise crashes without internet
versionInfo = getUpdateInfo()
try {
versionInfo = getUpdateInfo()
} catch (e: Exception) {
}
}.await()
// return the information about the latest version
return versionInfo
}
private fun getUpdateInfo(): UpdateInfo? {
// get the github API response
val latestVersionApiUrl = URL(GITHUB_API_URL)
val json = latestVersionApiUrl.readText()
// Parse and return the json data
val mapper = ObjectMapper()
return mapper.readValue(json, UpdateInfo::class.java)
}
}