Skip to content

Commit

Permalink
feat/#9: friendRetrofit 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
youjin09222 committed May 8, 2024
1 parent 96abc70 commit 64997a1
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions app/src/main/java/com/sopt/now/test/data/ApiFactory.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,30 @@ import retrofit2.Retrofit
import java.io.IOException

object ApiFactory {
private const val BASE_URL: String = BuildConfig.AUTH_BASE_URL
private const val AUTH_BASE_URL: String = BuildConfig.AUTH_BASE_URL
private const val FRIEND_BASE_URL: String = BuildConfig.FRIEND_BASE_URL
private lateinit var userPreference: UserPreference // UserPreference 추가

// UserPreference 초기화
fun initializeUserPreference(userPreference: UserPreference) {
this.userPreference = userPreference
}

val retrofit: Retrofit by lazy {
val authRetrofit: Retrofit by lazy {
Retrofit.Builder()
.baseUrl(BASE_URL)
.baseUrl(AUTH_BASE_URL)
.client(provideOkHttpClient(HeaderInterceptor()))
.addConverterFactory(Json.asConverterFactory("application/json".toMediaType()))
.build()
}

val friendRetrofit: Retrofit by lazy {
Retrofit.Builder()
.baseUrl(FRIEND_BASE_URL)
.addConverterFactory(Json.asConverterFactory("application/json".toMediaType()))
.build()
}

private fun provideOkHttpClient(interceptor: HeaderInterceptor): OkHttpClient
= OkHttpClient.Builder().run {
addInterceptor(interceptor)
Expand All @@ -43,10 +51,12 @@ object ApiFactory {
}
}

inline fun <reified T> create(): T = retrofit.create(T::class.java)
inline fun <reified T> authCreate(): T = authRetrofit.create(T::class.java)
inline fun <reified T> friendCreate(): T = friendRetrofit.create(T::class.java)
}

object ServicePool {
val authService = ApiFactory.create<AuthService>()
val userService = ApiFactory.create<UserService>()
val authService = ApiFactory.authCreate<AuthService>()
val userService = ApiFactory.authCreate<UserService>()
val friendService = ApiFactory.friendCreate<FriendService>()
}

0 comments on commit 64997a1

Please sign in to comment.