Skip to content

Instantly share code, notes, and snippets.

@m-debugger
Created October 31, 2018 22:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save m-debugger/fd653f13801f8489de3499cb3879e7a2 to your computer and use it in GitHub Desktop.
Save m-debugger/fd653f13801f8489de3499cb3879e7a2 to your computer and use it in GitHub Desktop.
when (info) {
null -> {
return unauthorized()
}
else -> {
val validInfo = validator.validateInfo(info)
when (validInfo) {
is Either.Right -> {
if (!validInfo.b.authorised) {
// authorised is false so return unauthorised
return unauthorized()
}
// authorised is true, so use the authorised token to create a certificate
val authorisedInfo = validInfo.b
val productService = productServiceBuilder.buildProductService(authorisedInfo)
// TODO add when (productService) here
//
return when (productService) {
is Either.Right -> {
val product = productService.b.getProduct(
authorisedInfo.userId!!)
APIGatewayProxyResponseEvent()
.withBody(jacksonObjectMapper().writeValueAsString(product))
.withStatusCode(200)
}
is Either.Left -> {
systemError()
}
}
}
is Either.Left -> {
return systemError()
}
}
}
}
@Takhion
Copy link

Takhion commented Nov 2, 2018

what about:

(info ?: return unauthorized())
    .let(validator::validateInfo)
    .flatMap { validatedInfo ->
        val authorisedInfo = validatedInfo.takeIf { it.authorised } ?: return unauthorized()

        productServiceBuilder.buildProductService(authorisedInfo)
            .map { productService ->
                val product = productService.getProduct(authorisedInfo.userId!!)

                APIGatewayProxyResponseEvent()
                    .withBody(jacksonObjectMapper().writeValueAsString(product))
                    .withStatusCode(200)
            }
    }
    .getOrElse(::systemError)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment