Skip to content

Commit

Permalink
#7 Feature: ApiFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
0se0 committed Nov 14, 2024
1 parent e3d474b commit b1af6ba
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions app/src/main/java/org/sopt/and/network/ApiFactory.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package org.sopt.and.network

import com.jakewharton.retrofit2.converter.kotlinx.serialization.asConverterFactory
import kotlinx.serialization.json.Json
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import org.sopt.and.BuildConfig
import retrofit2.Retrofit

object ApiFactory {
private const val BASE_URL: String = BuildConfig.BASE_URL

private val loggingInterceptor = HttpLoggingInterceptor().apply {
level = HttpLoggingInterceptor.Level.BODY
}

private val client = OkHttpClient.Builder()
.addInterceptor(loggingInterceptor)
.build()

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

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

object ServicePool {
val userService = ApiFactory.create<UserService>()
}

0 comments on commit b1af6ba

Please sign in to comment.