Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEAT/#6] Retrofit Module Setting #7

Merged
merged 2 commits into from
Dec 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .github/workflows/pr_checker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ jobs:
- name: Create Local Properties
run: touch local.properties

- name: Access Local Properties
env:
base_url: ${{ secrets.BASE_URL }}
run: |
echo base.url=\"$base_url\" >> local.properties

- name: Build debug APK
run: ./gradlew assembleDebug --stacktrace

Expand Down
55 changes: 29 additions & 26 deletions app/src/main/java/com/teumteum/teumteum/di/RetrofitModule.kt
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()
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ import javax.inject.Singleton
object ServiceModule {
@Singleton
@Provides
fun provideSampleService(@RetrofitModule.TeumTeum teumteumRetrofit: Retrofit) =
fun provideSampleService(teumteumRetrofit: Retrofit) =
teumteumRetrofit.create(SampleService::class.java)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package plugins

import Constants
import com.android.build.api.dsl.ApplicationExtension
import com.android.build.gradle.internal.cxx.configure.gradleLocalProperties
import com.teumteum.convention.src.main.kotlin.ext.androidTestImplementation
import com.teumteum.convention.src.main.kotlin.ext.getBundle
import com.teumteum.convention.src.main.kotlin.ext.getLibrary
Expand Down Expand Up @@ -49,49 +50,19 @@ class AndroidApplicationPlugin : Plugin<Project> {

buildTypes {
debug {
// buildConfigField(
// "String",
// "BASE_URL",
// gradleLocalProperties(rootDir).getProperty("test.base.url"),
// )
//
// buildConfigField(
// "String",
// "NATIVE_APP_KEY",
// gradleLocalProperties(rootDir).getProperty("test.native.app.key"),
// )
//
// buildConfigField(
// "String",
// "AMPLITUDE_API_KEY",
// gradleLocalProperties(rootDir).getProperty("amplitude.api.test.key"),
// )
//
// manifestPlaceholders["NATIVE_APP_KEY"] =
// gradleLocalProperties(rootDir).getProperty("testNativeAppKey")
// }
//
// release {
// buildConfigField(
// "String",
// "BASE_URL",
// gradleLocalProperties(rootDir).getProperty("base.url"),
// )
//
// buildConfigField(
// "String",
// "NATIVE_APP_KEY",
// gradleLocalProperties(rootDir).getProperty("native.app.key"),
// )
//
// buildConfigField(
// "String",
// "AMPLITUDE_API_KEY",
// gradleLocalProperties(rootDir).getProperty("amplitude.api.key"),
// )
//
// manifestPlaceholders["NATIVE_APP_KEY"] =
// gradleLocalProperties(rootDir).getProperty("nativeAppKey")
buildConfigField(
"String",
"BASE_URL",
gradleLocalProperties(rootDir).getProperty("base.url"),
)
}

release {
buildConfigField(
"String",
"BASE_URL",
gradleLocalProperties(rootDir).getProperty("base.url"),
)

isMinifyEnabled = false
proguardFiles(
Expand Down
20 changes: 10 additions & 10 deletions core/data/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@ plugins {
android {
buildTypes {
debug {
// buildConfigField(
// "String",
// "BASE_URL",
// gradleLocalProperties(rootDir).getProperty("test.base.url"),
// )
buildConfigField(
"String",
"BASE_URL",
gradleLocalProperties(rootDir).getProperty("base.url"),
)
}

release {
// buildConfigField(
// "String",
// "BASE_URL",
// gradleLocalProperties(rootDir).getProperty("base.url"),
// )
buildConfigField(
"String",
"BASE_URL",
gradleLocalProperties(rootDir).getProperty("base.url"),
)
}
}

Expand Down