summaryrefslogtreecommitdiffstats
path: root/sources/hv-collector-utils/src
diff options
context:
space:
mode:
authorIzabela Zawadzka <izabela.zawadzka@nokia.com>2019-03-20 14:59:24 +0100
committerIzabela Zawadzka <izabela.zawadzka@nokia.com>2019-03-28 11:53:11 +0100
commit75b95caba1ec7e126683be1df5928a380174d428 (patch)
tree500c5262fb7c78eb39e458a09bc4a7edd7a01678 /sources/hv-collector-utils/src
parent58ae1a831a6fe85abda8c4d866e5170c70499ac1 (diff)
Remove IO monad usage from simulators
Change-Id: I1c470777b91230f4a44a4960ca534e4b20c1ac43 Issue-ID: DCAEGEN2-1372 Signed-off-by: Izabela Zawadzka <izabela.zawadzka@nokia.com>
Diffstat (limited to 'sources/hv-collector-utils/src')
-rw-r--r--sources/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/http/netty.kt55
1 files changed, 29 insertions, 26 deletions
diff --git a/sources/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/http/netty.kt b/sources/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/http/netty.kt
index cf338a70..f133d630 100644
--- a/sources/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/http/netty.kt
+++ b/sources/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/http/netty.kt
@@ -20,50 +20,53 @@
package org.onap.dcae.collectors.veshv.utils.http
import arrow.core.Either
-import arrow.effects.IO
import org.onap.dcae.collectors.veshv.utils.logging.Logger
import reactor.core.publisher.Mono
+import reactor.core.publisher.toMono
import reactor.netty.NettyOutbound
import reactor.netty.http.server.HttpServerResponse
import javax.json.Json
private val logger = Logger("org.onap.dcae.collectors.veshv.utils.http.netty")
-fun HttpServerResponse.sendOrError(action: IO<Unit>): NettyOutbound =
- sendAndHandleErrors(action.map {
- Response(
- HttpStatus.OK,
- Content(
- ContentType.JSON,
- Json.createObjectBuilder().add("response", "Request accepted").build()
+fun HttpServerResponse.sendOrError(action: ()->Unit) = sendAndHandleErrors(
+ Mono
+ .fromSupplier(action)
+ .map {
+ Response(
+ HttpStatus.OK,
+ Content(
+ ContentType.JSON,
+ Json.createObjectBuilder().add("response", "Request accepted").build()
+ )
)
- )
- })
-
+ }
+)
-fun HttpServerResponse.sendAndHandleErrors(response: IO<Response>): NettyOutbound =
- response.attempt().unsafeRunSync().fold(
- { err ->
- logger.withWarn { log("Error occurred. Sending .", err) }
- val message = err.message
- sendResponse(errorResponse(message))
- },
- {
- sendResponse(it)
+fun HttpServerResponse.sendAndHandleErrors(response: Mono<Response>) =
+ response
+ .onErrorResume {
+ logger.withWarn { log("Error occurred. Sending .", it) }
+ errorResponse(it.localizedMessage).toMono()
+ }
+ .flatMap {
+ sendResponse(it).then()
}
- )
-fun <A> HttpServerResponse.sendEitherErrorOrResponse(response: Either<A, Response>): NettyOutbound =
+fun <A> HttpServerResponse.sendEitherErrorOrResponse(response: Either<A, Response>) =
when (response) {
- is Either.Left -> sendResponse(errorResponse(response.a.toString()))
- is Either.Right -> sendAndHandleErrors(IO.just(response.b))
+ is Either.Left -> sendResponse(errorResponse(response.a.toString())).then()
+ is Either.Right -> sendAndHandleErrors(Mono.just(response.b))
}
-private fun HttpServerResponse.sendResponse(response: Response): NettyOutbound {
+
+fun HttpServerResponse.sendResponse(response: Response): NettyOutbound {
val respWithStatus = status(response.status.number)
val responseContent = response.content
- return respWithStatus.sendString(Mono.just(responseContent.serializer.run { responseContent.value.show() }))
+ return respWithStatus.sendString(
+ Mono.just(responseContent.serializer.run { responseContent.value.show() })
+ )
}
private fun errorResponse(message: String?): Response =