Skip to content

Commit 0966945

Browse files
committed
[BOOK-46] feat: core:network 모듈 추가 및 API 통신 환경 구축
1 parent 16be4e0 commit 0966945

File tree

6 files changed

+140
-0
lines changed

6 files changed

+140
-0
lines changed

core/network/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

core/network/build.gradle.kts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
@file:Suppress("INLINE_FROM_HIGHER_PLATFORM")
2+
3+
import com.android.build.gradle.internal.cxx.configure.gradleLocalProperties
4+
5+
6+
plugins {
7+
alias(libs.plugins.booket.android.library)
8+
alias(libs.plugins.booket.android.retrofit)
9+
alias(libs.plugins.booket.android.hilt)
10+
}
11+
12+
android {
13+
namespace = "com.ninecraft.booket.core.network"
14+
15+
buildFeatures {
16+
buildConfig = true
17+
}
18+
19+
buildTypes {
20+
getByName("debug") {
21+
buildConfigField("String", "SERVER_BASE_URL", getServerBaseUrl("DEBUG_SERVER_BASE_URL"))
22+
}
23+
24+
getByName("release") {
25+
buildConfigField("String", "SERVER_BASE_URL", getServerBaseUrl("RELEASE_SERVER_BASE_URL"))
26+
}
27+
}
28+
}
29+
30+
dependencies {
31+
implementations(
32+
libs.logger,
33+
)
34+
}
35+
36+
fun getServerBaseUrl(propertyKey: String): String {
37+
return gradleLocalProperties(rootDir, providers).getProperty(propertyKey)
38+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package com.ninecraft.booket.core.network.di
2+
3+
import com.orhanobut.logger.Logger
4+
import dagger.Module
5+
import dagger.Provides
6+
import dagger.hilt.InstallIn
7+
import dagger.hilt.components.SingletonComponent
8+
import kotlinx.serialization.json.Json
9+
import okhttp3.MediaType.Companion.toMediaType
10+
import okhttp3.OkHttpClient
11+
import okhttp3.logging.HttpLoggingInterceptor
12+
import retrofit2.Retrofit
13+
import retrofit2.converter.kotlinx.serialization.asConverterFactory
14+
import com.ninecraft.booket.core.network.BuildConfig
15+
import com.ninecraft.booket.core.network.service.BooketService
16+
import retrofit2.create
17+
import java.util.concurrent.TimeUnit
18+
import javax.inject.Singleton
19+
20+
private const val MaxTimeoutMillis = 15_000L
21+
22+
private val jsonRule = Json {
23+
encodeDefaults = true
24+
ignoreUnknownKeys = true
25+
prettyPrint = true
26+
isLenient = true
27+
}
28+
29+
private val jsonConverterFactory = jsonRule.asConverterFactory("application/json".toMediaType())
30+
31+
@Module
32+
@InstallIn(SingletonComponent::class)
33+
internal object NetworkModule {
34+
35+
@Singleton
36+
@Provides
37+
internal fun provideHttpLoggingInterceptor(): HttpLoggingInterceptor {
38+
return HttpLoggingInterceptor { message ->
39+
Logger.d(message)
40+
}.apply {
41+
level = if (BuildConfig.DEBUG) {
42+
HttpLoggingInterceptor.Level.BODY
43+
} else {
44+
HttpLoggingInterceptor.Level.NONE
45+
}
46+
}
47+
}
48+
49+
@Singleton
50+
@Provides
51+
internal fun providePokemonOkHttpClient(
52+
httpLoggingInterceptor: HttpLoggingInterceptor,
53+
): OkHttpClient {
54+
return OkHttpClient.Builder()
55+
.connectTimeout(MaxTimeoutMillis, TimeUnit.MILLISECONDS)
56+
.readTimeout(MaxTimeoutMillis, TimeUnit.MILLISECONDS)
57+
.writeTimeout(MaxTimeoutMillis, TimeUnit.MILLISECONDS)
58+
.addInterceptor(httpLoggingInterceptor)
59+
.build()
60+
}
61+
62+
@Singleton
63+
@Provides
64+
internal fun providePokemonApiRetrofit(
65+
okHttpClient: OkHttpClient,
66+
): Retrofit {
67+
return Retrofit.Builder()
68+
.baseUrl(BuildConfig.SERVER_BASE_URL)
69+
.client(okHttpClient)
70+
.addConverterFactory(jsonConverterFactory)
71+
.build()
72+
}
73+
74+
@Singleton
75+
@Provides
76+
internal fun provideBooketService(
77+
retrofit: Retrofit,
78+
): BooketService {
79+
return retrofit.create()
80+
}
81+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.easyhooon.pokedex.core.network.response
2+
3+
import kotlinx.serialization.SerialName
4+
import kotlinx.serialization.Serializable
5+
6+
@Serializable
7+
data class HealthCheckResponse(
8+
@SerialName("status")
9+
val status: String,
10+
)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.ninecraft.booket.core.network.service
2+
3+
import com.easyhooon.pokedex.core.network.response.HealthCheckResponse
4+
import retrofit2.http.GET
5+
6+
interface BooketService {
7+
@GET("health")
8+
suspend fun checkServerHealth(): HealthCheckResponse
9+
}

settings.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ include(
2424
":app",
2525

2626
":core:designsystem",
27+
":core:network",
2728

2829
":feature:home",
2930
":feature:library",

0 commit comments

Comments
 (0)