mirror of
https://github.com/owenlejeune/MYDex.git
synced 2025-11-08 08:22:42 -05:00
setup retrofit client
This commit is contained in:
@@ -3,6 +3,8 @@
|
|||||||
xmlns:tools="http://schemas.android.com/tools"
|
xmlns:tools="http://schemas.android.com/tools"
|
||||||
package="com.owenlejeune.mydex">
|
package="com.owenlejeune.mydex">
|
||||||
|
|
||||||
|
<uses-permission android:name="android.permission.INTERNET" />
|
||||||
|
|
||||||
<application
|
<application
|
||||||
android:allowBackup="true"
|
android:allowBackup="true"
|
||||||
android:dataExtractionRules="@xml/data_extraction_rules"
|
android:dataExtractionRules="@xml/data_extraction_rules"
|
||||||
|
|||||||
@@ -3,6 +3,8 @@ package com.owenlejeune.mydex
|
|||||||
import android.app.Application
|
import android.app.Application
|
||||||
import com.facebook.stetho.Stetho
|
import com.facebook.stetho.Stetho
|
||||||
import com.kieronquinn.monetcompat.core.MonetCompat
|
import com.kieronquinn.monetcompat.core.MonetCompat
|
||||||
|
import com.owenlejeune.mydex.di.Modules
|
||||||
|
import com.owenlejeune.mydex.di.networkModule
|
||||||
import com.owenlejeune.mydex.di.preferencesModule
|
import com.owenlejeune.mydex.di.preferencesModule
|
||||||
import com.owenlejeune.mydex.preferences.AppPreferences
|
import com.owenlejeune.mydex.preferences.AppPreferences
|
||||||
import org.koin.android.ext.android.inject
|
import org.koin.android.ext.android.inject
|
||||||
@@ -24,9 +26,7 @@ class MYDexApplication: Application() {
|
|||||||
if (BuildConfig.DEBUG) Level.ERROR else Level.NONE
|
if (BuildConfig.DEBUG) Level.ERROR else Level.NONE
|
||||||
)
|
)
|
||||||
androidContext(this@MYDexApplication)
|
androidContext(this@MYDexApplication)
|
||||||
modules(
|
modules(Modules)
|
||||||
preferencesModule
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
MonetCompat.enablePaletteCompat()
|
MonetCompat.enablePaletteCompat()
|
||||||
|
|||||||
32
app/src/main/java/com/owenlejeune/mydex/api/Client.kt
Normal file
32
app/src/main/java/com/owenlejeune/mydex/api/Client.kt
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
package com.owenlejeune.mydex.api
|
||||||
|
|
||||||
|
import okhttp3.Interceptor
|
||||||
|
import org.koin.core.component.KoinComponent
|
||||||
|
import org.koin.core.component.inject
|
||||||
|
import retrofit2.Retrofit
|
||||||
|
|
||||||
|
class Client(baseUrl: String): KoinComponent {
|
||||||
|
|
||||||
|
private val converter: ConverterFactoryFactory by inject()
|
||||||
|
private val client: HttpClient by inject()
|
||||||
|
|
||||||
|
private var retrofit: Retrofit = Retrofit.Builder()
|
||||||
|
.baseUrl(baseUrl)
|
||||||
|
.client(client.httpClient)
|
||||||
|
.addConverterFactory(converter.get())
|
||||||
|
.build()
|
||||||
|
|
||||||
|
fun <T> create(service: Class<T>): T {
|
||||||
|
return retrofit.create(service)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun addInterceptor(interceptor: Interceptor) {
|
||||||
|
val iClient = client.httpClient.newBuilder()
|
||||||
|
.addInterceptor(interceptor)
|
||||||
|
.build()
|
||||||
|
retrofit = retrofit.newBuilder()
|
||||||
|
.client(iClient)
|
||||||
|
.build()
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
package com.owenlejeune.mydex.api
|
||||||
|
|
||||||
|
import retrofit2.Converter
|
||||||
|
|
||||||
|
interface ConverterFactoryFactory {
|
||||||
|
|
||||||
|
fun get(): Converter.Factory
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
package com.owenlejeune.mydex.api
|
||||||
|
|
||||||
|
import com.facebook.stetho.okhttp3.StethoInterceptor
|
||||||
|
import okhttp3.OkHttpClient
|
||||||
|
|
||||||
|
class DebugHttpClient: HttpClient {
|
||||||
|
override val httpClient: OkHttpClient
|
||||||
|
get() = OkHttpClient.Builder()
|
||||||
|
.addNetworkInterceptor(StethoInterceptor())
|
||||||
|
.build()
|
||||||
|
}
|
||||||
17
app/src/main/java/com/owenlejeune/mydex/api/GsonConverter.kt
Normal file
17
app/src/main/java/com/owenlejeune/mydex/api/GsonConverter.kt
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
package com.owenlejeune.mydex.api
|
||||||
|
|
||||||
|
import com.google.gson.Gson
|
||||||
|
import org.koin.core.component.KoinComponent
|
||||||
|
import org.koin.core.component.inject
|
||||||
|
import retrofit2.Converter
|
||||||
|
import retrofit2.converter.gson.GsonConverterFactory
|
||||||
|
|
||||||
|
class GsonConverter: ConverterFactoryFactory, KoinComponent {
|
||||||
|
|
||||||
|
private val gson: Gson by inject()
|
||||||
|
|
||||||
|
override fun get(): Converter.Factory {
|
||||||
|
return GsonConverterFactory.create(gson)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package com.owenlejeune.mydex.api
|
||||||
|
|
||||||
|
import okhttp3.OkHttpClient
|
||||||
|
|
||||||
|
interface HttpClient {
|
||||||
|
val httpClient: OkHttpClient
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
package com.owenlejeune.mydex.api
|
||||||
|
|
||||||
|
import okhttp3.OkHttpClient
|
||||||
|
|
||||||
|
class ProdHttpClient: HttpClient {
|
||||||
|
override val httpClient: OkHttpClient
|
||||||
|
get() = OkHttpClient.Builder().build()
|
||||||
|
}
|
||||||
@@ -1,4 +1,20 @@
|
|||||||
package com.owenlejeune.mydex.api.pokeapi
|
package com.owenlejeune.mydex.api.pokeapi
|
||||||
|
|
||||||
class PokeApiClient {
|
import com.owenlejeune.mydex.api.Client
|
||||||
|
import com.owenlejeune.mydex.preferences.AppPreferences
|
||||||
|
import org.koin.core.component.KoinComponent
|
||||||
|
import org.koin.core.component.inject
|
||||||
|
import org.koin.core.parameter.parametersOf
|
||||||
|
|
||||||
|
class PokeApiClient: KoinComponent {
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
const val BASE_URL = "https://pokeapi.co/api/v2/"
|
||||||
|
}
|
||||||
|
|
||||||
|
private val client: Client by inject { parametersOf(BASE_URL) }
|
||||||
|
private val preferences: AppPreferences by inject()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,8 +1,33 @@
|
|||||||
package com.owenlejeune.mydex.di
|
package com.owenlejeune.mydex.di
|
||||||
|
|
||||||
|
import com.google.gson.GsonBuilder
|
||||||
|
import com.google.gson.TypeAdapterFactory
|
||||||
|
import com.owenlejeune.mydex.BuildConfig
|
||||||
|
import com.owenlejeune.mydex.api.*
|
||||||
|
import com.owenlejeune.mydex.api.pokeapi.PokeApiClient
|
||||||
import com.owenlejeune.mydex.preferences.AppPreferences
|
import com.owenlejeune.mydex.preferences.AppPreferences
|
||||||
import org.koin.dsl.module
|
import org.koin.dsl.module
|
||||||
|
|
||||||
|
val networkModule = module {
|
||||||
|
single { if (BuildConfig.DEBUG) DebugHttpClient() else ProdHttpClient() }
|
||||||
|
single<ConverterFactoryFactory> { GsonConverter() }
|
||||||
|
factory { (baseUrl: String) -> Client(baseUrl) }
|
||||||
|
|
||||||
|
single<List<TypeAdapterFactory>> {
|
||||||
|
listOf(FlattenTypeAdapterFactory())
|
||||||
|
}
|
||||||
|
|
||||||
|
single {
|
||||||
|
GsonBuilder().apply {
|
||||||
|
get<List<TypeAdapterFactory>>().forEach { taf -> registerTypeAdapterFactory(taf) }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
single { PokeApiClient() }
|
||||||
|
}
|
||||||
|
|
||||||
val preferencesModule = module {
|
val preferencesModule = module {
|
||||||
single { AppPreferences(get()) }
|
single { AppPreferences(get()) }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
val Modules = listOf(networkModule, preferencesModule)
|
||||||
Reference in New Issue
Block a user