From b1af6ba107d8d61407adfe02b85b948e7ea2b66f Mon Sep 17 00:00:00 2001 From: 0se0 <030ggong@gmail.com> Date: Fri, 15 Nov 2024 04:36:04 +0900 Subject: [PATCH] #7 Feature: ApiFactory --- .../java/org/sopt/and/network/ApiFactory.kt | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 app/src/main/java/org/sopt/and/network/ApiFactory.kt diff --git a/app/src/main/java/org/sopt/and/network/ApiFactory.kt b/app/src/main/java/org/sopt/and/network/ApiFactory.kt new file mode 100644 index 0000000..bc75c82 --- /dev/null +++ b/app/src/main/java/org/sopt/and/network/ApiFactory.kt @@ -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 create(): T = retrofit.create(T::class.java) +} + +object ServicePool { + val userService = ApiFactory.create() +} \ No newline at end of file