add DeviceConfigurationService

This commit is contained in:
2024-11-30 18:37:00 +01:00
parent 198854de78
commit 893423c586

View File

@@ -0,0 +1,34 @@
package ltd.hlaeja.service
import java.util.UUID
import ltd.hlaeja.library.deviceConfiguration.Node
import ltd.hlaeja.property.DeviceConfigurationProperty
import mu.KotlinLogging
import org.springframework.http.HttpStatus.NOT_FOUND
import org.springframework.http.HttpStatus.NO_CONTENT
import org.springframework.http.HttpStatus.REQUEST_TIMEOUT
import org.springframework.stereotype.Service
import org.springframework.web.reactive.function.client.WebClient
import org.springframework.web.reactive.function.client.awaitBodyOrNull
import org.springframework.web.server.ResponseStatusException
private val log = KotlinLogging.logger {}
@Service
class DeviceConfigurationService(
private val webClient: WebClient,
private val deviceConfigurationProperty: DeviceConfigurationProperty,
) {
suspend fun getConfiguration(
node: UUID,
): Node.Response = webClient.get()
.uri("${deviceConfigurationProperty.url}/node-$node".also(::logCall))
.retrieve()
.onStatus(NOT_FOUND::equals) { throw ResponseStatusException(NO_CONTENT) }
.awaitBodyOrNull<Node.Response>() ?: throw ResponseStatusException(REQUEST_TIMEOUT)
private fun logCall(url: String) {
log.debug("calling: {}", url)
}
}