From 5b65630556682e822ef92ddc95c5e58fe551b635 Mon Sep 17 00:00:00 2001 From: usbharu <64310155+usbharu@users.noreply.github.com> Date: Thu, 12 Oct 2023 16:20:18 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=9E=8B=E5=AE=89=E5=85=A8=E3=81=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ap/resource/APResourceResolveService.kt | 12 +++++++-- .../resource/APResourceResolveServiceImpl.kt | 25 +++++++++---------- 2 files changed, 22 insertions(+), 15 deletions(-) diff --git a/src/main/kotlin/dev/usbharu/hideout/service/ap/resource/APResourceResolveService.kt b/src/main/kotlin/dev/usbharu/hideout/service/ap/resource/APResourceResolveService.kt index 20459fba..986a958e 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/ap/resource/APResourceResolveService.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/ap/resource/APResourceResolveService.kt @@ -4,6 +4,14 @@ import dev.usbharu.hideout.domain.model.ap.Object import dev.usbharu.hideout.domain.model.hideout.entity.User interface APResourceResolveService { - suspend fun resolve(url: String, singerId: Long?): Object - suspend fun resolve(url: String, singer: User?): Object + suspend fun resolve(url: String, clazz: Class, singer: User?): T + suspend fun resolve(url: String, clazz: Class, singerId: Long?): T +} + +suspend inline fun APResourceResolveService.resolve(url: String, singer: User?): T { + return resolve(url, T::class.java, singer) +} + +suspend inline fun APResourceResolveService.resolve(url: String, singerId: Long?): T { + return resolve(url, T::class.java, singerId) } diff --git a/src/main/kotlin/dev/usbharu/hideout/service/ap/resource/APResourceResolveServiceImpl.kt b/src/main/kotlin/dev/usbharu/hideout/service/ap/resource/APResourceResolveServiceImpl.kt index 3402aff4..acf5e822 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/ap/resource/APResourceResolveServiceImpl.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/ap/resource/APResourceResolveServiceImpl.kt @@ -1,7 +1,6 @@ package dev.usbharu.hideout.service.ap.resource import com.fasterxml.jackson.databind.ObjectMapper -import com.fasterxml.jackson.module.kotlin.readValue import dev.usbharu.hideout.domain.model.ap.Object import dev.usbharu.hideout.domain.model.hideout.entity.User import dev.usbharu.hideout.repository.UserRepository @@ -20,35 +19,35 @@ class APResourceResolveServiceImpl( ) : APResourceResolveService { - override suspend fun resolve(url: String, singerId: Long?): Object { - return internalResolve(url, singerId) + override suspend fun resolve(url: String, clazz: Class, singerId: Long?): T { + return internalResolve(url, singerId, clazz) } - override suspend fun resolve(url: String, singer: User?): Object { - return internalResolve(url, singer) + override suspend fun resolve(url: String, clazz: Class, singer: User?): T { + return internalResolve(url, singer, clazz) } - private suspend fun internalResolve(url: String, singerId: Long?): Object { + private suspend fun internalResolve(url: String, singerId: Long?, clazz: Class): T { val key = genCacheKey(url, singerId) cacheManager.putCache(key) { - runResolve(url, singerId?.let { userRepository.findById(it) }) + runResolve(url, singerId?.let { userRepository.findById(it) }, clazz) } - return cacheManager.getOrWait(key) + return cacheManager.getOrWait(key) as T } - private suspend fun internalResolve(url: String, singer: User?): Object { + private suspend fun internalResolve(url: String, singer: User?, clazz: Class): T { val key = genCacheKey(url, singer?.id) cacheManager.putCache(key) { - runResolve(url, singer) + runResolve(url, singer, clazz) } - return cacheManager.getOrWait(key) + return cacheManager.getOrWait(key) as T } - private suspend fun runResolve(url: String, singer: User?): Object { + private suspend fun runResolve(url: String, singer: User?, clazz: Class): Object { val bodyAsText = httpClient.get(url).bodyAsText() - return objectMapper.readValue(bodyAsText) + return objectMapper.readValue(bodyAsText, clazz) } private fun genCacheKey(url: String, singerId: Long?): String {