Scala-如何在Hammock中从IO [HttpResponse]获取数据?

我有一个简单的方法:

DeleteHash(string key,string cacheSubKey)
        {
            if (string.IsnullOrEmpty(key))
                throw new ArgumentNullException("Some problem here !");

            _database.HashDelete(key,cacheSubKey);
        }

这是一个http客户端。和def retrieveRepositories(url: String,params: String): IO[HttpResponse] = Hammock.getWithOpts(uri"$url",createOpts).exec[IO] 解码器:

json

现在我要这样称呼此客户:

implicit def decodeResponseEntity(response: HttpResponse): Either[CodecException,List[GitRepository]] = Decoder[List[GitRepository]].decode(response.entity)

但是,def getRepos(organization: String,params: String): F[Either[CodecException,List[GitRepository]]] = for { res <- retrieveRepositories(organization,params) result <- Sync[F].delay(decodeResponseEntity(res)) } yield result 行有问题,因为我得到了一个错误:result <- Sync[F].delay(decodeResponseEntity(res))。当我将Type mismatch. Reguired: IO[B_] but found F[Either[CodecException,List[GitRepository]]]方法添加到unsafeRunSync()时,它可以正常工作,但是我应该在最后而不是此处调用此方法。我该如何解决?

pb9999 回答:Scala-如何在Hammock中从IO [HttpResponse]获取数据?

如果可以的话,您可能希望更改retrieveRepositories的定义并在效果类型(F)上进行参数化,而不要使用具体的IO类型。

如果您无法更改retrieveRepositories,请在LiftIO中添加一个隐式getRepos约束。您将可以使用liftIO方法将具体的IO值提升到F中。另一种选择是使用Async类型类,该类从SyncLiftIO继承。

请参阅liftIO的文档:https://typelevel.org/cats-effect/typeclasses/liftio.html

本文链接:https://www.f2er.com/3127238.html

大家都在问