-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
5 changed files
with
60 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 29 additions & 26 deletions
55
app/src/main/java/com/teumteum/teumteum/di/RetrofitModule.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,59 @@ | ||
package com.teumteum.teumteum.di | ||
|
||
import com.jakewharton.retrofit2.converter.kotlinx.serialization.asConverterFactory | ||
import com.teumteum.teumteum.BuildConfig.BASE_URL | ||
import dagger.Module | ||
import dagger.Provides | ||
import dagger.hilt.InstallIn | ||
import dagger.hilt.components.SingletonComponent | ||
import kotlinx.coroutines.InternalCoroutinesApi | ||
import kotlinx.serialization.ExperimentalSerializationApi | ||
import kotlinx.serialization.json.Json | ||
import okhttp3.Interceptor | ||
import okhttp3.MediaType.Companion.toMediaType | ||
import okhttp3.OkHttpClient | ||
import okhttp3.logging.HttpLoggingInterceptor | ||
import retrofit2.Converter | ||
import retrofit2.Retrofit | ||
import javax.inject.Qualifier | ||
import javax.inject.Singleton | ||
|
||
@Module | ||
@InstallIn(SingletonComponent::class) | ||
object RetrofitModule { | ||
@Qualifier | ||
@Retention(AnnotationRetention.BINARY) | ||
annotation class TeumTeum | ||
|
||
@Provides | ||
@Singleton | ||
fun provideOkHttpClient( | ||
logger: HttpLoggingInterceptor | ||
): OkHttpClient = OkHttpClient.Builder().addInterceptor(logger) | ||
.build() | ||
private const val APPLICATION_JSON = "application/json" | ||
|
||
@Provides | ||
@Singleton | ||
fun provideLogger(): HttpLoggingInterceptor = HttpLoggingInterceptor().apply { | ||
fun provideHttpLoggingInterceptor(): Interceptor = HttpLoggingInterceptor().apply { | ||
level = HttpLoggingInterceptor.Level.BODY | ||
} | ||
|
||
@OptIn(ExperimentalSerializationApi::class, InternalCoroutinesApi::class) | ||
@Provides | ||
@Singleton | ||
@TeumTeum | ||
fun provideTeumTeumRetrofit(json: Json, client: OkHttpClient): Retrofit { | ||
kotlinx.coroutines.internal.synchronized(this) { | ||
val retrofit = Retrofit.Builder().baseUrl("").client(client) | ||
.addConverterFactory(json.asConverterFactory("application/json".toMediaType())) | ||
.build() | ||
return retrofit ?: throw RuntimeException("Retrofit creation failed.") | ||
} | ||
fun provideJson(): Json = Json { | ||
ignoreUnknownKeys = true | ||
prettyPrint = true | ||
} | ||
|
||
@Provides | ||
@Singleton | ||
fun provideJson(): Json = Json { | ||
ignoreUnknownKeys = true | ||
} | ||
fun provideOkHttpClient( | ||
loggingInterceptor: Interceptor | ||
): OkHttpClient = OkHttpClient.Builder() | ||
.addInterceptor(loggingInterceptor) | ||
.build() | ||
|
||
@Provides | ||
@Singleton | ||
fun provideJsonConverter(json: Json): Converter.Factory = | ||
json.asConverterFactory(APPLICATION_JSON.toMediaType()) | ||
|
||
@Provides | ||
@Singleton | ||
fun provideTeumTeumRetrofit( | ||
client: OkHttpClient, | ||
factory: Converter.Factory | ||
): Retrofit = Retrofit.Builder() | ||
.baseUrl(BASE_URL) | ||
.client(client) | ||
.addConverterFactory(factory) | ||
.build() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters