Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import cats.effect.IO
class Server(http: Http, database: Database) {
def process(request: Request): IO[Response] = {
val respF =
for {
creds <- request.getCredentials()
authorizedCreds <- authenticateUser(creds)
userInfo <- getUserInfo(authorizedCreds)
successfulResponse <- toSuccessfulResponse(userInfo)
} yield successfulResponse
respF.handleErrorWith(toFailedResponse)
}
def authenticateUser(creds: UserCredentials): IO[AuthCredentials] = ???
def getUserInfo(authCreds: AuthCredentials): IO[UserInfo] = ???
def toFailedResponse(err: Throwable): IO[Response] = ???
def toSuccessfulResponse(userInfo: UserInfo): IO[UserInfoResponse] = ???
}
trait Request {
def getCredentials(): IO[UserCredentials]
}
trait Http {
def post(userCreds: UserCredentials): IO[AuthCredentials]
}
trait Database{
def queryUser(userName: String): IO[UserInfo]
}
final case class UserCredentials(userId: String, password: String)
final case class AuthCredentials(userId: String, userName: String)
final case class UserInfo()
sealed trait Response
final case class UserInfoResponse(userInfo: UserInfo) extends Response
sealed trait CredentialErrorResponse
sealed trait AuthResponse
sealed trait AuthErrorResponse extends AuthResponse
sealed trait DBResponse
sealed trait DBErrorResponse extends DBResponse
final case class CredentialsMissing(msg: String) extends RuntimeException(msg) with CredentialErrorResponse
final case class UnauthorizedUser(msg: String) extends RuntimeException(msg) with CredentialErrorResponse
final case class NotFound(msg: String) extends RuntimeException(msg) with CredentialErrorResponse
final case class Forbidden(msg: String) extends RuntimeException(msg) with CredentialErrorResponse
final case class InternalServerError(msg: String) extends RuntimeException(msg) with CredentialErrorResponse
final case class AuthCredentialsResponse(authCredentials: AuthCredentials) extends AuthResponse
final case class UnauthorizedAuthUser(msg: String) extends RuntimeException(msg: String) with AuthErrorResponse
final case class AuthUserNotFound(msg: String) extends RuntimeException(msg: String) with AuthErrorResponse
final case class AuthServerError(msg: String) extends RuntimeException(msg: String) with AuthErrorResponse
final case class UserInfoFound() extends DBResponse
final case class UserInfoNotFound() extends DBResponse
final case class DBError(msg: String) extends RuntimeException(msg) with DBErrorResponse
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.