Add device registry
- add RegistryController - add DeviceRegistryService - add Helper.kt - add device registry property - set up registry property
This commit is contained in:
@@ -1,8 +1,13 @@
|
||||
package ltd.hlaeja
|
||||
|
||||
import ltd.hlaeja.property.DeviceRegistryProperty
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties
|
||||
import org.springframework.boot.runApplication
|
||||
|
||||
@EnableConfigurationProperties(
|
||||
DeviceRegistryProperty::class,
|
||||
)
|
||||
@SpringBootApplication
|
||||
class Application
|
||||
|
||||
|
||||
18
src/main/kotlin/ltd/hlaeja/controller/RegistryController.kt
Normal file
18
src/main/kotlin/ltd/hlaeja/controller/RegistryController.kt
Normal file
@@ -0,0 +1,18 @@
|
||||
package ltd.hlaeja.controller
|
||||
|
||||
import ltd.hlaeja.library.deviceRegistry.Device
|
||||
import ltd.hlaeja.service.DeviceRegistryService
|
||||
import org.springframework.web.bind.annotation.PostMapping
|
||||
import org.springframework.web.bind.annotation.RequestBody
|
||||
import org.springframework.web.bind.annotation.RestController
|
||||
|
||||
@RestController
|
||||
class RegistryController(
|
||||
private val registryService: DeviceRegistryService,
|
||||
) {
|
||||
|
||||
@PostMapping("/register")
|
||||
suspend fun addDevice(
|
||||
@RequestBody request: Device.Request,
|
||||
): Device.Response = registryService.registerDevice(request)
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package ltd.hlaeja.property
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties
|
||||
|
||||
@ConfigurationProperties(prefix = "device-registry")
|
||||
data class DeviceRegistryProperty(
|
||||
val url: String,
|
||||
)
|
||||
24
src/main/kotlin/ltd/hlaeja/service/DeviceRegistryService.kt
Normal file
24
src/main/kotlin/ltd/hlaeja/service/DeviceRegistryService.kt
Normal file
@@ -0,0 +1,24 @@
|
||||
package ltd.hlaeja.service
|
||||
|
||||
import ltd.hlaeja.library.deviceRegistry.Device
|
||||
import ltd.hlaeja.util.logCall
|
||||
import ltd.hlaeja.property.DeviceRegistryProperty
|
||||
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
|
||||
|
||||
@Service
|
||||
class DeviceRegistryService(
|
||||
private val webClient: WebClient,
|
||||
private val deviceRegistryProperty: DeviceRegistryProperty,
|
||||
) {
|
||||
suspend fun registerDevice(
|
||||
request: Device.Request,
|
||||
): Device.Response = webClient.post()
|
||||
.uri("${deviceRegistryProperty.url}/device".also(::logCall))
|
||||
.bodyValue(request)
|
||||
.retrieve()
|
||||
.awaitBodyOrNull<Device.Response>() ?: throw ResponseStatusException(REQUEST_TIMEOUT)
|
||||
}
|
||||
9
src/main/kotlin/ltd/hlaeja/util/Helper.kt
Normal file
9
src/main/kotlin/ltd/hlaeja/util/Helper.kt
Normal file
@@ -0,0 +1,9 @@
|
||||
package ltd.hlaeja.util
|
||||
|
||||
import mu.KotlinLogging
|
||||
|
||||
private val log = KotlinLogging.logger {}
|
||||
|
||||
fun logCall(url: String) {
|
||||
log.debug("calling: {}", url)
|
||||
}
|
||||
Reference in New Issue
Block a user