scala 图案类型与预期类型不兼容?

xvw2m8pv  于 2023-01-13  发布在  Scala
关注(0)|答案(1)|浏览(139)

我正在尝试为特定路径创建发布端点,但收到错误消息
模式类型与找到的预期类型ContextRequest[F,A]不兼容,需要:请求[F]

case _ @ POST -> Root / "batch-notify" as _ =>
      handler.handle("create.notifications.batchNotify") {
        for {
          _ <- log.info("Running batch notification job") *> Sync[F].pure(())
          r <- batchingService.createNotifications.flatMap(_ => Ok())
        } yield r
      }.fireAndForget.flatMap(_ => Accepted())

我对Scala还是个新手,我尝试过修复它,但毫无进展,有人能帮忙吗?

enxuqcxy

enxuqcxy1#

_ @ POST -> Root / "batch-notify" as _

akka

POST -> Root / "batch-notify" as _

akka

POST -> Root / "batch-notify" as user

是用于AuthedRoutesAuthedRequest
https://http4s.org/v1/docs/auth.html
如果您将其放入模式匹配中,其中Request预期为HttpRoutes(即没有身份验证)
https://http4s.org/v1/docs/service.html
就会出现编译错误

pattern type is incompatible with expected type;
 found   : org.http4s.AuthedRequest[F,A]
    (which expands to)  org.http4s.ContextRequest[F,A]
 required: org.http4s.Request[F]

您可以在www.example.com上找到如何将AuthedRoutesHttpRoutes组合的示例https://http4s.org/v1/docs/auth.html#composing-authenticated-routes

val spanishRoutes: AuthedRoutes[User, IO] =
    AuthedRoutes.of {
        case GET -> Root / "hola" as user => Ok(s"Hola, ${user.name}")
    }

val frenchRoutes: HttpRoutes[IO] =
    HttpRoutes.of {
        case GET -> Root / "bonjour" => Ok(s"Bonjour")
    }

val serviceSpanish: HttpRoutes[IO] =
  middleware(spanishRoutes) <+> frenchRoutes

相关问题