Create api from user without side effect
This commit is contained in:
parent
ba372a3d17
commit
6e1e12af0e
@ -3,6 +3,11 @@ package org.pixeldroid.app.utils.api
|
|||||||
import org.pixeldroid.app.utils.api.objects.*
|
import org.pixeldroid.app.utils.api.objects.*
|
||||||
import io.reactivex.Observable
|
import io.reactivex.Observable
|
||||||
import okhttp3.MultipartBody
|
import okhttp3.MultipartBody
|
||||||
|
import okhttp3.OkHttpClient
|
||||||
|
import org.pixeldroid.app.utils.db.AppDatabase
|
||||||
|
import org.pixeldroid.app.utils.db.entities.UserDatabaseEntity
|
||||||
|
import org.pixeldroid.app.utils.di.PixelfedAPIHolder
|
||||||
|
import org.pixeldroid.app.utils.di.TokenAuthenticator
|
||||||
import retrofit2.Response
|
import retrofit2.Response
|
||||||
import retrofit2.Retrofit
|
import retrofit2.Retrofit
|
||||||
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
|
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
|
||||||
@ -28,6 +33,30 @@ interface PixelfedAPI {
|
|||||||
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
|
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
|
||||||
.build().create(PixelfedAPI::class.java)
|
.build().create(PixelfedAPI::class.java)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private val intermediate: Retrofit.Builder = Retrofit.Builder()
|
||||||
|
.addConverterFactory(GsonConverterFactory.create())
|
||||||
|
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
|
||||||
|
|
||||||
|
|
||||||
|
fun apiForUser(
|
||||||
|
user: UserDatabaseEntity,
|
||||||
|
db: AppDatabase,
|
||||||
|
pixelfedAPIHolder: PixelfedAPIHolder
|
||||||
|
): PixelfedAPI =
|
||||||
|
intermediate
|
||||||
|
.baseUrl(user.instance_uri)
|
||||||
|
.client(
|
||||||
|
OkHttpClient().newBuilder().authenticator(TokenAuthenticator(user, db, pixelfedAPIHolder))
|
||||||
|
.addInterceptor {
|
||||||
|
it.request().newBuilder().run {
|
||||||
|
header("Accept", "application/json")
|
||||||
|
header("Authorization", "Bearer ${user.accessToken}")
|
||||||
|
it.proceed(build())
|
||||||
|
}
|
||||||
|
}.build()
|
||||||
|
)
|
||||||
|
.build().create(PixelfedAPI::class.java)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -8,9 +8,7 @@ import dagger.Module
|
|||||||
import dagger.Provides
|
import dagger.Provides
|
||||||
import kotlinx.coroutines.runBlocking
|
import kotlinx.coroutines.runBlocking
|
||||||
import okhttp3.*
|
import okhttp3.*
|
||||||
import retrofit2.Retrofit
|
import org.pixeldroid.app.utils.api.PixelfedAPI.Companion.apiForUser
|
||||||
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
|
|
||||||
import retrofit2.converter.gson.GsonConverterFactory
|
|
||||||
import javax.inject.Singleton
|
import javax.inject.Singleton
|
||||||
|
|
||||||
@Module
|
@Module
|
||||||
@ -78,9 +76,6 @@ class TokenAuthenticator(val user: UserDatabaseEntity, val db: AppDatabase, val
|
|||||||
}
|
}
|
||||||
|
|
||||||
class PixelfedAPIHolder(private val db: AppDatabase){
|
class PixelfedAPIHolder(private val db: AppDatabase){
|
||||||
private val intermediate: Retrofit.Builder = Retrofit.Builder()
|
|
||||||
.addConverterFactory(GsonConverterFactory.create())
|
|
||||||
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
|
|
||||||
|
|
||||||
var api: PixelfedAPI? =
|
var api: PixelfedAPI? =
|
||||||
db.userDao().getActiveUser()?.let {
|
db.userDao().getActiveUser()?.let {
|
||||||
@ -90,21 +85,8 @@ class PixelfedAPIHolder(private val db: AppDatabase){
|
|||||||
fun setToCurrentUser(
|
fun setToCurrentUser(
|
||||||
user: UserDatabaseEntity = db.userDao().getActiveUser()!!
|
user: UserDatabaseEntity = db.userDao().getActiveUser()!!
|
||||||
): PixelfedAPI {
|
): PixelfedAPI {
|
||||||
val newAPI = intermediate
|
val newAPI = apiForUser(user, db, this)
|
||||||
.baseUrl(user.instance_uri)
|
|
||||||
.client(
|
|
||||||
OkHttpClient().newBuilder().authenticator(TokenAuthenticator(user, db, this))
|
|
||||||
.addInterceptor {
|
|
||||||
it.request().newBuilder().run {
|
|
||||||
header("Accept", "application/json")
|
|
||||||
header("Authorization", "Bearer ${user.accessToken}")
|
|
||||||
it.proceed(build())
|
|
||||||
}
|
|
||||||
}.build()
|
|
||||||
)
|
|
||||||
.build().create(PixelfedAPI::class.java)
|
|
||||||
api = newAPI
|
api = newAPI
|
||||||
return newAPI
|
return newAPI
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user