wedroid/app/src/main/java/it/unisannio/ding/ids/wedroid/app/util/ServicesFactory.kt

85 lines
2.4 KiB
Kotlin

package it.unisannio.ding.ids.wedroid.app.util
import it.unisannio.ding.ids.wedroid.wrapper.api.BoardService
import it.unisannio.ding.ids.wedroid.wrapper.api.CardCommentService
import it.unisannio.ding.ids.wedroid.wrapper.api.CardService
import it.unisannio.ding.ids.wedroid.wrapper.api.ChecklistService
import it.unisannio.ding.ids.wedroid.wrapper.api.ListService
import it.unisannio.ding.ids.wedroid.wrapper.api.SwimlanesService
import it.unisannio.ding.ids.wedroid.wrapper.api.UserService
import okhttp3.OkHttpClient
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
class ServicesFactory(
reader: PreferenceReader
) {
private val retrofit: Retrofit
init {
val httpClient = OkHttpClient.Builder()
.addInterceptor {
val request = it.request().newBuilder()
.addHeader("Authorization", "Bearer ${reader.token}")
.addHeader("Accept", "application/json")
.addHeader("Content-type", "application/json")
.build()
it.proceed(request)
}.build()
retrofit = Retrofit.Builder()
.baseUrl(reader.baseUrl)
.addConverterFactory(GsonConverterFactory.create())
.client(httpClient)
.build()
}
val boardService by lazy {
retrofit.create(BoardService::class.java)
}
val cardCommentService by lazy {
retrofit.create(CardCommentService::class.java)
}
val cardService by lazy {
retrofit.create(CardService::class.java)
}
val checklistService by lazy {
retrofit.create(ChecklistService::class.java)
}
val listService by lazy {
retrofit.create(ListService::class.java)
}
val swimlanesService by lazy {
retrofit.create(SwimlanesService::class.java)
}
val userService by lazy {
retrofit.create(UserService::class.java)
}
companion object {
@Volatile
private var INSTANCE: ServicesFactory? = null
fun getInstance(reader: PreferenceReader): ServicesFactory {
val tempInstance = INSTANCE
if (tempInstance != null)
return tempInstance
synchronized(this) {
val instance = ServicesFactory(reader)
INSTANCE = instance
return instance
}
}
}
}