首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在自定义方法中保留ZIO响应

如何在自定义方法中保留ZIO响应
EN

Stack Overflow用户
提问于 2022-12-03 18:56:52
回答 1查看 23关注 0票数 0

我有这个方法

代码语言:javascript
复制
import ClientServer.*
import zio.http.{Client, *}
import zio.json.*
import zio.http.model.Method
import zio.{ExitCode, URIO, ZIO}
import sttp.capabilities.*
import sttp.client3.Request
import zio.*
import zio.http.model.Headers.Header
import zio.http.model.Version.Http_1_0
import zio.stream.*
import java.net.InetAddress
import sttp.model.sse.ServerSentEvent
import sttp.client3._



object fillFileWithLeagues:

  def fill = for {
    openDotaResponse <- Client.request("https://api.opendota.com/api/leagues")
    bodyOfResponse <- openDotaResponse.body.asString
    listOfLeagues <- ZIO.fromEither(bodyOfResponse.fromJson[List[League]].left.map(error => new Exception(error)))
    save = FileStorage.saveToFile(listOfLeagues.toJson) //Ok
    }yield ()
    println("Im here fillFileWithLeagues.fill ")

当我尝试使用

代码语言:javascript
复制
fillFileWithLeagues.fill

什么都没发生

我正在尝试使用目标api的数据填充文件

代码语言:javascript
复制
fillFileWithLeagues.fill
代码语言:javascript
复制
def readFromFileV8(path: Path = Path("src", "main", "resources", "data.json")): ZIO[Any, Throwable, String] =
  val zioStr = (for bool <- Files.isReadable(path) yield bool).flatMap(bool =>
  if (bool) Files.readAllLines(path, Charset.Standard.utf8).map(_.head)
  else {
    fillFileWithLeagues.fill
    wait(10000)
    println("im here readFromFileV8")
    readFromFileV8()})
  zioStr

我希望data.json文件必须从

代码语言:javascript
复制
Client.request("https://api.opendota.com/api/leagues")

但什么都没发生

也许我应该使用一些sttp或者其他一些工具?

EN

回答 1

Stack Overflow用户

发布于 2022-12-04 14:08:44

如果我们修复代码的缩进,我们会发现如下:

代码语言:javascript
复制
object fillFileWithLeagues {

  def fill = {
    for {
      openDotaResponse <- Client.request("https://api.opendota.com/api/leagues")
      bodyOfResponse <- openDotaResponse.body.asString
      listOfLeagues <- ZIO.fromEither(bodyOfResponse.fromJson[List[League]].left.map(error => new Exception(error)))
      save = FileStorage.saveToFile(listOfLeagues.toJson) //Ok
    } yield ()
  }

  println("Im here fillFileWithLeagues.fill ")
}

正如您所看到的,println是fillFileWithLeagues的一部分,而不是fill的一部分。

另一个潜在的问题是,像fillFileWithLeagues.fill这样的表达式只返回一个ZIO实例,它还没有被计算。要评估它,需要运行它。例如,如下:

代码语言:javascript
复制
import zio._

object MainApp extends ZIOAppDefault {
  def run = fillFileWithLeagues.fill
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74669729

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档