def registerIntegration(req: Request[IO], auth: String) = {
// First check that we have the correct payload
req
.as[RegisterIntegrationPayload]
.option
.flatMap({
case None => BadRequest("Malformed request body")
case Some(payload) => {
// Check that the user referenced exists
repository
.getUser(payload.userId)
.flatMap({
case None => BadRequest("No user found")
case Some(_) => {
// Unwrap the lightning auth header
authStringToLightningAuth(auth) match {
case Left(st) => BadRequest(st)
case Right((user, pass)) => {
// Make sure we have the correct authentication
LightningAdServer
.getAccount(user, pass)
.flatMap({
case Left(error) =>
InternalServerError(
s"Lightning ad server failed: $error"
)
case Right(accountName) => {
// Mark that the user is lightning registered
repository
.markUserLightningRegistered(payload.userId)
.flatMap(registered =>
if (registered)
Ok("Registered for lightning integration")
else
InternalServerError(
"Failed to register for lightning integration"
)
)
}
})
}
}
}
})
}
})
}
EitherT
or OptionT
Eithers
into Option
or vice versa)
foo.flatMap(x => x.bar.flatMap(y => ???))
is the same as foo.flatMap(x => x.bar).flatMap(y => ???)
flatMap
calls.
for
syntax may help with that.
I'm trying to use parReduceMapA on a List[Set[MyType]] where MyType has a Semigroup instance defined for it, but I'm getting the error:could not find implicit value for parameter T: cats.Reducible[List]
The code is using tagless final, so it looks kinda like this
object MyService {
def impl[F[_]: Spawn: Parallel]: MyService[F] = new MyService[F] {
private def otherFunc(param1: String, param2: Set[MyType]): List[Set[MyType]] = ???
def myFunc(param1: String, param2: Set[MyType]): F[List[Set[MyType]]] =
for {
myVal <- otherFunc(param1, param2).parReduceMapA(Applicative[F].pure)
... etc