|
| 1 | +package org.yapp.infra.external.aladin.helper |
| 2 | + |
| 3 | +import mu.KotlinLogging |
| 4 | +import org.yapp.globalutils.annotation.Helper |
| 5 | +import org.yapp.infra.external.aladin.AladinApi |
| 6 | +import org.yapp.infra.external.aladin.response.AladinBookDetailResponse |
| 7 | +import org.yapp.infra.external.aladin.response.AladinSearchResponse |
| 8 | + |
| 9 | + |
| 10 | +@Helper |
| 11 | +class AladinApiHelper( |
| 12 | + private val aladinApi: AladinApi |
| 13 | +) { |
| 14 | + private val log = KotlinLogging.logger {} |
| 15 | + |
| 16 | + fun searchBooks(query: String, params: Map<String, Any>): AladinSearchResponse { |
| 17 | + return aladinApi.searchBooks(query, params) |
| 18 | + .onSuccess { response -> |
| 19 | + log.info("Aladin search successful for query: '$query', total results: ${response.totalResults}") |
| 20 | + } |
| 21 | + .getOrElse { exception -> |
| 22 | + log.error("Failed to call Aladin search API for query: '$query'", exception) |
| 23 | + // TODO: 특정 비즈니스 예외로 맵핑하거나, 공통 예외 처리 계층에서 처리하도록 리팩토링 |
| 24 | + throw IllegalStateException( |
| 25 | + "Failed to retrieve search results from Aladin API: ${exception.message}", |
| 26 | + exception |
| 27 | + ) |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + fun lookupBook(itemId: String, itemIdType: String, optResult: List<String>?): AladinBookDetailResponse { |
| 32 | + // AladinApi로 optResult를 직접 전달 |
| 33 | + return aladinApi.lookupBook(itemId, itemIdType, optResult) |
| 34 | + .onSuccess { response -> |
| 35 | + log.info("Aladin lookup successful for itemId: '$itemId', title: ${response.item?.firstOrNull()?.title}") |
| 36 | + } |
| 37 | + .getOrElse { exception -> |
| 38 | + log.error("Failed to call Aladin lookup API for itemId: '$itemId'", exception) |
| 39 | + throw IllegalStateException( |
| 40 | + "Failed to retrieve book details from Aladin API: ${exception.message}", |
| 41 | + exception |
| 42 | + ) |
| 43 | + } |
| 44 | + } |
| 45 | +} |
0 commit comments