Created
May 26, 2020 10:10
-
-
Save last-ent/ec183a09b5fa496cb7421b59fbce057b to your computer and use it in GitHub Desktop.
This file contains 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
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