-
-
Save pakoito/f811e16628b2b6f6060dc34db4274cf4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | |
} | |
} | |
} | |
} |
Using comprehensions:
Either.monadError<Throwable>().bindingCatch {
val goodInfo = info.toOption().toEither { unauthorized() }.bind()
val validInfo = validator.validateInfo(goodInfo).mapLeft { systemError() }.bind()
val result = if (!validInfo.authorised) {
unauthorized().left()
} else {
catch {
val productService = productServiceBuilder.buildProductService(validInfo).bind()
val product = productService.getProduct(validInfo.userId!!).bind()
APIGatewayProxyResponseEvent()
.withBody(jacksonObjectMapper().writeValueAsString(product))
.withStatusCode(200)
}.mapLeft {
systemError()
}
}
result.bind()
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Using fluent chains: