Coors To Address - Picplz/picplz-aos GitHub Wiki
์ง๋์ ์ค์ ์ขํ๋ฅผ ํ์ํด์ ํด๋น ์ขํ์ ์ฃผ์๋ฅผ rest api๋ฅผ ์ด์ฉํด์ ๊ตฌํ๋ ๋์
๊ณต์ ์์ฒญ ํ์์ ๋ง์ถฐ data class๋ก ์ ์
data class KaKaoAddressRequest(
val x: String,
val y: String
)
๊ณต์ response ํ์์ ๋ง์ถฐ ์ค์ฒฉ data class๋ก ์ ์
data class KaKaoAddressResponse(
val meta: Meta,
val documents: List<Document>
) {
data class Meta(
val total_count: Int
)
data class Document(
val address: Address,
val road_address: RoadAddress?
)
data class Address(
val address_name: String,
val region_1depth_name: String,
val region_2depth_name: String,
val region_3depth_name: String,
val mountain_yn: String,
val main_address_no: String,
val sub_address_no: String
)
data class RoadAddress(
val address_name: String,
val region_1depth_name: String,
val region_2depth_name: String,
val region_3depth_name: String,
val road_name: String,
val underground_yn: String,
val main_building_no: String,
val sub_building_no: String,
val building_name: String,
val zone_no: String
)
}
data class์ ์ค์ฒฉ ํด๋์ค๋ json์ ๊ณ์ธต๊ตฌ์กฐ๋ฅผ ํํํ๊ธฐ ์ข๋ค
- ์ด๋ json ํ์๊ณผ kotlin์ ๊ฐ์ฒด์์ ์ง๋ ฌํ/์ญ์ง๋ ฌํ๋ retrofit์ ํตํด ์ด๋ค์ง๋ค
package com.hm.picplz.data.service
import com.hm.picplz.data.model.KaKaoAddressResponse
import retrofit2.http.GET
import retrofit2.http.Header
import retrofit2.http.Query
interface KakaoMapService {
@GET("v2/local/geo/coord2address.json")
suspend fun getAddressFromCoordsService(
@Header("Authorization") authorization: String,
@Query("x") x: String,
@Query("y") y: String
): KaKaoAddressResponse
}
HTTP Method ๋ฐ ์๋ํฌ์ธํธ ์ง์ : ์ด๋
ธํ
์ด์
(@
)์ ์ด์ฉ
ํ๋ผ๋ฏธํฐ ์ด๋
ธํ
์ด์
์ ํตํด ๊ฐ๊ฐ @Header
, @Query
, @Path
, Body
์ ์ ๊ฐ๋ฅ
//์์
interface ApiService {
@GET("users")
suspend fun getUser(
@Header("Authorization") token: String, // ํค๋ ํ๋ผ๋ฏธํฐ
@Query("name") name: String, // URL ์ฟผ๋ฆฌ ํ๋ผ๋ฏธํฐ (?name=๊ฐ)
@Path("id") id: String, // URL ๊ฒฝ๋ก ํ๋ผ๋ฏธํฐ
@Body userInfo: UserInfo // ์์ฒญ ๋ฐ๋
)
}
// ์นด์นด์ค ์ขํ ๊ฒ์ api
interface KakaoMapService {
@GET("v2/local/geo/coord2address.json")
suspend fun getAddressFromCoordsService(
@Header("Authorization") authorization: String,
@Query("x") x: String,
@Query("y") y: String
): KaKaoAddressResponse
}
ํค๋ ๋ฐ ์ฟผ๋ฆฌ ์ ๋ ฅ interface๋ฅผ ์ ์ํด์ค๋๋ค
KakaoMapSource
ํ์ผ์์ retrofit ์ค์ ๋ฐ ์ธ์คํด์ค ์์ฑ์ ์งํํฉ๋๋ค
private val retrofit: Retrofit = Retrofit.Builder()
.baseUrl("https://dapi.kakao.com/")
.addConverterFactory(GsonConverterFactory.create())
.build()
.baseUrl
: api์ base url์ ์ค์ ํ ์ ์์ต๋๋ค
.addConverterFactory(GsonConverterFactory.create())
JSON ๋ณํ๊ธฐ๋ฅผ ์ค์ ํฉ๋๋ค
private val kakaoMapService: KakaoMapService = retrofit.create(KakaoMapService::class.java)
์ ์ํ ์ธํฐํ์ด์ค๊ณผ ์ค์ ์ ๋ง์ถฐ์ retrofit์ด api๋ฅผ ์์ฒญํ๋ ํจ์๋ฅผ ๊ตฌํ
๊ตฌํ์ฒด๋ฅผ ๋ฐํ์ผ๋ก runCatching
๋ฐฉ์์ผ๋ก api๋ฅผ ํธ์ถ
suspend fun getAddressFromCoords(request: KaKaoAddressRequest): Result<KaKaoAddressResponse> =
runCatching {
kakaoMapService.getAddressFromCoordsService(
authorization = "KakaoAK ${BuildConfig.KAKAO_REST_API_KEY}",
x = request.x,
y = request.y
)
}
์ฝํ๋ฆฐ์ ์์ธ์ฒ๋ฆฌ ๋ฐฉ์์ผ๋ก .onSuccess
์ .onFailure
๋ก ์์ธ์ฒ๋ฆฌ๋ฅผ ํ ์ ์์ต๋๋ค.
// try-catch ๋ฐฉ์
try {
// ์๋ฌ ๋ฐ์ ๊ฐ๋ฅํ ์ฝ๋
} catch (e: Exception) {
// ์๋ฌ ์ฒ๋ฆฌ
}
// runCatching ๋ฐฉ์
runCatching {
// ์๋ฌ ๋ฐ์ ๊ฐ๋ฅํ ์ฝ๋
}.onSuccess { result ->
// ์ฑ๊ณต ์ฒ๋ฆฌ
}.onFailure { exception ->
// ์๋ฌ ์ฒ๋ฆฌ
}
runCatching {
// ์ํํ ์์
}.onSuccess { response ->
// ์ฑ๊ณต ์ ์ฒ๋ฆฌ
}.onFailure { exception ->
// ์คํจ ์ ์ฒ๋ฆฌ
}.map { response ->
// ์ฑ๊ณต ๊ฒฐ๊ณผ๋ฅผ ๋ณํ
}.mapCatching { response ->
// ์์ธ๊ฐ ๋ฐ์ํ ์ ์๋ ๋ณํ
}.recover { exception ->
// ์คํจ๋ฅผ ๋ณต๊ตฌ
}.recoverCatching { exception ->
// ์์ธ๊ฐ ๋ฐ์ํ ์ ์๋ ๋ณต๊ตฌ
}.getOrNull() // null ๋๋ ๊ฒฐ๊ณผ
.getOrDefault(defaultValue) // ๊ธฐ๋ณธ๊ฐ ๋๋ ๊ฒฐ๊ณผ
.getOrElse { exception -> // ์ง์ ์ฒ๋ฆฌ
// ์์ธ ์ฒ๋ฆฌ ๋ก์ง
}
์์ ๊ฐ์ด ๋ค์ํ ์ฒ๋ฆฌ๊ฐ ๊ฐ๋ฅํฉ๋๋ค.
fun handleIntent(intent: SearchPhotographerIntent) {
when (intent) {
is SearchPhotographerIntent.SetAddress -> {
_state.update { it.copy(address = intent.address) }
}
is SearchPhotographerIntent.GetAddress -> {
viewModelScope.launch {
kakaoSource.getAddressFromCoords(KaKaoAddressRequest(intent.Coords.longitude.toString(), intent.Coords.latitude.toString()))
.onSuccess { response ->
val twoDepthRegion = response.documents.firstOrNull()?.address?.region_2depth_name ?: ""
val threeDepthRegion = response.documents.firstOrNull()?.address?.region_3depth_name ?: ""
val address = "$twoDepthRegion $threeDepthRegion"
handleIntent(SearchPhotographerIntent.SetAddress(address))
}
.onFailure { error ->
Log.e("kakaoMapAddressSearch", "์ขํ ๊ฒ์ ์คํจ : ", error)
}
}
}
...
}
}