Have separate retrofit instance for authenticated calls.

This commit is contained in:
Kavin 2022-07-02 19:55:22 +01:00
parent 284eceeba3
commit dc6f4cefb5
No known key found for this signature in database
GPG Key ID: 49451E4482CC5BCD
15 changed files with 32 additions and 21 deletions

View File

@ -61,6 +61,8 @@ class MainActivity : AppCompatActivity() {
RetrofitInstance.url = RetrofitInstance.url =
PreferenceHelper.getString(this, "selectInstance", "https://pipedapi.kavin.rocks/")!! PreferenceHelper.getString(this, "selectInstance", "https://pipedapi.kavin.rocks/")!!
RetrofitInstance.authUrl =
PreferenceHelper.getString(this, "selectAuthInstance", "https://pipedapi.kavin.rocks/")!!
ThemeHelper.updateTheme(this) ThemeHelper.updateTheme(this)
LocaleHelper.updateLanguage(this) LocaleHelper.updateLanguage(this)

View File

@ -91,7 +91,7 @@ class PlaylistAdapter(
fun run() { fun run() {
CoroutineScope(Dispatchers.IO).launch { CoroutineScope(Dispatchers.IO).launch {
val response = try { val response = try {
RetrofitInstance.api.removeFromPlaylist( RetrofitInstance.authApi.removeFromPlaylist(
token, token,
PlaylistId(playlistId = playlistId, index = position) PlaylistId(playlistId = playlistId, index = position)
) )

View File

@ -76,7 +76,7 @@ class PlaylistsAdapter(
fun run() { fun run() {
GlobalScope.launch { GlobalScope.launch {
val response = try { val response = try {
RetrofitInstance.api.deletePlaylist(token, PlaylistId(id)) RetrofitInstance.authApi.deletePlaylist(token, PlaylistId(id))
} catch (e: IOException) { } catch (e: IOException) {
println(e) println(e)
Log.e(TAG, "IOException, you might not have internet connection") Log.e(TAG, "IOException, you might not have internet connection")

View File

@ -70,7 +70,7 @@ class SubscriptionChannelAdapter(private val subscriptions: MutableList<Subscrip
CoroutineScope(Dispatchers.IO).launch { CoroutineScope(Dispatchers.IO).launch {
val response = try { val response = try {
val token = PreferenceHelper.getToken(context) val token = PreferenceHelper.getToken(context)
RetrofitInstance.api.subscribe( RetrofitInstance.authApi.subscribe(
token, token,
Subscribe(channelId) Subscribe(channelId)
) )
@ -92,7 +92,7 @@ class SubscriptionChannelAdapter(private val subscriptions: MutableList<Subscrip
CoroutineScope(Dispatchers.IO).launch { CoroutineScope(Dispatchers.IO).launch {
val response = try { val response = try {
val token = PreferenceHelper.getToken(context) val token = PreferenceHelper.getToken(context)
RetrofitInstance.api.unsubscribe( RetrofitInstance.authApi.unsubscribe(
token, token,
Subscribe(channelId) Subscribe(channelId)
) )

View File

@ -55,7 +55,7 @@ class AddtoPlaylistDialog : DialogFragment() {
fun run() { fun run() {
lifecycleScope.launchWhenCreated { lifecycleScope.launchWhenCreated {
val response = try { val response = try {
RetrofitInstance.api.playlists(token) RetrofitInstance.authApi.playlists(token)
} catch (e: IOException) { } catch (e: IOException) {
println(e) println(e)
Log.e(TAG, "IOException, you might not have internet connection") Log.e(TAG, "IOException, you might not have internet connection")
@ -95,7 +95,7 @@ class AddtoPlaylistDialog : DialogFragment() {
fun run() { fun run() {
lifecycleScope.launchWhenCreated { lifecycleScope.launchWhenCreated {
val response = try { val response = try {
RetrofitInstance.api.addToPlaylist(token, PlaylistId(playlistId, videoId)) RetrofitInstance.authApi.addToPlaylist(token, PlaylistId(playlistId, videoId))
} catch (e: IOException) { } catch (e: IOException) {
println(e) println(e)
Log.e(TAG, "IOException, you might not have internet connection") Log.e(TAG, "IOException, you might not have internet connection")

View File

@ -63,7 +63,7 @@ class CreatePlaylistDialog : DialogFragment() {
fun run() { fun run() {
lifecycleScope.launchWhenCreated { lifecycleScope.launchWhenCreated {
val response = try { val response = try {
RetrofitInstance.api.createPlaylist(token, Playlists(name = name)) RetrofitInstance.authApi.createPlaylist(token, Playlists(name = name))
} catch (e: IOException) { } catch (e: IOException) {
println(e) println(e)
Log.e(TAG, "IOException, you might not have internet connection") Log.e(TAG, "IOException, you might not have internet connection")

View File

@ -57,7 +57,7 @@ class DeleteAccountDialog : DialogFragment() {
val token = PreferenceHelper.getToken(requireContext()) val token = PreferenceHelper.getToken(requireContext())
try { try {
RetrofitInstance.api.deleteAccount(token, DeleteUserRequest(password)) RetrofitInstance.authApi.deleteAccount(token, DeleteUserRequest(password))
} catch (e: Exception) { } catch (e: Exception) {
Log.e(TAG, e.toString()) Log.e(TAG, e.toString())
Toast.makeText(context, R.string.unknown_error, Toast.LENGTH_SHORT).show() Toast.makeText(context, R.string.unknown_error, Toast.LENGTH_SHORT).show()

View File

@ -70,7 +70,7 @@ class LoginDialog : DialogFragment() {
fun run() { fun run() {
lifecycleScope.launchWhenCreated { lifecycleScope.launchWhenCreated {
val response = try { val response = try {
RetrofitInstance.api.login(login) RetrofitInstance.authApi.login(login)
} catch (e: IOException) { } catch (e: IOException) {
println(e) println(e)
Log.e(TAG, "IOException, you might not have internet connection") Log.e(TAG, "IOException, you might not have internet connection")
@ -102,7 +102,7 @@ class LoginDialog : DialogFragment() {
fun run() { fun run() {
lifecycleScope.launchWhenCreated { lifecycleScope.launchWhenCreated {
val response = try { val response = try {
RetrofitInstance.api.register(login) RetrofitInstance.authApi.register(login)
} catch (e: IOException) { } catch (e: IOException) {
println(e) println(e)
Log.e(TAG, "IOException, you might not have internet connection") Log.e(TAG, "IOException, you might not have internet connection")

View File

@ -70,7 +70,7 @@ class PlaylistOptionsDialog(
fun run() { fun run() {
CoroutineScope(Dispatchers.IO).launch { CoroutineScope(Dispatchers.IO).launch {
val response = try { val response = try {
RetrofitInstance.api.importPlaylist(token, PlaylistId(playlistId)) RetrofitInstance.authApi.importPlaylist(token, PlaylistId(playlistId))
} catch (e: IOException) { } catch (e: IOException) {
println(e) println(e)
return@launch return@launch

View File

@ -87,7 +87,7 @@ class ChannelFragment : Fragment() {
lifecycleScope.launchWhenCreated { lifecycleScope.launchWhenCreated {
val response = try { val response = try {
val token = PreferenceHelper.getToken(requireContext()) val token = PreferenceHelper.getToken(requireContext())
RetrofitInstance.api.isSubscribed( RetrofitInstance.authApi.isSubscribed(
channelId!!, channelId!!,
token token
) )
@ -127,7 +127,7 @@ class ChannelFragment : Fragment() {
lifecycleScope.launchWhenCreated { lifecycleScope.launchWhenCreated {
val response = try { val response = try {
val token = PreferenceHelper.getToken(requireContext()) val token = PreferenceHelper.getToken(requireContext())
RetrofitInstance.api.subscribe( RetrofitInstance.authApi.subscribe(
token, token,
Subscribe(channelId) Subscribe(channelId)
) )
@ -150,7 +150,7 @@ class ChannelFragment : Fragment() {
lifecycleScope.launchWhenCreated { lifecycleScope.launchWhenCreated {
val response = try { val response = try {
val token = PreferenceHelper.getToken(requireContext()) val token = PreferenceHelper.getToken(requireContext())
RetrofitInstance.api.unsubscribe( RetrofitInstance.authApi.unsubscribe(
token, token,
Subscribe(channelId) Subscribe(channelId)
) )

View File

@ -87,7 +87,7 @@ class LibraryFragment : Fragment() {
binding.playlistRefresh.isRefreshing = true binding.playlistRefresh.isRefreshing = true
lifecycleScope.launchWhenCreated { lifecycleScope.launchWhenCreated {
val response = try { val response = try {
RetrofitInstance.api.playlists(token) RetrofitInstance.authApi.playlists(token)
} catch (e: IOException) { } catch (e: IOException) {
println(e) println(e)
Log.e(TAG, "IOException, you might not have internet connection") Log.e(TAG, "IOException, you might not have internet connection")

View File

@ -1025,7 +1025,7 @@ class PlayerFragment : Fragment() {
lifecycleScope.launchWhenCreated { lifecycleScope.launchWhenCreated {
val response = try { val response = try {
val token = PreferenceHelper.getToken(requireContext()) val token = PreferenceHelper.getToken(requireContext())
RetrofitInstance.api.isSubscribed( RetrofitInstance.authApi.isSubscribed(
channel_id, channel_id,
token token
) )
@ -1065,7 +1065,7 @@ class PlayerFragment : Fragment() {
lifecycleScope.launchWhenCreated { lifecycleScope.launchWhenCreated {
val response = try { val response = try {
val token = PreferenceHelper.getToken(requireContext()) val token = PreferenceHelper.getToken(requireContext())
RetrofitInstance.api.subscribe( RetrofitInstance.authApi.subscribe(
token, token,
Subscribe(channel_id) Subscribe(channel_id)
) )
@ -1088,7 +1088,7 @@ class PlayerFragment : Fragment() {
lifecycleScope.launchWhenCreated { lifecycleScope.launchWhenCreated {
val response = try { val response = try {
val token = PreferenceHelper.getToken(requireContext()) val token = PreferenceHelper.getToken(requireContext())
RetrofitInstance.api.unsubscribe( RetrofitInstance.authApi.unsubscribe(
token, token,
Subscribe(channel_id) Subscribe(channel_id)
) )

View File

@ -109,7 +109,7 @@ class SubscriptionsFragment : Fragment() {
fun run() { fun run() {
lifecycleScope.launchWhenCreated { lifecycleScope.launchWhenCreated {
val response = try { val response = try {
RetrofitInstance.api.getFeed(token) RetrofitInstance.authApi.getFeed(token)
} catch (e: IOException) { } catch (e: IOException) {
Log.e(TAG, e.toString()) Log.e(TAG, e.toString())
Log.e(TAG, "IOException, you might not have internet connection") Log.e(TAG, "IOException, you might not have internet connection")
@ -148,7 +148,7 @@ class SubscriptionsFragment : Fragment() {
fun run() { fun run() {
lifecycleScope.launchWhenCreated { lifecycleScope.launchWhenCreated {
val response = try { val response = try {
RetrofitInstance.api.subscriptions(token) RetrofitInstance.authApi.subscriptions(token)
} catch (e: IOException) { } catch (e: IOException) {
Log.e(TAG, e.toString()) Log.e(TAG, e.toString())
Log.e(TAG, "IOException, you might not have internet connection") Log.e(TAG, "IOException, you might not have internet connection")

View File

@ -120,6 +120,7 @@ class InstanceSettings : PreferenceFragmentCompat() {
instance?.setOnPreferenceChangeListener { _, newValue -> instance?.setOnPreferenceChangeListener { _, newValue ->
requireMainActivityRestart = true requireMainActivityRestart = true
RetrofitInstance.url = newValue.toString() RetrofitInstance.url = newValue.toString()
RetrofitInstance.authUrl = newValue.toString()
RetrofitInstance.lazyMgr.reset() RetrofitInstance.lazyMgr.reset()
logout() logout()
true true
@ -302,7 +303,7 @@ class InstanceSettings : PreferenceFragmentCompat() {
lifecycleScope.launchWhenCreated { lifecycleScope.launchWhenCreated {
val response = try { val response = try {
val token = PreferenceHelper.getToken(requireContext()) val token = PreferenceHelper.getToken(requireContext())
RetrofitInstance.api.importSubscriptions( RetrofitInstance.authApi.importSubscriptions(
false, false,
token, token,
channels channels

View File

@ -5,6 +5,7 @@ import retrofit2.converter.jackson.JacksonConverterFactory
object RetrofitInstance { object RetrofitInstance {
lateinit var url: String lateinit var url: String
lateinit var authUrl: String
val lazyMgr = resettableManager() val lazyMgr = resettableManager()
val api: PipedApi by resettableLazy(lazyMgr) { val api: PipedApi by resettableLazy(lazyMgr) {
Retrofit.Builder() Retrofit.Builder()
@ -13,4 +14,11 @@ object RetrofitInstance {
.build() .build()
.create(PipedApi::class.java) .create(PipedApi::class.java)
} }
val authApi: PipedApi by resettableLazy(lazyMgr) {
Retrofit.Builder()
.baseUrl(authUrl)
.addConverterFactory(JacksonConverterFactory.create())
.build()
.create(PipedApi::class.java)
}
} }