我有这个方法
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 ")当我尝试使用
fillFileWithLeagues.fill什么都没发生
我正在尝试使用目标api的数据填充文件
fillFileWithLeagues.filldef 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文件必须从
Client.request("https://api.opendota.com/api/leagues")但什么都没发生
也许我应该使用一些sttp或者其他一些工具?
发布于 2022-12-04 14:08:44
如果我们修复代码的缩进,我们会发现如下:
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实例,它还没有被计算。要评估它,需要运行它。例如,如下:
import zio._
object MainApp extends ZIOAppDefault {
def run = fillFileWithLeagues.fill
}https://stackoverflow.com/questions/74669729
复制相似问题