mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-29 13:53:54 +01:00
Decouple from the data-client service factory, with some code cleanup in the process (#5496)
This commit is contained in:
parent
7ec2a22fce
commit
ab9e57f5be
10 changed files with 75 additions and 71 deletions
|
|
@ -0,0 +1,25 @@
|
|||
package fr.free.nrw.commons.wikidata
|
||||
|
||||
import okhttp3.OkHttpClient
|
||||
import org.wikipedia.json.GsonUtil
|
||||
import retrofit2.Retrofit
|
||||
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
|
||||
import retrofit2.converter.gson.GsonConverterFactory
|
||||
|
||||
class CommonsServiceFactory(private val okHttpClient: OkHttpClient) {
|
||||
|
||||
private val builder: Retrofit.Builder by lazy {
|
||||
// All instances of retrofit share this configuration, but create it lazily
|
||||
Retrofit.Builder().client(okHttpClient)
|
||||
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
|
||||
.addConverterFactory(GsonConverterFactory.create(GsonUtil.getDefaultGson()))
|
||||
}
|
||||
|
||||
private val retrofitCache: MutableMap<String, Retrofit> = mutableMapOf()
|
||||
|
||||
fun <T : Any> create(baseUrl: String, service: Class<T>): T = retrofitCache.getOrPut(baseUrl) {
|
||||
// Cache instances of retrofit based on API backend
|
||||
builder.baseUrl(baseUrl).build()
|
||||
}.create(service)
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue