generated from AND-SOPT-ANDROID/and-sopt-android-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
155 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package org.sopt.and.data.di | ||
|
||
import okhttp3.Interceptor | ||
import okhttp3.Response | ||
import org.sopt.and.sharedpreference.User | ||
import javax.inject.Inject | ||
|
||
class AuthInterceptor @Inject constructor(private val user: User) : Interceptor { | ||
override fun intercept(chain: Interceptor.Chain): Response { | ||
|
||
val token = user.getUserToken() | ||
|
||
val request = chain.request().newBuilder() | ||
.apply { | ||
token?.let { | ||
addHeader(TOKEN, token) | ||
} | ||
} | ||
.build() | ||
|
||
return chain.proceed(request) | ||
} | ||
|
||
companion object { | ||
const val TOKEN: String = "token" | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
app/src/main/java/org/sopt/and/data/di/DataSourceModule.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package org.sopt.and.data.di | ||
|
||
import dagger.Binds | ||
import dagger.Module | ||
import dagger.hilt.InstallIn | ||
import dagger.hilt.components.SingletonComponent | ||
import org.sopt.and.data.datasource.WavveDataSource | ||
import org.sopt.and.data.datasourceimpl.WavveDataSourceImpl | ||
import javax.inject.Singleton | ||
|
||
@Module | ||
@InstallIn(SingletonComponent::class) | ||
internal abstract class DataSourceModule { | ||
@Binds | ||
@Singleton | ||
abstract fun bindsDataSource(myDataSourceImpl: WavveDataSourceImpl): WavveDataSource | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package org.sopt.and.data.di | ||
|
||
import android.util.Log | ||
import com.jakewharton.retrofit2.converter.kotlinx.serialization.asConverterFactory | ||
import dagger.Module | ||
import dagger.Provides | ||
import dagger.hilt.InstallIn | ||
import dagger.hilt.components.SingletonComponent | ||
import kotlinx.serialization.json.Json | ||
import okhttp3.MediaType.Companion.toMediaType | ||
import okhttp3.OkHttpClient | ||
import okhttp3.logging.HttpLoggingInterceptor | ||
import org.sopt.and.BuildConfig | ||
import org.sopt.and.sharedpreference.User | ||
import retrofit2.Retrofit | ||
import javax.inject.Singleton | ||
|
||
@Module | ||
@InstallIn(SingletonComponent::class) | ||
object NetworkModule { | ||
|
||
private const val CONTENT_TYPE = "application/json" | ||
private const val BASE_URL = BuildConfig.BASE_URL | ||
|
||
@Singleton | ||
@Provides | ||
fun provideJson(): Json { | ||
return Json { | ||
ignoreUnknownKeys = true | ||
isLenient = true | ||
encodeDefaults = true | ||
} | ||
} | ||
|
||
@Singleton | ||
@Provides | ||
fun provideLoggingInterceptor(): HttpLoggingInterceptor { | ||
return HttpLoggingInterceptor { message -> | ||
Log.d("Retrofit2", "CONNECTION INFO -> $message") | ||
}.apply { | ||
level = HttpLoggingInterceptor.Level.BODY | ||
} | ||
} | ||
|
||
|
||
@Singleton | ||
@Provides | ||
fun provideAuthInterceptor(user: User): AuthInterceptor { | ||
return AuthInterceptor(user) | ||
} | ||
|
||
@Singleton | ||
@Provides | ||
fun provideOkHttpClient( | ||
loggingInterceptor: HttpLoggingInterceptor, | ||
authInterceptor: AuthInterceptor | ||
): OkHttpClient { | ||
return OkHttpClient.Builder() | ||
.addInterceptor(loggingInterceptor) | ||
.addInterceptor(authInterceptor) | ||
.build() | ||
} | ||
|
||
@Singleton | ||
@Provides | ||
fun provideRetrofit(okHttpClient: OkHttpClient, json: Json): Retrofit { | ||
return Retrofit.Builder() | ||
.baseUrl(BASE_URL) | ||
.client(okHttpClient) | ||
.addConverterFactory(json.asConverterFactory(CONTENT_TYPE.toMediaType())) | ||
.build() | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
app/src/main/java/org/sopt/and/data/di/RepositoryModule.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package org.sopt.and.data.di | ||
|
||
import dagger.Binds | ||
import dagger.Module | ||
import dagger.hilt.InstallIn | ||
import dagger.hilt.components.SingletonComponent | ||
import org.sopt.and.data.repositoryimpl.WavveRepositoryImpl | ||
import org.sopt.and.domain.repository.WavveRepository | ||
import javax.inject.Singleton | ||
|
||
@Module | ||
@InstallIn(SingletonComponent::class) | ||
internal abstract class RepositoryModule { | ||
@Binds | ||
@Singleton | ||
abstract fun bindsRepository( | ||
myRepositoryImpl: WavveRepositoryImpl | ||
): WavveRepository | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package org.sopt.and.data.di | ||
|
||
import dagger.Module | ||
import dagger.Provides | ||
import dagger.hilt.InstallIn | ||
import dagger.hilt.components.SingletonComponent | ||
import org.sopt.and.data.service.WavveService | ||
import retrofit2.Retrofit | ||
import javax.inject.Singleton | ||
|
||
@Module | ||
@InstallIn(SingletonComponent::class) | ||
object ServiceModule { | ||
@Provides | ||
@Singleton | ||
fun providerService(retrofit: Retrofit): WavveService = | ||
retrofit.create(WavveService::class.java) | ||
} |