I am currently Implementing business logic code which is dependent on the custom scala library which has a lot of impure functions and we have a lot of code with is dependent on this library and cannot refactor as it will affect others. So I am trying to write my scala code as pure as possible and need suggestions for few use cases.
I have an LDAP utility library that does the following Authenticate based on username and password and returns Boolean, However, if there is a Connection exception or user not found it directly throws an exception. Reading and updating data based on UUID and parameters like first name and last name does pretty much the same thing My approach for the authentication use case is below
def authenticateToUpdate(userName:String,password:String):Option[Boolean] =
Some(ldapService.authenticate(username,password))
// or using either type to get the message as well
def authenticateToUpdate(userName:String,password:String):Either[String,Boolean]=
(ldapService.authenticate(username,password)) match {
case true => Right(true)
case false => Left("Validation failed, Invalid Password")
case exception: Exception => Left(exception.toString)
}
//##for Some type
authenticateToUpdate(userName,password).fold(InvalidPassword)(updateOrder(orderId,userName))
//##for Either type
// Looking into how to implement
So I want to write my code as functional and close to pure function as possible need help and suggestions on how can I approach such scenario to write functional scala code.