0

I have created two similar methods:

override def getUsers(organization: String, params: String): F[Either[CodecException, List[Users]]] = for {
    resp <- getUsersFromClient(organization, params)
    result <- Sync[F].delay(resp)
  } yield result

  override def getAnimals(url: String, params: String): F[Either[CodecException, List[Animal]]] = for {
    resp <- getAnimalsFromClient(url, params)
    result <- Sync[F].delay(resp)
  } yield result

And I think I should refactor them in some way to have only one method or something by implicits, but I do not have any idea how it should be done in a right way to keep functional style. Could you help me with that?

EDIT:

def getUsersFromClient(param1: String, params: String): F[HttpResponse] = Hammock.getWithOpts(...)

def getAnimals(param1: String, params: String): F[HttpResponse] = Hammock.getWithOpts(...)

This method also are similar and return F[HttpResponse]

Developus
  • 1,400
  • 2
  • 14
  • 50

1 Answers1

0

It's hard to say because you didn't provide your getUsersFromClient, getAnimalsFromClient, decodeResponseEntity, decodeResponseContributor...

For examle you can try to make your method generic and higher-order

def get[A](s: String, params: String, getFromClient: (String, String) => ..., decodeResponse: ... => ...): F[Either[CodecException, List[A]]] = for {
  resp <- getFromClient(s, params)
  result <- Sync[F].delay(decodeResponse(resp), CodecException.withMessage("Something goes wrong while decoding response."))
} yield result
Dmytro Mitin
  • 48,194
  • 3
  • 28
  • 66
  • I tried do this but in compilation time I got an error in result list `type mismatch; found : hammock.HttpResponse required: Either[hammock.CodecException,List[A]] } yield result` because `getFromClient` is a method which return `F[HttpResonse]` – Developus Nov 11 '19 at 19:50