如何为HTTP4编写惯用的Scala代码

oxiaedzo  于 2022-11-09  发布在  Scala
关注(0)|答案(1)|浏览(131)

我有点纠结于如何将命令式样式转换为函数式样式。
在命令式Web请求中,我习惯于说类似以下psudo代码的内容:

public Response controllerAction(Request request) {
    val (req, parserErrors) = parser.parseRequest(request);
    if (parserErrors.any()) {
        return FourHundredError(parserErrors);
    }

    val businessErrors = model.validate(req);
    if (businessErrors.any()){
        return FourOhFour(businessErrors);
    }

    val (response, errorsWithOurStuff) = model.doBusinessLogicStuff(req);
    if (errorsWithOurStuff.any()) {
        return FiveHundredError(errorsWithOurStuff);
    }

    return OK(response)
}

我正在尝试使用HTTP4将其转换为函数样式。

def businessRoutes[F[_]: Sync](BL: BusinessLogic[F]): HttpRoutes[F] = {
    val dsl = new Http4sDsl[F]{}
    import dsl._
    HttpRoutes.of[F] {
      case req @ POST -> Root / "sms" =>
        for {
          request <- req.as[BL.BuisnessRequest]
          requestErrors <- BL.validateRequest(request)
          response <- if (requestErrors.isEmpty) {
            BL.processRequest(request) match {
              case Failure(e) => InternalServerError(e)
              case Success(response) => Ok(response)
            }
          } else {
            BadRequest(requestErrors)
          }
        } yield response
    }
  }

上面的代码看起来只是...对我不好,我不知道怎么让它变得更好。我的目标是保留这里包含的所有http样式抽象,因为我不想将HTTP4或Circe向下泄漏到业务层。我觉得我得到了一个for,然后是一个if,然后是一个match,所有的响应都乱七八糟地放在一起,没有顺序。为了理解这里的代码,我写得很努力,我希望一些ScalaMaven能告诉我如何清理这些代码,并使其具有可读性。

xsuvu9jc

xsuvu9jc1#

IMHO,问题的根源在于您建模数据的方式;主要是使用validateRequest
永远记住,parse, don't validate
此外,我会使用如下所示的主处理程序来处理非类型化错误:

import cats.syntax.all._
import io.circe.{Error => CirceError}

object model {
  final case class RawRequest(...)
  final case class BuisnessRequest(...)
  final case class BuisnessResponse(...)
}

object errors {
  // Depending on how you end up using those,
  // it may be good to use scala.util.control.NoStackTrace with these.
  // They may also be case classes to hold some context.
  final case object ValidationError extends Throwable
  final case object BusinessError extends Throwable
}

trait BusinessLogic {
  def validateRequest(rawRequest: RawRequest): IO[BusinessRequets]
  def processRequest(request: BusinessRequets): IO[BuisnessResponse]
}

final class HttpLayer(bl: BusinessLogic) extends Http4sDsl[IO] {
  private final val errorHanlder: PartialFunction[Throwable, IO[Response[IO]] = {
    case circeError: CirceError =>
      BadRequest(...)

    case ValidationError =>
      NotFound(...)

    case BusinessError =>
      InternalServerError(...)
  }

  val routes: HttpRoutes[IO] = HttpRoutes[F] {
     case req @ POST -> Root / "sms" =>
       req
        .as[RawRequest] // This may fail with CirceError.
        .flatMap(bl.validateRequest) // This may fail with ValidationError.
        .flatMap(bl.processRequest) // This may fail with BusinessError.
        .redeemWith(recover = errorHandler, response => Ok(response))
  }
}

为了简单起见,我在这里使用了混凝土IO,如果愿意,您可以使用F[_]

相关问题