Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: retrofit 의존성 주입 라이브러리 적용 #353

Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,29 @@ import com.now.domain.repository.AdventureRepository
import com.now.naaga.data.mapper.toDomain
import com.now.naaga.data.mapper.toDto
import com.now.naaga.data.remote.dto.FinishGameDto
import com.now.naaga.data.remote.retrofit.ServicePool
import com.now.naaga.data.remote.retrofit.service.AdventureService
import com.now.naaga.util.getValueOrThrow

class DefaultAdventureRepository : AdventureRepository {
class DefaultAdventureRepository(
private val adventureService: AdventureService,
) : AdventureRepository {
override suspend fun fetchMyAdventures(): List<Adventure> {
val response = ServicePool.adventureService.getMyGames().getValueOrThrow()
val response = adventureService.getMyGames().getValueOrThrow()
return response.map { it.toDomain() }
}

override suspend fun fetchAdventure(adventureId: Long): Adventure {
val response = ServicePool.adventureService.getGame(adventureId).getValueOrThrow()
val response = adventureService.getGame(adventureId).getValueOrThrow()
return response.toDomain()
}

override suspend fun fetchAdventureByStatus(status: AdventureStatus): List<Adventure> {
val response = ServicePool.adventureService.getGamesByStatus(status.name).getValueOrThrow()
val response = adventureService.getGamesByStatus(status.name).getValueOrThrow()
return response.map { it.toDomain() }
}

override suspend fun beginAdventure(coordinate: Coordinate): Adventure {
val response = ServicePool.adventureService.beginGame(coordinate.toDto()).getValueOrThrow()
val response = adventureService.beginGame(coordinate.toDto()).getValueOrThrow()
return response.toDomain()
}

Expand All @@ -42,30 +44,30 @@ class DefaultAdventureRepository : AdventureRepository {
coordinate: Coordinate,
): AdventureStatus {
val finishGameDto = FinishGameDto(endType.name, coordinate.toDto())
val response = ServicePool.adventureService.endGame(adventureId, finishGameDto).getValueOrThrow()
val response = adventureService.endGame(adventureId, finishGameDto).getValueOrThrow()
return AdventureStatus.getStatus(response.gameStatus)
}

override suspend fun fetchAdventureResult(adventureId: Long): AdventureResult {
val response = ServicePool.adventureService.getGameResult(adventureId).getValueOrThrow()
val response = adventureService.getGameResult(adventureId).getValueOrThrow()
return response.toDomain()
}

override suspend fun fetchMyAdventureResults(
sortBy: SortType,
order: OrderType,
): List<AdventureResult> {
val response = ServicePool.adventureService.getMyGameResults(sortBy.name, order.name).getValueOrThrow()
val response = adventureService.getMyGameResults(sortBy.name, order.name).getValueOrThrow()
return response.map { it.toDomain() }
}

override suspend fun fetchHint(adventureId: Long, hintId: Long): Hint {
val response = ServicePool.adventureService.getHint(adventureId, hintId).getValueOrThrow()
val response = adventureService.getHint(adventureId, hintId).getValueOrThrow()
return response.toDomain()
}

override suspend fun makeHint(adventureId: Long, coordinate: Coordinate): Hint {
val response = ServicePool.adventureService.requestHint(adventureId, coordinate.toDto()).getValueOrThrow()
val response = adventureService.requestHint(adventureId, coordinate.toDto()).getValueOrThrow()
return response.toDomain()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import com.now.domain.model.PlatformAuth
import com.now.domain.repository.AuthRepository
import com.now.naaga.data.local.AuthDataSource
import com.now.naaga.data.mapper.toDto
import com.now.naaga.data.remote.retrofit.ServicePool.authService
import com.now.naaga.data.remote.retrofit.service.AuthService
import com.now.naaga.util.getValueOrThrow
import com.now.naaga.util.unlinkWithKakao
import kotlinx.coroutines.CoroutineDispatcher
Expand All @@ -13,6 +13,7 @@ import kotlinx.coroutines.withContext

class DefaultAuthRepository(
private val authDataSource: AuthDataSource,
private val authService: AuthService,
private val dispatcher: CoroutineDispatcher = Dispatchers.IO,
) : AuthRepository {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import com.now.domain.model.Coordinate
import com.now.domain.model.Place
import com.now.domain.repository.PlaceRepository
import com.now.naaga.data.mapper.toDomain
import com.now.naaga.data.remote.retrofit.ServicePool.placeService
import com.now.naaga.data.remote.retrofit.service.PlaceService
import com.now.naaga.util.getValueOrThrow
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.MultipartBody
Expand All @@ -13,7 +13,9 @@ import okhttp3.RequestBody.Companion.asRequestBody
import okhttp3.RequestBody.Companion.toRequestBody
import java.io.File

class DefaultPlaceRepository : PlaceRepository {
class DefaultPlaceRepository(
private val placeService: PlaceService,
) : PlaceRepository {
override suspend fun fetchMyPlaces(
sortBy: String,
order: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ package com.now.naaga.data.repository
import com.now.domain.model.Rank
import com.now.domain.repository.RankRepository
import com.now.naaga.data.mapper.toDomain
import com.now.naaga.data.remote.retrofit.ServicePool.rankService
import com.now.naaga.data.remote.retrofit.service.RankService
import com.now.naaga.util.getValueOrThrow

class DefaultRankRepository : RankRepository {
class DefaultRankRepository(
private val rankService: RankService,
) : RankRepository {
override suspend fun getAllRanks(
sortBy: String,
order: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ package com.now.naaga.data.repository
import com.now.domain.model.Statistics
import com.now.domain.repository.StatisticsRepository
import com.now.naaga.data.mapper.toDomain
import com.now.naaga.data.remote.retrofit.ServicePool.statisticsService
import com.now.naaga.data.remote.retrofit.service.StatisticsService
import com.now.naaga.util.getValueOrThrow

class DefaultStatisticsRepository : StatisticsRepository {
class DefaultStatisticsRepository(
private val statisticsService: StatisticsService,
) : StatisticsRepository {
override suspend fun getMyStatistics(): Statistics {
val response = statisticsService.getMyStatistics()
return response.getValueOrThrow().toDomain()
Expand Down
19 changes: 14 additions & 5 deletions android/app/src/main/java/com/now/naaga/di/RepositoryModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ import com.now.domain.repository.PlaceRepository
import com.now.domain.repository.RankRepository
import com.now.domain.repository.StatisticsRepository
import com.now.naaga.data.local.AuthDataSource
import com.now.naaga.data.local.AuthDataSource
import com.now.naaga.data.remote.retrofit.service.AdventureService
import com.now.naaga.data.remote.retrofit.service.AuthService
import com.now.naaga.data.remote.retrofit.service.PlaceService
import com.now.naaga.data.remote.retrofit.service.RankService
import com.now.naaga.data.remote.retrofit.service.StatisticsService
import com.now.naaga.data.repository.DefaultAdventureRepository
import com.now.naaga.data.repository.DefaultAuthRepository
import com.now.naaga.data.repository.DefaultPlaceRepository
Expand All @@ -22,21 +28,24 @@ import javax.inject.Singleton
class RepositoryModule {
@Singleton
@Provides
fun provideRankRepository(): RankRepository = DefaultRankRepository()
fun provideRankRepository(rankService: RankService): RankRepository = DefaultRankRepository(rankService)

@Singleton
@Provides
fun provideAuthRepository(authDataSource: AuthDataSource): AuthRepository = DefaultAuthRepository(authDataSource)
fun provideAuthRepository(authDataSource: AuthDataSource, authService: AuthService): AuthRepository =
DefaultAuthRepository(authDataSource, authService)

@Singleton
@Provides
fun provideAdventureRepository(): AdventureRepository = DefaultAdventureRepository()
fun provideAdventureRepository(adventureService: AdventureService): AdventureRepository =
DefaultAdventureRepository(adventureService)

@Singleton
@Provides
fun providePlaceRepository(): PlaceRepository = DefaultPlaceRepository()
fun providePlaceRepository(placeService: PlaceService): PlaceRepository = DefaultPlaceRepository(placeService)

@Singleton
@Provides
fun provideStatisticsRepository(): StatisticsRepository = DefaultStatisticsRepository()
fun provideStatisticsRepository(statisticsService: StatisticsService): StatisticsRepository =
DefaultStatisticsRepository(statisticsService)
}
75 changes: 75 additions & 0 deletions android/app/src/main/java/com/now/naaga/di/ServiceModule.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package com.now.naaga.di

import com.jakewharton.retrofit2.converter.kotlinx.serialization.asConverterFactory
import com.now.naaga.BuildConfig
import com.now.naaga.NaagaApplication
import com.now.naaga.data.remote.retrofit.service.AdventureService
import com.now.naaga.data.remote.retrofit.service.AuthService
import com.now.naaga.data.remote.retrofit.service.PlaceService
import com.now.naaga.data.remote.retrofit.service.RankService
import com.now.naaga.data.remote.retrofit.service.StatisticsService
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import kotlinx.serialization.json.Json
import okhttp3.Interceptor
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.OkHttpClient
import retrofit2.Retrofit
import javax.inject.Singleton

@Module
@InstallIn(SingletonComponent::class)
class ServiceModule {
private val BASE_URL = BuildConfig.BASE_URL

@Singleton
@Provides
fun createInterceptor(): Interceptor = Interceptor { chain ->
val token: String = NaagaApplication.authDataSource.getAccessToken() ?: ""
with(chain) {
val newRequest = request().newBuilder()
.addHeader("Authorization", "Bearer $token")
.addHeader("Content-Type", "application/json")
.build()
proceed(newRequest)
}
}

@Singleton
@Provides
fun createOkHttpClient(interceptor: Interceptor): OkHttpClient {
return OkHttpClient.Builder().apply {
addInterceptor(interceptor)
}.build()
}

@Singleton
@Provides
fun provideRetrofit(okHttpClient: OkHttpClient): Retrofit = Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(Json.asConverterFactory("application/json".toMediaType()))
.client(okHttpClient)
.build()

@Singleton
@Provides
fun provideRankService(retrofit: Retrofit): RankService = retrofit.create(RankService::class.java)

@Singleton
@Provides
fun provideStatisticsService(retrofit: Retrofit): StatisticsService = retrofit.create(StatisticsService::class.java)

@Singleton
@Provides
fun provideAdventureService(retrofit: Retrofit): AdventureService = retrofit.create(AdventureService::class.java)

@Singleton
@Provides
fun providePlaceService(retrofit: Retrofit): PlaceService = retrofit.create(PlaceService::class.java)

@Singleton
@Provides
fun provideAuthService(retrofit: Retrofit): AuthService = retrofit.create(AuthService::class.java)
}
Loading