공부/이펙티브코틀린

아이템 29 - 외부 API 를 랩(warp)해서 사용하라

띵커베르 2024. 6. 23. 11:41
728x90
  • 불안한 API를 로직과 직접 결합시키지 않는 것이 좋다
/**
 * 외부 API를 랩핑하지 않은 코드
 * WeatherService 클래스에서 직접 외부 API를 호출합니다.
 * API 응답을 받아 바로 처리합니다.
 */
class WeatherServiceNotWrapped {
    fun getWeather(location: String): String {
        val response = ExternalWeatherApi.getWeather(location)
        return response ?: throw RuntimeException("Failed to fetch weather")
    }
}

/**
 * 외부 API 클라이언트를 랩핑한 클래스
 * WeatherApiWrapper 클래스를 만들어 외부 API 호출을 감쌉니다.
 * WeatherService 클래스는 WeatherApiWrapper 를 통해 간접적으로 외부 API 를 호출합니다.
 * WeatherApiWrapper 클래스는 예외 처리를 통해 실패 상황을 관리합니다.
 */
class WeatherApiWrapper {
    fun getWeather(location: String): String {
        val response = ExternalWeatherApi.getWeather(location)
        return response ?: throw RuntimeException("Failed to fetch weather")
    }
}


// 서비스 클래스
class WeatherService(private val weatherApiWrapper: WeatherApiWrapper) {
    fun getWeather(location: String): String {
        return weatherApiWrapper.getWeather(location)
    }
}

fun main() {
    val weatherApiWrapper = WeatherApiWrapper()
    val weatherService = WeatherService(weatherApiWrapper)

    println(weatherService.getWeather("Seoul"))
}

object ExternalWeatherApi {
    fun getWeather(location: String): String? {
        return "Sunny"
    }

}

 

  • 테스트 코드때 목킹하기는 편하고, 웬만하면 래핑해서 사용중임~
728x90