Skip to content

Commit cccd2ec

Browse files
committed
Feat: 카카오 로컬 API 연동 및 현재 주소 조회 기능 추가
1 parent 0ae1011 commit cccd2ec

11 files changed

Lines changed: 162 additions & 2 deletions

File tree

app/build.gradle.kts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,17 @@ android {
4646
name = "KAKAO_NATIVE_APP_KEY",
4747
value = "\"$kakaoNativeAppKey\"",
4848
)
49+
50+
val kakaoRestApiKey =
51+
(properties["kakao.rest.api.key"] as? String)
52+
?: System.getenv("KAKAO_REST_API_KEY")
53+
?: throw GradleException("KAKAO_REST_API_KEY 값이 없습니다.")
54+
55+
buildConfigField(
56+
type = "String",
57+
name = "KAKAO_REST_API_KEY",
58+
value = "\"$kakaoRestApiKey\"",
59+
)
4960
}
5061

5162
buildTypes {

app/src/main/java/com/threegap/bitnagil/di/core/NetworkModule.kt

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ import javax.inject.Singleton
3030
@InstallIn(SingletonComponent::class)
3131
object NetworkModule {
3232
private const val APPLICATION_JSON = "application/json"
33+
private const val REST_API_KEY = BuildConfig.KAKAO_REST_API_KEY
34+
private const val KAKAO_URL = "https://dapi.kakao.com"
3335

3436
@Provides
3537
@Singleton
@@ -74,6 +76,43 @@ object NetworkModule {
7476
override suspend fun clearTokens() = dataStore.clearAuthToken()
7577
}
7678

79+
@Provides
80+
@Singleton
81+
@Kakao
82+
fun provideKakaoAuthInterceptor(): Interceptor =
83+
Interceptor { chain ->
84+
val request = chain.request().newBuilder()
85+
.addHeader("Authorization", "KakaoAK $REST_API_KEY")
86+
.build()
87+
chain.proceed(request)
88+
}
89+
90+
@Provides
91+
@Singleton
92+
@Kakao
93+
fun provideKakaoOkHttpClient(
94+
httpLoggingInterceptor: HttpLoggingInterceptor,
95+
@Kakao kakaoAuthInterceptor: Interceptor,
96+
): OkHttpClient = OkHttpClient.Builder()
97+
.addInterceptor(kakaoAuthInterceptor)
98+
.addInterceptor(httpLoggingInterceptor)
99+
.connectTimeout(10L, TimeUnit.SECONDS)
100+
.writeTimeout(30L, TimeUnit.SECONDS)
101+
.readTimeout(30L, TimeUnit.SECONDS)
102+
.build()
103+
104+
@Provides
105+
@Singleton
106+
@Kakao
107+
fun provideKakaoRetrofit(
108+
converterFactory: Converter.Factory,
109+
@Kakao okHttpClient: OkHttpClient,
110+
): Retrofit = Retrofit.Builder()
111+
.baseUrl(KAKAO_URL)
112+
.addConverterFactory(converterFactory)
113+
.client(okHttpClient)
114+
.build()
115+
77116
@Provides
78117
@Singleton
79118
fun provideTokenAuthenticator(

app/src/main/java/com/threegap/bitnagil/di/core/Qualifier.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,7 @@ annotation class NoneAuth
99
@Qualifier
1010
@Retention(AnnotationRetention.BINARY)
1111
annotation class Auth
12+
13+
@Qualifier
14+
@Retention(AnnotationRetention.BINARY)
15+
annotation class Kakao

app/src/main/java/com/threegap/bitnagil/di/data/DataSourceModule.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package com.threegap.bitnagil.di.data
22

3+
import com.threegap.bitnagil.data.address.datasource.AddressDataSource
4+
import com.threegap.bitnagil.data.address.datasource.LocationDataSource
5+
import com.threegap.bitnagil.data.address.datasourceImpl.AddressDataSourceImpl
6+
import com.threegap.bitnagil.data.address.datasourceImpl.LocationDataSourceImpl
37
import com.threegap.bitnagil.data.auth.datasource.AuthLocalDataSource
48
import com.threegap.bitnagil.data.auth.datasource.AuthRemoteDataSource
59
import com.threegap.bitnagil.data.auth.datasourceimpl.AuthLocalDataSourceImpl
@@ -63,4 +67,12 @@ abstract class DataSourceModule {
6367
@Binds
6468
@Singleton
6569
abstract fun bindVersionDataSource(versionDataSourceImpl: VersionDataSourceImpl): VersionDataSource
70+
71+
@Binds
72+
@Singleton
73+
abstract fun bindLocationDataSource(locationDataSourceImpl: LocationDataSourceImpl): LocationDataSource
74+
75+
@Binds
76+
@Singleton
77+
abstract fun bindAddressDataSource(addressDataSourceImpl: AddressDataSourceImpl): AddressDataSource
6678
}

app/src/main/java/com/threegap/bitnagil/di/data/RepositoryModule.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.threegap.bitnagil.di.data
22

3+
import com.threegap.bitnagil.data.address.repositoryImpl.AddressRepositoryImpl
34
import com.threegap.bitnagil.data.auth.repositoryimpl.AuthRepositoryImpl
45
import com.threegap.bitnagil.data.emotion.repositoryImpl.EmotionRepositoryImpl
56
import com.threegap.bitnagil.data.onboarding.repositoryImpl.OnBoardingRepositoryImpl
@@ -8,6 +9,7 @@ import com.threegap.bitnagil.data.routine.repositoryImpl.RoutineRepositoryImpl
89
import com.threegap.bitnagil.data.user.repositoryImpl.UserRepositoryImpl
910
import com.threegap.bitnagil.data.version.repositoryImpl.VersionRepositoryImpl
1011
import com.threegap.bitnagil.data.writeroutine.repositoryImpl.WriteRoutineRepositoryImpl
12+
import com.threegap.bitnagil.domain.address.repository.AddressRepository
1113
import com.threegap.bitnagil.domain.auth.repository.AuthRepository
1214
import com.threegap.bitnagil.domain.emotion.repository.EmotionRepository
1315
import com.threegap.bitnagil.domain.onboarding.repository.OnBoardingRepository
@@ -57,4 +59,8 @@ abstract class RepositoryModule {
5759
@Binds
5860
@Singleton
5961
abstract fun bindVersionRepository(versionRepositoryImpl: VersionRepositoryImpl): VersionRepository
62+
63+
@Binds
64+
@Singleton
65+
abstract fun bindAddressRepository(addressRepositoryImpl: AddressRepositoryImpl): AddressRepository
6066
}

app/src/main/java/com/threegap/bitnagil/di/data/ServiceModule.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.threegap.bitnagil.di.data
22

3+
import com.threegap.bitnagil.data.address.service.AddressService
34
import com.threegap.bitnagil.data.auth.service.AuthService
45
import com.threegap.bitnagil.data.emotion.service.EmotionService
56
import com.threegap.bitnagil.data.onboarding.service.OnBoardingService
@@ -9,6 +10,7 @@ import com.threegap.bitnagil.data.user.service.UserService
910
import com.threegap.bitnagil.data.version.service.VersionService
1011
import com.threegap.bitnagil.data.writeroutine.service.WriteRoutineService
1112
import com.threegap.bitnagil.di.core.Auth
13+
import com.threegap.bitnagil.di.core.Kakao
1214
import com.threegap.bitnagil.di.core.NoneAuth
1315
import com.threegap.bitnagil.network.token.ReissueService
1416
import dagger.Module
@@ -66,4 +68,9 @@ object ServiceModule {
6668
@Singleton
6769
fun provideVersionService(@NoneAuth retrofit: Retrofit): VersionService =
6870
retrofit.create(VersionService::class.java)
71+
72+
@Provides
73+
@Singleton
74+
fun provideAddressService(@Kakao retrofit: Retrofit): AddressService =
75+
retrofit.create(AddressService::class.java)
6976
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.threegap.bitnagil.data.address.datasource
2+
3+
import com.threegap.bitnagil.data.address.model.response.Coord2AddressResponse
4+
5+
interface AddressDataSource {
6+
suspend fun fetchCurrentAddress(longitude: Double, latitude: Double): Result<Coord2AddressResponse>
7+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.threegap.bitnagil.data.address.datasourceImpl
2+
3+
import com.threegap.bitnagil.data.address.datasource.AddressDataSource
4+
import com.threegap.bitnagil.data.address.model.response.Coord2AddressResponse
5+
import com.threegap.bitnagil.data.address.service.AddressService
6+
import javax.inject.Inject
7+
8+
class AddressDataSourceImpl @Inject constructor(
9+
private val addressService: AddressService,
10+
) : AddressDataSource {
11+
12+
override suspend fun fetchCurrentAddress(longitude: Double, latitude: Double): Result<Coord2AddressResponse> {
13+
return runCatching {
14+
addressService.fetchCurrentAddress(
15+
longitude = longitude.toString(),
16+
latitude = latitude.toString(),
17+
)
18+
}
19+
}
20+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.threegap.bitnagil.data.address.repositoryImpl
2+
3+
import com.threegap.bitnagil.data.address.datasource.AddressDataSource
4+
import com.threegap.bitnagil.data.address.datasource.LocationDataSource
5+
import com.threegap.bitnagil.data.address.model.response.toAddress
6+
import com.threegap.bitnagil.domain.address.model.CurrentLocation
7+
import com.threegap.bitnagil.domain.address.repository.AddressRepository
8+
import javax.inject.Inject
9+
10+
class AddressRepositoryImpl @Inject constructor(
11+
private val locationDataSource: LocationDataSource,
12+
private val addressDataSource: AddressDataSource,
13+
) : AddressRepository {
14+
15+
override suspend fun fetchCurrentLocation(): Result<CurrentLocation> {
16+
return locationDataSource.fetchCurrentLocation()
17+
}
18+
19+
override suspend fun fetchCurrentAddress(latitude: Double, longitude: Double): Result<String> {
20+
return addressDataSource.fetchCurrentAddress(latitude, longitude)
21+
.mapCatching {
22+
it.toAddress() ?: throw Exception("Address not found")
23+
}
24+
}
25+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.threegap.bitnagil.data.address.service
2+
3+
import com.threegap.bitnagil.data.address.model.response.Coord2AddressResponse
4+
import retrofit2.http.GET
5+
import retrofit2.http.Query
6+
7+
interface AddressService {
8+
@GET("/v2/local/geo/coord2address.json")
9+
suspend fun fetchCurrentAddress(
10+
@Query("x") longitude: String,
11+
@Query("y") latitude: String,
12+
): Coord2AddressResponse
13+
}

0 commit comments

Comments
 (0)